]> git.proxmox.com Git - mirror_novnc.git/blob - utils/use_require.js
Update zh_CN.po
[mirror_novnc.git] / utils / use_require.js
1 #!/usr/bin/env node
2
3 const path = require('path');
4 const program = require('commander');
5 const fs = require('fs');
6 const fse = require('fs-extra');
7 const babel = require('babel-core');
8
9 const SUPPORTED_FORMATS = new Set(['amd', 'commonjs', 'systemjs', 'umd']);
10
11 program
12 .option('--as [format]', `output files using various import formats instead of ES6 import and export. Supports ${Array.from(SUPPORTED_FORMATS)}.`)
13 .option('-m, --with-source-maps [type]', 'output source maps when not generating a bundled app (type may be empty for external source maps, inline for inline source maps, or both) ')
14 .option('--with-app', 'process app files as well as core files')
15 .option('--only-legacy', 'only output legacy files (no ES6 modules) for the app')
16 .option('--clean', 'clear the lib folder before building')
17 .parse(process.argv);
18
19 // the various important paths
20 const paths = {
21 main: path.resolve(__dirname, '..'),
22 core: path.resolve(__dirname, '..', 'core'),
23 app: path.resolve(__dirname, '..', 'app'),
24 vendor: path.resolve(__dirname, '..', 'vendor'),
25 out_dir_base: path.resolve(__dirname, '..', 'build'),
26 lib_dir_base: path.resolve(__dirname, '..', 'lib'),
27 };
28
29 const no_copy_files = new Set([
30 // skip these -- they don't belong in the processed application
31 path.join(paths.vendor, 'sinon.js'),
32 path.join(paths.vendor, 'browser-es-module-loader'),
33 path.join(paths.vendor, 'promise.js'),
34 path.join(paths.app, 'images', 'icons', 'Makefile'),
35 ]);
36
37 const no_transform_files = new Set([
38 // don't transform this -- we want it imported as-is to properly catch loading errors
39 path.join(paths.app, 'error-handler.js'),
40 ]);
41
42 no_copy_files.forEach(file => no_transform_files.add(file));
43
44 // util.promisify requires Node.js 8.x, so we have our own
45 function promisify(original) {
46 return function promise_wrap() {
47 const args = Array.prototype.slice.call(arguments);
48 return new Promise((resolve, reject) => {
49 original.apply(this, args.concat((err, value) => {
50 if (err) return reject(err);
51 resolve(value);
52 }));
53 });
54 };
55 }
56
57 const readFile = promisify(fs.readFile);
58 const writeFile = promisify(fs.writeFile);
59
60 const readdir = promisify(fs.readdir);
61 const lstat = promisify(fs.lstat);
62
63 const copy = promisify(fse.copy);
64 const unlink = promisify(fse.unlink);
65 const ensureDir = promisify(fse.ensureDir);
66 const rmdir = promisify(fse.rmdir);
67
68 const babelTransformFile = promisify(babel.transformFile);
69
70 // walkDir *recursively* walks directories trees,
71 // calling the callback for all normal files found.
72 function walkDir(base_path, cb, filter) {
73 return readdir(base_path)
74 .then((files) => {
75 const paths = files.map(filename => path.join(base_path, filename));
76 return Promise.all(paths.map(filepath => lstat(filepath)
77 .then((stats) => {
78 if (filter !== undefined && !filter(filepath, stats)) return;
79
80 if (stats.isSymbolicLink()) return;
81 if (stats.isFile()) return cb(filepath);
82 if (stats.isDirectory()) return walkDir(filepath, cb, filter);
83 })));
84 });
85 }
86
87 function transform_html(legacy_scripts, only_legacy) {
88 // write out the modified vnc.html file that works with the bundle
89 const src_html_path = path.resolve(__dirname, '..', 'vnc.html');
90 const out_html_path = path.resolve(paths.out_dir_base, 'vnc.html');
91 return readFile(src_html_path)
92 .then((contents_raw) => {
93 let contents = contents_raw.toString();
94
95 const start_marker = '<!-- begin scripts -->\n';
96 const end_marker = '<!-- end scripts -->';
97 const start_ind = contents.indexOf(start_marker) + start_marker.length;
98 const end_ind = contents.indexOf(end_marker, start_ind);
99
100 let new_script = '';
101
102 if (only_legacy) {
103 // Only legacy version, so include things directly
104 for (let i = 0;i < legacy_scripts.length;i++) {
105 new_script += ` <script src="${legacy_scripts[i]}"></script>\n`;
106 }
107 } else {
108 // Otherwise include both modules and legacy fallbacks
109 new_script += ' <script type="module" crossorigin="anonymous" src="app/ui.js"></script>\n';
110 for (let i = 0;i < legacy_scripts.length;i++) {
111 new_script += ' <script nomodule src="${legacy_scripts[i]}"></script>\n';
112 }
113 }
114
115 contents = contents.slice(0, start_ind) + `${new_script}\n` + contents.slice(end_ind);
116
117 return contents;
118 })
119 .then((contents) => {
120 console.log(`Writing ${out_html_path}`);
121 return writeFile(out_html_path, contents);
122 });
123 }
124
125 function make_lib_files(import_format, source_maps, with_app_dir, only_legacy) {
126 if (!import_format) {
127 throw new Error("you must specify an import format to generate compiled noVNC libraries");
128 } else if (!SUPPORTED_FORMATS.has(import_format)) {
129 throw new Error(`unsupported output format "${import_format}" for import/export -- only ${Array.from(SUPPORTED_FORMATS)} are supported`);
130 }
131
132 // NB: we need to make a copy of babel_opts, since babel sets some defaults on it
133 const babel_opts = () => ({
134 plugins: [`transform-es2015-modules-${import_format}`],
135 presets: ['es2015'],
136 ast: false,
137 sourceMaps: source_maps,
138 });
139
140 // No point in duplicate files without the app, so force only converted files
141 if (!with_app_dir) {
142 only_legacy = true;
143 }
144
145 let in_path;
146 let out_path_base;
147 if (with_app_dir) {
148 out_path_base = paths.out_dir_base;
149 in_path = paths.main;
150 } else {
151 out_path_base = paths.lib_dir_base;
152 }
153 const legacy_path_base = only_legacy ? out_path_base : path.join(out_path_base, 'legacy');
154
155 fse.ensureDirSync(out_path_base);
156
157 const helpers = require('./use_require_helpers');
158 const helper = helpers[import_format];
159
160 const outFiles = [];
161
162 const handleDir = (js_only, vendor_rewrite, in_path_base, filename) => Promise.resolve()
163 .then(() => {
164 if (no_copy_files.has(filename)) return;
165
166 const out_path = path.join(out_path_base, path.relative(in_path_base, filename));
167 const legacy_path = path.join(legacy_path_base, path.relative(in_path_base, filename));
168
169 if (path.extname(filename) !== '.js') {
170 if (!js_only) {
171 console.log(`Writing ${out_path}`);
172 return copy(filename, out_path);
173 }
174 return; // skip non-javascript files
175 }
176
177 return Promise.resolve()
178 .then(() => {
179 if (only_legacy && !no_transform_files.has(filename)) {
180 return;
181 }
182 return ensureDir(path.dirname(out_path))
183 .then(() => {
184 console.log(`Writing ${out_path}`);
185 return copy(filename, out_path);
186 });
187 })
188 .then(() => ensureDir(path.dirname(legacy_path)))
189 .then(() => {
190 if (no_transform_files.has(filename)) {
191 return;
192 }
193
194 const opts = babel_opts();
195 if (helper && helpers.optionsOverride) {
196 helper.optionsOverride(opts);
197 }
198 // Adjust for the fact that we move the core files relative
199 // to the vendor directory
200 if (vendor_rewrite) {
201 opts.plugins.push(["import-redirect",
202 {"root": legacy_path_base,
203 "redirect": { "vendor/(.+)": "./vendor/$1"}}]);
204 }
205
206 return babelTransformFile(filename, opts)
207 .then((res) => {
208 console.log(`Writing ${legacy_path}`);
209 const {map} = res;
210 let {code} = res;
211 if (source_maps === true) {
212 // append URL for external source map
213 code += `\n//# sourceMappingURL=${path.basename(legacy_path)}.map\n`;
214 }
215 outFiles.push(`${legacy_path}`);
216 return writeFile(legacy_path, code)
217 .then(() => {
218 if (source_maps === true || source_maps === 'both') {
219 console.log(` and ${legacy_path}.map`);
220 outFiles.push(`${legacy_path}.map`);
221 return writeFile(`${legacy_path}.map`, JSON.stringify(map));
222 }
223 });
224 });
225 });
226 });
227
228 if (with_app_dir && helper && helper.noCopyOverride) {
229 helper.noCopyOverride(paths, no_copy_files);
230 }
231
232 Promise.resolve()
233 .then(() => {
234 const handler = handleDir.bind(null, true, false, in_path || paths.main);
235 const filter = (filename, stats) => !no_copy_files.has(filename);
236 return walkDir(paths.vendor, handler, filter);
237 })
238 .then(() => {
239 const handler = handleDir.bind(null, true, !in_path, in_path || paths.core);
240 const filter = (filename, stats) => !no_copy_files.has(filename);
241 return walkDir(paths.core, handler, filter);
242 })
243 .then(() => {
244 if (!with_app_dir) return;
245 const handler = handleDir.bind(null, false, false, in_path);
246 const filter = (filename, stats) => !no_copy_files.has(filename);
247 return walkDir(paths.app, handler, filter);
248 })
249 .then(() => {
250 if (!with_app_dir) return;
251
252 if (!helper || !helper.appWriter) {
253 throw new Error(`Unable to generate app for the ${import_format} format!`);
254 }
255
256 const out_app_path = path.join(legacy_path_base, 'app.js');
257 console.log(`Writing ${out_app_path}`);
258 return helper.appWriter(out_path_base, legacy_path_base, out_app_path)
259 .then((extra_scripts) => {
260 const rel_app_path = path.relative(out_path_base, out_app_path);
261 const legacy_scripts = extra_scripts.concat([rel_app_path]);
262 transform_html(legacy_scripts, only_legacy);
263 })
264 .then(() => {
265 if (!helper.removeModules) return;
266 console.log(`Cleaning up temporary files...`);
267 return Promise.all(outFiles.map((filepath) => {
268 unlink(filepath)
269 .then(() => {
270 // Try to clean up any empty directories if this
271 // was the last file in there
272 const rmdir_r = dir =>
273 rmdir(dir)
274 .then(() => rmdir_r(path.dirname(dir)))
275 .catch(() => {
276 // Assume the error was ENOTEMPTY and ignore it
277 });
278 return rmdir_r(path.dirname(filepath));
279 });
280 }));
281 });
282 })
283 .catch((err) => {
284 console.error(`Failure converting modules: ${err}`);
285 process.exit(1);
286 });
287 }
288
289 if (program.clean) {
290 console.log(`Removing ${paths.lib_dir_base}`);
291 fse.removeSync(paths.lib_dir_base);
292
293 console.log(`Removing ${paths.out_dir_base}`);
294 fse.removeSync(paths.out_dir_base);
295 }
296
297 make_lib_files(program.as, program.withSourceMaps, program.withApp, program.onlyLegacy);