forked from emgeee/recursive-install
-
Notifications
You must be signed in to change notification settings - Fork 1
/
recursive-npm.js
45 lines (39 loc) · 1020 Bytes
/
recursive-npm.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
#!/usr/bin/env node
const path = require("path");
const shell = require("shelljs");
const execSync = require("child_process").execSync;
function getPackageJsonLocations(dirname) {
return shell
.find(dirname)
.filter(function (fname) {
return (
!(fname.indexOf("node_modules") > -1 || fname[0] === ".") &&
path.basename(fname) === "package.json"
);
})
.map(function (fname) {
return path.dirname(fname);
});
}
function runNpmCommand(dir) {
let exitCode = 0;
let cmd = `npm ${process.argv.slice(2).join(" ")}`;
try {
console.log(`Running ${cmd} in ${dir}`);
execSync(cmd, { cwd: dir });
} catch (err) {
exitCode = err.status;
}
return {
dirname: dir,
exitCode: exitCode,
};
}
if (require.main === module) {
var exitCode = getPackageJsonLocations(process.cwd())
.map(runNpmCommand)
.reduce(function (code, result) {
return result.exitCode > code ? result.exitCode : code;
}, 0);
process.exit(exitCode);
}