]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/cd.js
import ceph 16.2.6
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / cd.js
1 /**
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.
5 *
6 * Available flags:
7 * --env: Generates angular environment files.
8 *
9 * --pre: Modifies 'angular.json' to enable the build of custom locales using
10 * angular --localize.
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'
17 *
18 * --res: Restores 'angular.json' to its original and removes the backup file.
19 */
20
21 const fs = require('fs');
22
23 const filename = './angular.json';
24 const backup = './angular.backup.json';
25
26 if (process.argv.includes('--env')) {
27 envBuild();
28 }
29
30 if (process.argv.includes('--pre')) {
31 prepareLocales();
32 }
33
34 if (process.argv.includes('--res')) {
35 restoreLocales();
36 }
37
38 function prepareLocales() {
39 try {
40 fs.accessSync(backup, fs.constants.F_OK);
41 logger(`'${backup}' already exists, restoring it into '${filename}'}`);
42 fs.copyFileSync(backup, filename);
43 } catch (err) {
44 fs.copyFileSync(filename, backup);
45 logger(`'${filename}' was copied to '${backup}'`);
46 }
47
48 let langs = process.env.DASHBOARD_FRONTEND_LANGS || '';
49 if (langs == 'ALL') {
50 logger(`Preparing build of all languages.`);
51 return;
52 } else if (langs.length == 0) {
53 langs = [];
54 logger(`Preparing build of EN.`);
55 } else {
56 langs = langs.split(/[ ,]/);
57 logger(`Preparing build of EN and ${langs}.`);
58 }
59
60 let angular = require(filename);
61
62 let allLocales = angular['projects']['ceph-dashboard']['i18n']['locales'];
63 let locales = {};
64
65 langs.forEach((lang) => {
66 const short = lang.substring(0, 2);
67 locale = allLocales[short];
68 if (locale) {
69 locales[short] = locale;
70 } else {
71 switch (lang) {
72 case 'zh-Hans':
73 case 'zh-CN':
74 locales['zh-Hans'] = allLocales['zh-Hans'];
75 break;
76
77 case 'zh-TW':
78 case 'zh-Hant':
79 locales['zh-Hant'] = allLocales['zh-Hant'];
80 break;
81 }
82 }
83 });
84
85 angular['projects']['ceph-dashboard']['i18n']['locales'] = locales;
86 const newAngular = JSON.stringify(angular, null, 2);
87
88 fs.writeFile(filename, newAngular, (err) => {
89 if (err) throw err;
90 logger(`Writing to ${filename}`);
91 });
92 }
93
94 function restoreLocales() {
95 fs.access(backup, fs.constants.F_OK, (err) => {
96 logger(`'${backup}' ${err ? 'does not exist' : 'exists'}`);
97
98 if (!err) {
99 fs.copyFile(backup, filename, (err) => {
100 if (err) throw err;
101 logger(`'${backup}' was copied to '${filename}'`);
102
103 fs.unlink(backup, (err) => {
104 if (err) throw err;
105 logger(`successfully deleted '${backup}'`);
106 });
107 });
108 }
109 });
110 }
111
112 function envBuild() {
113 origFile = 'src/environments/environment.tpl.ts';
114 devFile = 'src/environments/environment.ts';
115 prodFile = 'src/environments/environment.prod.ts';
116
117 const replacements = [
118 { from: '{DEFAULT_LANG}', to: process.env.npm_package_config_locale },
119 { from: '{COPYRIGHT_YEAR}', to: new Date().getFullYear() }
120 ];
121 let dev = replacements.concat([{ from: `'{PRODUCTION}'`, to: false }]);
122 let prod = replacements.concat([{ from: `'{PRODUCTION}'`, to: true }]);
123
124 fs.copyFile(origFile, devFile, (err) => {
125 if (err) throw err;
126 logger(`'${origFile}' was copied to '${devFile}'`);
127
128 replace(devFile, dev);
129 });
130
131 fs.copyFile(origFile, prodFile, (err) => {
132 if (err) throw err;
133 logger(`'${origFile}' was copied to '${prodFile}'`);
134
135 replace(prodFile, prod);
136 });
137 }
138
139 /**
140 * Replace strings in a file.
141 *
142 * @param {*} filename Relative path to the file
143 * @param {*} replacements List of replacements, each should have from' and 'to'
144 * proprieties.
145 */
146 function replace(filename, replacements) {
147 fs.readFile(filename, 'utf8', (err, data) => {
148 if (err) throw err;
149
150 replacements.forEach((rep) => {
151 data = data.replace(rep.from, rep.to);
152 });
153
154 fs.writeFile(filename, data, 'utf8', (err) => {
155 if (err) throw err;
156 logger(`Placeholders were replace in '${filename}'`);
157 });
158 });
159 }
160
161 /**
162 * Writes logs to the console using the [cd.js] prefix
163 */
164 function logger(message) {
165 console.log(`[cd.js] ${message}`);
166 }