forked from microsoft/ccf-app-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_bundle.js
67 lines (55 loc) · 1.66 KB
/
build_bundle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { readdirSync, statSync, readFileSync, writeFileSync } from "fs";
import { join, posix, sep } from "path";
const args = process.argv.slice(2);
const getAllFiles = function (dirPath, arrayOfFiles) {
arrayOfFiles = arrayOfFiles || [];
const files = readdirSync(dirPath);
for (const file of files) {
const filePath = join(dirPath, file);
if (statSync(filePath).isDirectory()) {
arrayOfFiles = getAllFiles(filePath, arrayOfFiles);
} else {
arrayOfFiles.push(filePath);
}
}
return arrayOfFiles;
};
const removePrefix = function (s, prefix) {
return s.substr(prefix.length).split(sep).join(posix.sep);
};
const rootDir = args[0];
const metadataPath = join(rootDir, "app.json");
const metadata = JSON.parse(readFileSync(metadataPath, "utf-8"));
const srcDir = join(rootDir, "src");
const allFiles = getAllFiles(srcDir);
// The trailing / is included so that it is trimmed in removePrefix.
// This produces "foo/bar.js" rather than "/foo/bar.js"
const toTrim = srcDir + "/";
const modules = allFiles.map(function (filePath) {
return {
name: removePrefix(filePath, toTrim),
module: readFileSync(filePath, "utf-8"),
};
});
const bundlePath = join(args[0], "bundle.json");
const appRegPath = join(args[0], "set_js_app.json");
const bundle = {
metadata: metadata,
modules: modules,
};
const app_reg = {
actions: [
{
name: "set_js_app",
args: {
bundle: bundle,
disable_bytecode_cache: false,
},
},
],
};
console.log(
`Writing bundle containing ${modules.length} modules to ${bundlePath}`
);
writeFileSync(bundlePath, JSON.stringify(bundle));
writeFileSync(appRegPath, JSON.stringify(app_reg));