Skip to content

Commit 3ec5917

Browse files
author
dchamb
committed
Add npm-recursive-test command
This command complements the already existing `npm-recursive-install` command. The implementation could be made a lot more DRY, but this is enough to test whether it might be suitable for inclusion upstream.
1 parent 79883d0 commit 3ec5917

5 files changed

Lines changed: 79 additions & 45 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"test": "./node_modules/.bin/mocha"
1313
},
1414
"bin": {
15-
"npm-recursive-install": "./recursive-install.js"
15+
"npm-recursive-install": "./recursive-install.js",
16+
"npm-recursive-test": "./recursive-test.js"
1617
},
1718
"repository": {
1819
"type": "git",

recursive-install.js

100755100644
Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,9 @@
11
#!/usr/bin/env node
2-
3-
var path = require('path')
4-
var shell = require('shelljs')
52
var argv = require('yargs').argv
6-
7-
function noop (x) { return x }
8-
9-
function getPackageJsonLocations (dirname) {
10-
return shell.find(dirname)
11-
.filter(function (fname) {
12-
return !(fname.indexOf('node_modules') > -1 || fname[0] === '.') &&
13-
path.basename(fname) === 'package.json'
14-
})
15-
.map(function (fname) {
16-
return path.dirname(fname)
17-
})
18-
}
19-
20-
function npmInstall (dir) {
21-
shell.cd(dir)
22-
console.log('Installing ' + dir + '/package.json...')
23-
var result = shell.exec('npm install')
24-
console.log('')
25-
26-
return {
27-
dirname: dir,
28-
exitCode: result.code
29-
}
30-
}
31-
32-
function filterRoot (dir) {
33-
if (path.normalize(dir) === path.normalize(process.cwd())) {
34-
console.log('Skipping root package.json...')
35-
return false
36-
} else {
37-
return true
38-
}
39-
}
3+
var recursive = require('./recursive');
4+
var getPackageJsonLocations = recursive.getPackageJsonLocations;
5+
var filterRoot = recursive.filterRoot;
6+
var npmInstall = recursive.npmInstall;
407

418
if (require.main === module) {
429
var exitCode = getPackageJsonLocations(process.cwd())
@@ -48,9 +15,3 @@ if (require.main === module) {
4815

4916
process.exit(exitCode)
5017
}
51-
52-
module.exports = {
53-
getPackageJsonLocations: getPackageJsonLocations,
54-
npmInstall: npmInstall,
55-
filterRoot: filterRoot
56-
};

recursive-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env node
2+
var argv = require('yargs').argv
3+
var recursive = require('./recursive');
4+
var getPackageJsonLocations = recursive.getPackageJsonLocations;
5+
var filterRoot = recursive.filterRoot;
6+
var npmTest = recursive.npmTest;
7+
8+
if (require.main === module) {
9+
var exitCode = getPackageJsonLocations(process.cwd())
10+
.filter(argv.skipRoot ? filterRoot : noop)
11+
.map(npmTest)
12+
.reduce(function (code, result) {
13+
return result.exitCode > code ? result.exitCode : code
14+
}, 0)
15+
16+
process.exit(exitCode)
17+
}

recursive.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var path = require('path')
2+
var shell = require('shelljs')
3+
4+
function noop (x) { return x }
5+
6+
function getPackageJsonLocations (dirname) {
7+
return shell.find(dirname)
8+
.filter(function (fname) {
9+
return !(fname.indexOf('node_modules') > -1 || fname[0] === '.') &&
10+
path.basename(fname) === 'package.json'
11+
})
12+
.map(function (fname) {
13+
return path.dirname(fname)
14+
})
15+
}
16+
17+
function npmInstall (dir) {
18+
shell.cd(dir)
19+
console.log('Installing ' + dir + '/package.json...')
20+
var result = shell.exec('npm install')
21+
console.log('')
22+
23+
return {
24+
dirname: dir,
25+
exitCode: result.code
26+
}
27+
}
28+
29+
function npmTest (dir) {
30+
shell.cd(dir)
31+
console.log('Running tests for ' + dir + '/package.json...')
32+
var result = shell.exec('npm test')
33+
console.log('')
34+
35+
return {
36+
dirname: dir,
37+
exitCode: result.code
38+
}
39+
}
40+
41+
function filterRoot (dir) {
42+
if (path.normalize(dir) === path.normalize(process.cwd())) {
43+
console.log('Skipping root package.json...')
44+
return false
45+
} else {
46+
return true
47+
}
48+
}
49+
50+
module.exports = {
51+
getPackageJsonLocations: getPackageJsonLocations,
52+
npmInstall: npmInstall,
53+
npmTest: npmTest,
54+
filterRoot: filterRoot
55+
};

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var path = require('path')
22
var shell = require('shelljs')
33
var assert = require('assert')
44

5-
var script = path.join(__dirname, '../recursive-install.js')
5+
var script = path.join(__dirname, '../recursive.js')
66

77
describe('recursive install', function () {
88
var cwd = path.join(shell.tempdir(), 'recursive-install')

0 commit comments

Comments
 (0)