]>
Commit | Line | Data |
---|---|---|
ae510306 SR |
1 | #!/usr/bin/env node |
2 | ||
2b5f94fa JD |
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'); | |
ae510306 | 8 | |
6cae7b58 | 9 | const SUPPORTED_FORMATS = new Set(['amd', 'commonjs', 'systemjs', 'umd']); |
ae510306 SR |
10 | |
11 | program | |
6cae7b58 SR |
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') | |
4a65d50d | 15 | .option('--only-legacy', 'only output legacy files (no ES6 modules) for the app') |
c4e5a50e | 16 | .option('--clean', 'clear the lib folder before building') |
ae510306 SR |
17 | .parse(process.argv); |
18 | ||
19 | // the various important paths | |
152c3995 SR |
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 | }; | |
ae510306 | 28 | |
adfc9d3f SR |
29 | const no_copy_files = new Set([ |
30 | // skip these -- they don't belong in the processed application | |
152c3995 SR |
31 | path.join(paths.vendor, 'sinon.js'), |
32 | path.join(paths.vendor, 'browser-es-module-loader'), | |
33 | path.join(paths.vendor, 'promise.js'), | |
d584c5f6 | 34 | path.join(paths.app, 'images', 'icons', 'Makefile'), |
adfc9d3f SR |
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 | |
152c3995 | 39 | path.join(paths.app, 'error-handler.js'), |
adfc9d3f SR |
40 | ]); |
41 | ||
651c23ec | 42 | no_copy_files.forEach(file => no_transform_files.add(file)); |
152c3995 | 43 | |
21633268 PO |
44 | // util.promisify requires Node.js 8.x, so we have our own |
45 | function promisify(original) { | |
e7777653 | 46 | return function promise_wrap() { |
2b5f94fa | 47 | const args = Array.prototype.slice.call(arguments); |
21633268 | 48 | return new Promise((resolve, reject) => { |
651c23ec | 49 | original.apply(this, args.concat((err, value) => { |
21633268 PO |
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); | |
be7b4e88 | 64 | const unlink = promisify(fse.unlink); |
21633268 | 65 | const ensureDir = promisify(fse.ensureDir); |
be7b4e88 | 66 | const rmdir = promisify(fse.rmdir); |
21633268 PO |
67 | |
68 | const babelTransformFile = promisify(babel.transformFile); | |
69 | ||
6cae7b58 SR |
70 | // walkDir *recursively* walks directories trees, |
71 | // calling the callback for all normal files found. | |
651c23ec | 72 | function walkDir(base_path, cb, filter) { |
21633268 | 73 | return readdir(base_path) |
7b536961 PO |
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 | }); | |
651c23ec | 85 | } |
ae510306 | 86 | |
651c23ec | 87 | function transform_html (legacy_scripts, only_legacy) { |
ae510306 | 88 | // write out the modified vnc.html file that works with the bundle |
2b5f94fa JD |
89 | const src_html_path = path.resolve(__dirname, '..', 'vnc.html'); |
90 | const out_html_path = path.resolve(paths.out_dir_base, 'vnc.html'); | |
21633268 | 91 | return readFile(src_html_path) |
7b536961 PO |
92 | .then((contents_raw) => { |
93 | let contents = contents_raw.toString(); | |
ae510306 | 94 | |
7b536961 PO |
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); | |
ae510306 | 99 | |
7b536961 | 100 | let new_script = ''; |
4a65d50d | 101 | |
7b536961 | 102 | if (only_legacy) { |
4a65d50d | 103 | // Only legacy version, so include things directly |
7b536961 PO |
104 | for (let i = 0;i < legacy_scripts.length;i++) { |
105 | new_script += ` <script src="${legacy_scripts[i]}"></script>\n`; | |
106 | } | |
107 | } else { | |
4a65d50d PO |
108 | // Otherwise detect if it's a modern browser and select |
109 | // variant accordingly | |
7b536961 | 110 | new_script += `\ |
4a65d50d PO |
111 | <script type="module">\n\ |
112 | window._noVNC_has_module_support = true;\n\ | |
113 | </script>\n\ | |
114 | <script>\n\ | |
115 | window.addEventListener("load", function() {\n\ | |
116 | if (window._noVNC_has_module_support) return;\n\ | |
117 | let legacy_scripts = ${JSON.stringify(legacy_scripts)};\n\ | |
118 | for (let i = 0;i < legacy_scripts.length;i++) {\n\ | |
119 | let script = document.createElement("script");\n\ | |
120 | script.src = legacy_scripts[i];\n\ | |
121 | script.async = false;\n\ | |
122 | document.head.appendChild(script);\n\ | |
123 | }\n\ | |
124 | });\n\ | |
125 | </script>\n`; | |
126 | ||
127 | // Original, ES6 modules | |
7b536961 PO |
128 | new_script += ' <script type="module" crossorigin="anonymous" src="app/ui.js"></script>\n'; |
129 | } | |
4a65d50d | 130 | |
7b536961 | 131 | contents = contents.slice(0, start_ind) + `${new_script}\n` + contents.slice(end_ind); |
ae510306 | 132 | |
7b536961 PO |
133 | return contents; |
134 | }) | |
135 | .then((contents) => { | |
136 | console.log(`Writing ${out_html_path}`); | |
137 | return writeFile(out_html_path, contents); | |
138 | }); | |
6cae7b58 | 139 | } |
ae510306 | 140 | |
651c23ec | 141 | function make_lib_files(import_format, source_maps, with_app_dir, only_legacy) { |
6cae7b58 SR |
142 | if (!import_format) { |
143 | throw new Error("you must specify an import format to generate compiled noVNC libraries"); | |
144 | } else if (!SUPPORTED_FORMATS.has(import_format)) { | |
145 | throw new Error(`unsupported output format "${import_format}" for import/export -- only ${Array.from(SUPPORTED_FORMATS)} are supported`); | |
146 | } | |
147 | ||
148 | // NB: we need to make a copy of babel_opts, since babel sets some defaults on it | |
149 | const babel_opts = () => ({ | |
150 | plugins: [`transform-es2015-modules-${import_format}`], | |
cdb860ad | 151 | presets: ['es2015'], |
6cae7b58 SR |
152 | ast: false, |
153 | sourceMaps: source_maps, | |
154 | }); | |
6cae7b58 | 155 | |
4a65d50d PO |
156 | // No point in duplicate files without the app, so force only converted files |
157 | if (!with_app_dir) { | |
158 | only_legacy = true; | |
159 | } | |
160 | ||
2b5f94fa JD |
161 | let in_path; |
162 | let out_path_base; | |
6cae7b58 | 163 | if (with_app_dir) { |
8727f598 | 164 | out_path_base = paths.out_dir_base; |
152c3995 | 165 | in_path = paths.main; |
6cae7b58 | 166 | } else { |
8727f598 | 167 | out_path_base = paths.lib_dir_base; |
6cae7b58 | 168 | } |
4a65d50d | 169 | const legacy_path_base = only_legacy ? out_path_base : path.join(out_path_base, 'legacy'); |
6cae7b58 SR |
170 | |
171 | fse.ensureDirSync(out_path_base); | |
172 | ||
173 | const helpers = require('./use_require_helpers'); | |
174 | const helper = helpers[import_format]; | |
a80955ee | 175 | |
be7b4e88 PO |
176 | const outFiles = []; |
177 | ||
2b5f94fa | 178 | const handleDir = (js_only, vendor_rewrite, in_path_base, filename) => Promise.resolve() |
7b536961 PO |
179 | .then(() => { |
180 | if (no_copy_files.has(filename)) return; | |
adfc9d3f | 181 | |
7b536961 PO |
182 | const out_path = path.join(out_path_base, path.relative(in_path_base, filename)); |
183 | const legacy_path = path.join(legacy_path_base, path.relative(in_path_base, filename)); | |
4a65d50d | 184 | |
7b536961 PO |
185 | if(path.extname(filename) !== '.js') { |
186 | if (!js_only) { | |
187 | console.log(`Writing ${out_path}`); | |
188 | return copy(filename, out_path); | |
189 | } | |
190 | return; // skip non-javascript files | |
ae510306 | 191 | } |
ae510306 | 192 | |
7b536961 PO |
193 | return Promise.resolve() |
194 | .then(() => { | |
195 | if (only_legacy && !no_transform_files.has(filename)) { | |
196 | return; | |
197 | } | |
198 | return ensureDir(path.dirname(out_path)) | |
199 | .then(() => { | |
200 | console.log(`Writing ${out_path}`); | |
201 | return copy(filename, out_path); | |
202 | }) | |
203 | }) | |
204 | .then(() => ensureDir(path.dirname(legacy_path))) | |
205 | .then(() => { | |
206 | if (no_transform_files.has(filename)) { | |
207 | return; | |
208 | } | |
adfc9d3f | 209 | |
7b536961 PO |
210 | const opts = babel_opts(); |
211 | if (helper && helpers.optionsOverride) { | |
212 | helper.optionsOverride(opts); | |
213 | } | |
1524df89 PO |
214 | // Adjust for the fact that we move the core files relative |
215 | // to the vendor directory | |
7b536961 PO |
216 | if (vendor_rewrite) { |
217 | opts.plugins.push(["import-redirect", | |
218 | {"root": legacy_path_base, | |
219 | "redirect": { "vendor/(.+)": "./vendor/$1"}}]); | |
220 | } | |
adfc9d3f | 221 | |
7b536961 PO |
222 | return babelTransformFile(filename, opts) |
223 | .then((res) => { | |
224 | console.log(`Writing ${legacy_path}`); | |
225 | const {map} = res; | |
226 | let {code} = res; | |
227 | if (source_maps === true) { | |
6cae7b58 | 228 | // append URL for external source map |
7b536961 PO |
229 | code += `\n//# sourceMappingURL=${path.basename(legacy_path)}.map\n`; |
230 | } | |
231 | outFiles.push(`${legacy_path}`); | |
232 | return writeFile(legacy_path, code) | |
233 | .then(() => { | |
234 | if (source_maps === true || source_maps === 'both') { | |
235 | console.log(` and ${legacy_path}.map`); | |
236 | outFiles.push(`${legacy_path}.map`); | |
237 | return writeFile(`${legacy_path}.map`, JSON.stringify(map)); | |
238 | } | |
239 | }); | |
240 | }); | |
21633268 | 241 | }); |
6cae7b58 | 242 | }); |
6cae7b58 | 243 | |
152c3995 SR |
244 | if (with_app_dir && helper && helper.noCopyOverride) { |
245 | helper.noCopyOverride(paths, no_copy_files); | |
246 | } | |
247 | ||
21633268 | 248 | Promise.resolve() |
7b536961 PO |
249 | .then(() => { |
250 | const handler = handleDir.bind(null, true, false, in_path || paths.main); | |
251 | const filter = (filename, stats) => !no_copy_files.has(filename); | |
252 | return walkDir(paths.vendor, handler, filter); | |
253 | }) | |
254 | .then(() => { | |
255 | const handler = handleDir.bind(null, true, !in_path, in_path || paths.core); | |
256 | const filter = (filename, stats) => !no_copy_files.has(filename); | |
257 | return walkDir(paths.core, handler, filter); | |
258 | }) | |
259 | .then(() => { | |
260 | if (!with_app_dir) return; | |
261 | const handler = handleDir.bind(null, false, false, in_path); | |
262 | const filter = (filename, stats) => !no_copy_files.has(filename); | |
263 | return walkDir(paths.app, handler, filter); | |
4a65d50d | 264 | }) |
be7b4e88 | 265 | .then(() => { |
7b536961 PO |
266 | if (!with_app_dir) return; |
267 | ||
268 | if (!helper || !helper.appWriter) { | |
269 | throw new Error(`Unable to generate app for the ${import_format} format!`); | |
270 | } | |
271 | ||
272 | const out_app_path = path.join(legacy_path_base, 'app.js'); | |
273 | console.log(`Writing ${out_app_path}`); | |
274 | return helper.appWriter(out_path_base, legacy_path_base, out_app_path) | |
275 | .then((extra_scripts) => { | |
276 | const rel_app_path = path.relative(out_path_base, out_app_path); | |
277 | const legacy_scripts = extra_scripts.concat([rel_app_path]); | |
278 | transform_html(legacy_scripts, only_legacy); | |
279 | }) | |
be7b4e88 | 280 | .then(() => { |
7b536961 PO |
281 | if (!helper.removeModules) return; |
282 | console.log(`Cleaning up temporary files...`); | |
283 | return Promise.all(outFiles.map((filepath) => { | |
284 | unlink(filepath) | |
285 | .then(() => { | |
be7b4e88 PO |
286 | // Try to clean up any empty directories if this |
287 | // was the last file in there | |
7b536961 PO |
288 | const rmdir_r = dir => |
289 | rmdir(dir) | |
290 | .then(() => rmdir_r(path.dirname(dir))) | |
291 | .catch(() => { | |
be7b4e88 | 292 | // Assume the error was ENOTEMPTY and ignore it |
7b536961 PO |
293 | }); |
294 | return rmdir_r(path.dirname(filepath)); | |
295 | }); | |
296 | })); | |
be7b4e88 | 297 | }); |
7b536961 PO |
298 | }) |
299 | .catch((err) => { | |
300 | console.error(`Failure converting modules: ${err}`); | |
301 | process.exit(1); | |
be7b4e88 | 302 | }); |
651c23ec | 303 | } |
ae510306 | 304 | |
c4e5a50e SR |
305 | if (program.clean) { |
306 | console.log(`Removing ${paths.lib_dir_base}`); | |
307 | fse.removeSync(paths.lib_dir_base); | |
308 | ||
309 | console.log(`Removing ${paths.out_dir_base}`); | |
310 | fse.removeSync(paths.out_dir_base); | |
311 | } | |
312 | ||
4a65d50d | 313 | make_lib_files(program.as, program.withSourceMaps, program.withApp, program.onlyLegacy); |