forked from acacode/swagger-typescript-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts_runner.js
66 lines (55 loc) · 1.44 KB
/
scripts_runner.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
const packageJson = require('./package.json');
const { 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 await (const command of commands) {
for await (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();