Skip to content

Commit 8d7a364

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 8d7a364

4 files changed

Lines changed: 78 additions & 42 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

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,12 @@
11
#!/usr/bin/env node
2-
3-
var path = require('path')
4-
var shell = require('shelljs')
52
var argv = require('yargs').argv
3+
var recursive = require('./recursive');
4+
var getPackageJsonLocations = recursive.getPackageJsonLocations;
5+
var filterRoot = recursive.filterRoot;
6+
var npmInstall = recursive.npmInstall;
67

78
function noop (x) { return x }
89

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-
}
40-
4110
if (require.main === module) {
4211
var exitCode = getPackageJsonLocations(process.cwd())
4312
.filter(argv.skipRoot ? filterRoot : noop)
@@ -48,9 +17,3 @@ if (require.main === module) {
4817

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

recursive-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
function noop (x) { return x }
9+
10+
if (require.main === module) {
11+
var exitCode = getPackageJsonLocations(process.cwd())
12+
.filter(argv.skipRoot ? filterRoot : noop)
13+
.map(npmTest)
14+
.reduce(function (code, result) {
15+
return result.exitCode > code ? result.exitCode : code
16+
}, 0)
17+
18+
process.exit(exitCode)
19+
}

recursive.js

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

0 commit comments

Comments
 (0)