forked from vegarringdal/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-builder.js
53 lines (44 loc) · 1.52 KB
/
example-builder.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
const fs = require('fs');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
// Bundler specific constants
const BUNDLERCONFIGNAME = 'rollup.config.js';
const BUNDLERCOMMAND = 'rollup --config ';
// Gets path to recursively bundle
let BASEPATH = './examples';
const arg = process.argv[2];
if(arg) BASEPATH += "/" + arg;
// Gets all directories contained in the current directory
const { promises: { readdir } } = fs;
const getDirectories = async source =>
(await readdir(source, { withFileTypes: true }))
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
// Bundles the specified directory
async function bundlePath(path) {
try {
const { stdout, stderr } = await exec(BUNDLERCOMMAND + path);
if(stdout) console.log(stdout);
if(stderr) console.log(stderr);
} catch (e) {
console.error(e); // should contain code (exit code) and signal (that caused the termination).
}
}
// Bundles the specified dir if it there is a bundler config recursively
async function traverseAndBundle(path) {
const result = await fs.promises.readdir(path);
if(result.length === 0) return;
let isProjectFolder = false;
result.forEach(file => {
if (file === BUNDLERCONFIGNAME) isProjectFolder = true;
});
if (isProjectFolder) {
const bundlerPath = path + '/' + BUNDLERCONFIGNAME;
await bundlePath(bundlerPath);
}
const directories = await getDirectories(path);
for (const directory of directories) {
await traverseAndBundle(path + '/' + directory);
}
}
traverseAndBundle(BASEPATH);