2 * Ceph Dashboard node script
3 * This file should be used to aggregate all external scripts we need.
4 * Multiple flags can be used in the same call.
7 * --env: Generates angular environment files.
9 * --pre: Modifies 'angular.json' to enable the build of custom locales using
11 * Languages can be defined using the environment variable DASHBOARD_FRONTEND_LANGS,
12 * if no value is provided all languages will be build.
13 * Default language is always build, even if not provided.
14 * p.e.: 'DASHBOARD_FRONTEND_LANGS="pt" node cd --pre', will build EN and PT.
15 * For backward compatibility we accept both long and short version of
16 * languages, p.e.: 'pt' and 'pt-BR'
18 * --res: Restores 'angular.json' to its original and removes the backup file.
21 const fs
= require('fs');
23 const filename
= './angular.json';
24 const backup
= './angular.backup.json';
26 if (process
.argv
.includes('--env')) {
30 if (process
.argv
.includes('--pre')) {
34 if (process
.argv
.includes('--res')) {
38 function prepareLocales() {
40 fs
.accessSync(backup
, fs
.constants
.F_OK
);
41 logger(`'${backup}' already exists, restoring it into '${filename}'}`);
42 fs
.copyFileSync(backup
, filename
);
44 fs
.copyFileSync(filename
, backup
);
45 logger(`'${filename}' was copied to '${backup}'`);
48 let langs
= process
.env
.DASHBOARD_FRONTEND_LANGS
|| '';
50 logger(`Preparing build of all languages.`);
52 } else if (langs
.length
== 0) {
54 logger(`Preparing build of EN.`);
56 langs
= langs
.split(/[ ,]/);
57 logger(`Preparing build of EN and ${langs}.`);
60 let angular
= require(filename
);
62 let allLocales
= angular
['projects']['ceph-dashboard']['i18n']['locales'];
65 langs
.forEach((lang
) => {
66 const short = lang
.substring(0, 2);
67 locale
= allLocales
[short];
69 locales
[short] = locale
;
74 locales
['zh-Hans'] = allLocales
['zh-Hans'];
79 locales
['zh-Hant'] = allLocales
['zh-Hant'];
85 angular
['projects']['ceph-dashboard']['i18n']['locales'] = locales
;
86 const newAngular
= JSON
.stringify(angular
, null, 2);
88 fs
.writeFile(filename
, newAngular
, (err
) => {
90 logger(`Writing to ${filename}`);
94 function restoreLocales() {
95 fs
.access(backup
, fs
.constants
.F_OK
, (err
) => {
96 logger(`'${backup}' ${err ? 'does not exist' : 'exists'}`);
99 fs
.copyFile(backup
, filename
, (err
) => {
101 logger(`'${backup}' was copied to '${filename}'`);
103 fs
.unlink(backup
, (err
) => {
105 logger(`successfully deleted '${backup}'`);
112 function envBuild() {
113 origFile
= 'src/environments/environment.tpl.ts';
114 devFile
= 'src/environments/environment.ts';
115 prodFile
= 'src/environments/environment.prod.ts';
117 const replacements
= [
118 { from: '{DEFAULT_LANG}', to
: process
.env
.npm_package_config_locale
},
119 { from: '{COPYRIGHT_YEAR}', to
: new Date().getFullYear() }
121 let dev
= replacements
.concat([{ from: `'{PRODUCTION}'`, to
: false }]);
122 let prod
= replacements
.concat([{ from: `'{PRODUCTION}'`, to
: true }]);
124 fs
.copyFile(origFile
, devFile
, (err
) => {
126 logger(`'${origFile}' was copied to '${devFile}'`);
128 replace(devFile
, dev
);
131 fs
.copyFile(origFile
, prodFile
, (err
) => {
133 logger(`'${origFile}' was copied to '${prodFile}'`);
135 replace(prodFile
, prod
);
140 * Replace strings in a file.
142 * @param {*} filename Relative path to the file
143 * @param {*} replacements List of replacements, each should have from' and 'to'
146 function replace(filename
, replacements
) {
147 fs
.readFile(filename
, 'utf8', (err
, data
) => {
150 replacements
.forEach((rep
) => {
151 data
= data
.replace(rep
.from, rep
.to
);
154 fs
.writeFile(filename
, data
, 'utf8', (err
) => {
156 logger(`Placeholders were replace in '${filename}'`);
162 * Writes logs to the console using the [cd.js] prefix
164 function logger(message
) {
165 console
.log(`[cd.js] ${message}`);