]> git.proxmox.com Git - mirror_novnc.git/blob - utils/use_require.js
feat: add French localization strings
[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 program
10 .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) ')
11 .option('--clean', 'clear the lib folder before building')
12 .parse(process.argv);
13
14 // the various important paths
15 const paths = {
16 main: path.resolve(__dirname, '..'),
17 core: path.resolve(__dirname, '..', 'core'),
18 vendor: path.resolve(__dirname, '..', 'vendor'),
19 libDirBase: path.resolve(__dirname, '..', 'lib'),
20 };
21
22 // util.promisify requires Node.js 8.x, so we have our own
23 function promisify(original) {
24 return function promiseWrap() {
25 const args = Array.prototype.slice.call(arguments);
26 return new Promise((resolve, reject) => {
27 original.apply(this, args.concat((err, value) => {
28 if (err) return reject(err);
29 resolve(value);
30 }));
31 });
32 };
33 }
34
35 const writeFile = promisify(fs.writeFile);
36
37 const readdir = promisify(fs.readdir);
38 const lstat = promisify(fs.lstat);
39
40 const ensureDir = promisify(fse.ensureDir);
41
42 const babelTransformFile = promisify(babel.transformFile);
43
44 // walkDir *recursively* walks directories trees,
45 // calling the callback for all normal files found.
46 function walkDir(basePath, cb, filter) {
47 return readdir(basePath)
48 .then((files) => {
49 const paths = files.map(filename => path.join(basePath, filename));
50 return Promise.all(paths.map(filepath => lstat(filepath)
51 .then((stats) => {
52 if (filter !== undefined && !filter(filepath, stats)) return;
53
54 if (stats.isSymbolicLink()) return;
55 if (stats.isFile()) return cb(filepath);
56 if (stats.isDirectory()) return walkDir(filepath, cb, filter);
57 })));
58 });
59 }
60
61 function makeLibFiles(sourceMaps) {
62 // NB: we need to make a copy of babelOpts, since babel sets some defaults on it
63 const babelOpts = () => ({
64 plugins: [],
65 presets: [
66 [ '@babel/preset-env',
67 { modules: 'commonjs' } ]
68 ],
69 ast: false,
70 sourceMaps: sourceMaps,
71 });
72
73 fse.ensureDirSync(paths.libDirBase);
74
75 const outFiles = [];
76
77 const handleDir = (vendorRewrite, inPathBase, filename) => Promise.resolve()
78 .then(() => {
79 const outPath = path.join(paths.libDirBase, path.relative(inPathBase, filename));
80
81 if (path.extname(filename) !== '.js') {
82 return; // skip non-javascript files
83 }
84 return Promise.resolve()
85 .then(() => ensureDir(path.dirname(outPath)))
86 .then(() => {
87 const opts = babelOpts();
88 // Adjust for the fact that we move the core files relative
89 // to the vendor directory
90 if (vendorRewrite) {
91 opts.plugins.push(["import-redirect",
92 {"root": paths.libDirBase,
93 "redirect": { "vendor/(.+)": "./vendor/$1"}}]);
94 }
95
96 return babelTransformFile(filename, opts)
97 .then((res) => {
98 console.log(`Writing ${outPath}`);
99 const {map} = res;
100 let {code} = res;
101 if (sourceMaps === true) {
102 // append URL for external source map
103 code += `\n//# sourceMappingURL=${path.basename(outPath)}.map\n`;
104 }
105 outFiles.push(`${outPath}`);
106 return writeFile(outPath, code)
107 .then(() => {
108 if (sourceMaps === true || sourceMaps === 'both') {
109 console.log(` and ${outPath}.map`);
110 outFiles.push(`${outPath}.map`);
111 return writeFile(`${outPath}.map`, JSON.stringify(map));
112 }
113 });
114 });
115 });
116 });
117
118 Promise.resolve()
119 .then(() => {
120 const handler = handleDir.bind(null, false, paths.main);
121 return walkDir(paths.vendor, handler);
122 })
123 .then(() => {
124 const handler = handleDir.bind(null, true, paths.core);
125 return walkDir(paths.core, handler);
126 })
127 .catch((err) => {
128 console.error(`Failure converting modules: ${err}`);
129 process.exit(1);
130 });
131 }
132
133 if (program.clean) {
134 console.log(`Removing ${paths.libDirBase}`);
135 fse.removeSync(paths.libDirBase);
136 }
137
138 makeLibFiles(program.withSourceMaps);