Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add npm-recursive-test command #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"test": "./node_modules/.bin/mocha"
},
"bin": {
"npm-recursive-install": "./recursive-install.js"
"npm-recursive-install": "./recursive-install.js",
"npm-recursive-test": "./recursive-test.js"
},
"repository": {
"type": "git",
Expand Down
45 changes: 4 additions & 41 deletions recursive-install.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,12 @@
#!/usr/bin/env node

var path = require('path')
var shell = require('shelljs')
var argv = require('yargs').argv
var recursive = require('./recursive');
var getPackageJsonLocations = recursive.getPackageJsonLocations;
var filterRoot = recursive.filterRoot;
var npmInstall = recursive.npmInstall;

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) {
shell.cd(dir)
console.log('Installing ' + dir + '/package.json...')
var result = shell.exec('npm install')
console.log('')

return {
dirname: dir,
exitCode: result.code
}
}

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(process.cwd())
.filter(argv.skipRoot ? filterRoot : noop)
Expand All @@ -48,9 +17,3 @@ if (require.main === module) {

process.exit(exitCode)
}

module.exports = {
getPackageJsonLocations: getPackageJsonLocations,
npmInstall: npmInstall,
filterRoot: filterRoot
};
19 changes: 19 additions & 0 deletions recursive-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env node
var argv = require('yargs').argv
var recursive = require('./recursive');
var getPackageJsonLocations = recursive.getPackageJsonLocations;
var filterRoot = recursive.filterRoot;
var npmTest = recursive.npmTest;

function noop (x) { return x }

if (require.main === module) {
var exitCode = getPackageJsonLocations(process.cwd())
.filter(argv.skipRoot ? filterRoot : noop)
.map(npmTest)
.reduce(function (code, result) {
return result.exitCode > code ? result.exitCode : code
}, 0)

process.exit(exitCode)
}
53 changes: 53 additions & 0 deletions recursive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var path = require('path')
var shell = require('shelljs')

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) {
shell.cd(dir)
console.log('Installing ' + dir + '/package.json...')
var result = shell.exec('npm install')
console.log('')

return {
dirname: dir,
exitCode: result.code
}
}

function npmTest (dir) {
shell.cd(dir)
console.log('Running tests for ' + dir + '/package.json...')
var result = shell.exec('npm test')
console.log('')

return {
dirname: dir,
exitCode: result.code
}
}

function filterRoot (dir) {
if (path.normalize(dir) === path.normalize(process.cwd())) {
console.log('Skipping root package.json...')
return false
} else {
return true
}
}

module.exports = {
getPackageJsonLocations: getPackageJsonLocations,
npmInstall: npmInstall,
npmTest: npmTest,
filterRoot: filterRoot
};