-
Notifications
You must be signed in to change notification settings - Fork 10
/
build_bundle.js
56 lines (44 loc) · 1.48 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
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 bundle = {
metadata: metadata,
modules: modules,
};
console.log(
`Writing bundle containing ${modules.length} modules to ${bundlePath}`,
);
writeFileSync(bundlePath, JSON.stringify(bundle));