forked from acacode/swagger-typescript-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scriptsRunner.js
60 lines (49 loc) · 1.38 KB
/
scriptsRunner.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
const packageJson = require("./package.json");
const { exec, spawn } = require("child_process");
const commands = process.argv.slice(2);
const packageScripts = Object.keys(packageJson.scripts);
const execute = (scriptName) =>
new Promise((resolve, reject) => {
console.log(`npm run ${scriptName}`);
const spawned = spawn(/^win/.test(process.platform) ? "npm.cmd" : "npm", ["run", scriptName]);
spawned.stdout.on("data", (data) => {
process.stdout.write(data);
});
spawned.stderr.on("data", (data) => {
process.stderr.write(data);
});
spawned.on("error", (error) => {
console.error(error);
});
spawned.on("message", (message) => {
console.log(message);
});
spawned.on("close", (code) => {
if (code) {
reject(code);
} else {
resolve(code);
}
});
});
const run = async () => {
for (const command of commands) {
for (const scriptName of packageScripts) {
try {
if (scriptName === command) {
await execute(scriptName);
}
if (command.includes("*")) {
const commandPart = command.replace("*", "");
// TODO: refactor
if (scriptName.startsWith(commandPart) || scriptName.endsWith(commandPart)) {
await execute(scriptName);
}
}
} catch (e) {
process.exit(1);
}
}
}
};
run();