forked from bony2023/react-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-peers.js
50 lines (39 loc) · 1.28 KB
/
install-peers.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
const fs = require('fs/promises');
const { spawn } = require('child_process');
async function main() {
const package = JSON.parse((await fs.readFile('package.json')).toString());
if (!package.peerDependencies) return;
const args = Object.keys(package.peerDependencies).map((key) => `${key}@${package.peerDependencies[key]}`);
if (!args.length) return;
args.unshift('add', '--peer', '--pure-lockfile');
try {
const yarn = process.platform === 'win32' ? 'yarn.cmd' : 'yarn';
await execute(yarn, args);
} catch (error) {
console.error(error.message || error);
process.exit(1);
}
}
function execute(processName, args, options) {
console.info(`${processName} ${args.join(' ')}`);
return new Promise((res, rej) => {
const child = spawn(processName, args, options);
child.on('error', (error) => {
rej(new Error(`Could not run ${processName} ${args.join(' ')}: ${error}.`));
});
child.stdout.on('data', (data) => {
process.stdout.write(data);
});
child.stderr.on('data', (data) => {
process.stderr.write(data);
});
child.on('close', (code) => {
if (code === 0) {
res();
} else {
rej(new Error(`process ${processName} ${args.join(' ')} exited with code ${code}.`));
}
});
});
}
main();