forked from ds300/postinstall-postinstall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
26 lines (21 loc) · 731 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
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, stdio: 'ignore'})
}
}
function shouldUseYarn() {
try {
exec('yarnpkg --version', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
}