forked from ds300/postinstall-postinstall
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
34 lines (29 loc) · 1 KB
/
run.js
File metadata and controls
34 lines (29 loc) · 1 KB
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
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";
try {
exec(`${pkgManager} run postinstall`, { cwd: appPath });
} catch (error) {
console.log("errr status is: ", error.status);
console.log("\nerrr message is: ", error.message);
console.log("\nstdout is: ", error.stdout.toString());
console.log("\nstderr is: ", error.stderr.toString());
console.log("\n\n\n");
}
}
}
function shouldUseYarn() {
try {
exec("yarnpkg --version", { stdio: "ignore" });
return true;
} catch (e) {
return false;
}
}