forked from ds300/postinstall-postinstall
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.js
35 lines (30 loc) · 933 Bytes
/
run.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
const fs = require('fs')
const cwd = require('process').cwd()
const path = require('path')
const exec = require('child_process').execSync
const appPath = path.normalize(cwd.slice(0, cwd.lastIndexOf('node_modules')))
const packageJsonPath = path.join(appPath, 'package.json')
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath))
if (packageJson.scripts && packageJson.scripts.postinstall) {
const pkgManager = shouldUseYarn() ? 'yarn' : 'npm';
exec(`${pkgManager} run postinstall`, {cwd: appPath}, (error, _stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
});
}
}
function shouldUseYarn() {
try {
exec('yarnpkg --version', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
}