-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample-builder.js
More file actions
114 lines (96 loc) · 2.78 KB
/
example-builder.js
File metadata and controls
114 lines (96 loc) · 2.78 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env node
const { Command } = require("commander");
const path = require("path");
const fs = require("fs");
const { spawn } = require("child_process");
function resolveExample(ex) {
return path.resolve(__dirname, "examples", ex);
}
function getBuildCommand(type) {
const env = { ...process.env };
if (type.startsWith("webpack")) {
if (type === "webpack4") {
env.NODE_OPTIONS = "--openssl-legacy-provider";
}
return {
command: process.platform === "win32" ? "npx.cmd" : "npx",
args: ["webpack", "-c", "./webpack.config.js"],
env,
};
} else {
throw new Error(`Unsupported example type '${type}'.`);
}
}
async function buildExample(dir, silent) {
if (!fs.existsSync(dir) || !fs.lstatSync(dir).isDirectory()) {
throw new Error(`Example at '${dir}' is not a directory.`);
}
const type = path.basename(path.dirname(dir));
const { command, args, env } = getBuildCommand(type);
return new Promise((res, rej) => {
const proc = spawn(command, args, {
cwd: dir,
env,
});
proc.stderr.on("data", (err) => console.error(err.toString("utf-8")));
if (!silent) {
proc.stdout.on("data", (err) => console.log(err.toString("utf-8")));
}
proc.on("exit", (code) => {
if (code === 0) {
res();
} else {
rej();
}
});
});
}
const program = new Command();
program
.name("example-builder")
.description("Builds test bundles for bundle-breaker.");
program
.command("build")
.description("Build a specific example")
.argument(
"<example>",
"Directory containing the example to build relative to the cwd."
)
.action(async (ex) => {
await buildExample(resolveExample(ex), false);
console.log(`Built example '${ex}'`);
});
program
.command("build-all")
.description("Build all examples")
.action(async () => {
const exampleDirs = ["webpack4", "webpack5"];
const promises = [];
const fail = [];
const success = [];
for (const dir of exampleDirs.map(resolveExample)) {
for (const name of fs.readdirSync(dir)) {
const ex = resolveExample(path.join(dir, name));
if (name !== "node_modules" && fs.lstatSync(ex).isDirectory()) {
promises.push(
buildExample(ex, true)
.then(() => success.push(ex))
.catch(() => fail.push(ex))
);
}
}
}
console.log(`Building ${promises.length} example(s)...`);
await Promise.all(promises);
console.log(`\nBuilt ${success.length} example(s):`);
for (const ex of success) {
console.log(` - ${ex}`);
}
if (fail.length > 0) {
console.log(`Failed to build ${fail.length} example(s):`);
for (const ex of fail) {
console.log(` - ${ex}`);
}
}
});
program.parse();