Commit | Line | Data |
---|---|---|
5480a636 AE |
1 | const childProcess = require("child_process"); |
2 | const compiler = require("./compiler"); | |
3 | const fs = require("fs"); | |
4 | const path = require("path"); | |
5 | ||
6 | if (process.argv.length !== 3) { | |
7 | throw new Error("Requires the path to an existing repository."); | |
8 | } | |
9 | ||
10 | const repository = process.argv[2]; | |
11 | if (!fs.existsSync(repository)) { | |
12 | throw new Error(`Unable to locate repsitory, the path ${repository} is invalid.`); | |
13 | } | |
14 | process.chdir(repository); | |
15 | ||
16 | let rjsPaths = []; | |
17 | ||
18 | // get all directories at the repository root | |
19 | fs.readdirSync("./") | |
20 | .filter(directory => fs.statSync(directory).isDirectory()) | |
21 | .forEach(directory => { | |
22 | // look for a generic `js` directory | |
23 | let path = `./${directory}/js/`; | |
24 | if (fs.existsSync(path)) { | |
25 | fs.readdirSync(path) | |
26 | .filter(filename => { | |
27 | // ignore build configurations | |
28 | if (filename === "require.build.js") { | |
29 | if (rjsPaths.indexOf(path) === -1) rjsPaths.push(path); | |
30 | ||
31 | return false; | |
32 | } | |
33 | ||
34 | let stat = fs.statSync(path + filename); | |
35 | // allow only non-minified *.js files | |
36 | if (stat.isFile() && !stat.isSymbolicLink() && filename.match(/\.js$/) && !filename.match(/\.min\.js$/)) { | |
37 | return true; | |
38 | } | |
39 | ||
40 | return false; | |
41 | }) | |
238a4492 TD |
42 | .forEach(async filename => { |
43 | for (const COMPILER_TARGET_DEFAULT of [true, false]) { | |
5480a636 AE |
44 | let outFilename = filename.replace(/\.js$/, (COMPILER_TARGET_DEFAULT ? "" : ".tiny") + ".min.js"); |
45 | console.time(outFilename); | |
46 | { | |
238a4492 | 47 | let output = await compiler.compile(fs.readFileSync(path + filename, 'utf-8'), { |
5480a636 AE |
48 | compress: { |
49 | global_defs: { | |
50 | COMPILER_TARGET_DEFAULT: COMPILER_TARGET_DEFAULT | |
51 | } | |
52 | } | |
53 | }); | |
54 | ||
55 | fs.writeFileSync(path + outFilename, output.code); | |
56 | } | |
57 | console.timeEnd(outFilename); | |
238a4492 | 58 | }; |
5480a636 AE |
59 | }); |
60 | } | |
61 | }); | |
62 | ||
63 | const rjsCmd = (process.platform === "win32") ? "r.js.cmd" : "r.js"; | |
238a4492 | 64 | rjsPaths.forEach(async path => { |
5480a636 AE |
65 | let buildConfig = `${path}require.build.js`; |
66 | let outFilename = require(process.cwd() + `/${buildConfig}`).out; | |
67 | ||
238a4492 TD |
68 | for (const COMPILER_TARGET_DEFAULT of [true, false]) { |
69 | let overrides = ""; | |
5480a636 AE |
70 | if (!COMPILER_TARGET_DEFAULT) { |
71 | outFilename = outFilename.replace(/\.min\.js$/, '.tiny.min.js'); | |
72 | overrides += " out=" + outFilename; | |
73 | } | |
74 | ||
75 | console.time(outFilename); | |
76 | childProcess.execSync(`${rjsCmd} -o require.build.js ${overrides}`, { | |
77 | cwd: path, | |
78 | stdio: [0, 1, 2] | |
79 | }); | |
238a4492 TD |
80 | const output = await compiler.compile(fs.readFileSync(path + '/' + outFilename, "utf-8")); |
81 | fs.writeFileSync(path + '/' + outFilename, output.code); | |
5480a636 | 82 | console.timeEnd(outFilename); |
238a4492 | 83 | }; |
5480a636 | 84 | }); |