From 8d7a3648a396653d0a89feeafc6d9175ee3ac736 Mon Sep 17 00:00:00 2001 From: dchamb Date: Thu, 27 Oct 2016 15:19:24 +0100 Subject: [PATCH] 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. --- package.json | 3 ++- recursive-install.js | 45 ++++--------------------------------- recursive-test.js | 19 ++++++++++++++++ recursive.js | 53 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 42 deletions(-) create mode 100755 recursive-test.js create mode 100644 recursive.js diff --git a/package.json b/package.json index aaf8257..64d999b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/recursive-install.js b/recursive-install.js index d349f22..c665fa3 100755 --- a/recursive-install.js +++ b/recursive-install.js @@ -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) @@ -48,9 +17,3 @@ if (require.main === module) { process.exit(exitCode) } - -module.exports = { - getPackageJsonLocations: getPackageJsonLocations, - npmInstall: npmInstall, - filterRoot: filterRoot -}; diff --git a/recursive-test.js b/recursive-test.js new file mode 100755 index 0000000..6e0aefc --- /dev/null +++ b/recursive-test.js @@ -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) +} diff --git a/recursive.js b/recursive.js new file mode 100644 index 0000000..60478d2 --- /dev/null +++ b/recursive.js @@ -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 +};