-
Notifications
You must be signed in to change notification settings - Fork 28
/
recursive-install.js
executable file
·60 lines (52 loc) · 1.44 KB
/
recursive-install.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
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env node
var path = require('path')
var shell = require('shelljs')
var execSync = require('child_process').execSync
var argv = require('yargs').argv
function noop (x) { return x }
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 npmInstall (dir) {
var exitCode = 0;
try {
if(argv.production) {
console.log('Installing ' + dir + '/package.json with --production option')
execSync('npm install --production', { cwd: dir})
} else {
console.log('Installing ' + dir + '/package.json')
execSync('npm install', { cwd: dir})
}
console.log('')
} catch (err) {
exitCode = err.status
}
return {
dirname: dir,
exitCode: exitCode
}
}
function filterRoot (dir) {
if (path.normalize(dir) === path.normalize(process.cwd())) {
console.log('Skipping root package.json')
return false
} else {
return true
}
}
if (require.main === module) {
var exitCode = getPackageJsonLocations(argv.rootDir ? argv.rootDir : process.cwd())
.filter(argv.skipRoot ? filterRoot : noop)
.map(npmInstall)
.reduce(function (code, result) {
return result.exitCode > code ? result.exitCode : code
}, 0)
process.exit(exitCode)
}