From 448efd0eaa6f97af0889bf47efc543a1ea2f8d7e Mon Sep 17 00:00:00 2001 From: Daijiro Wachi Date: Sat, 21 Feb 2015 23:19:10 +0900 Subject: ls: allow filtering by --dev / --prod[uction] --- doc/cli/npm-ls.md | 14 +++++++ lib/ls.js | 16 ++++++++ test/tap/ls-env.js | 77 +++++++++++++++++++++++++++++++++++ test/tap/shrinkwrap-dev-dependency.js | 2 +- 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 test/tap/ls-env.js diff --git a/doc/cli/npm-ls.md b/doc/cli/npm-ls.md index 0f0d79489..318cdd8fd 100644 --- a/doc/cli/npm-ls.md +++ b/doc/cli/npm-ls.md @@ -67,6 +67,20 @@ project. Max display depth of the dependency tree. +### prod / production + +* Type: Boolean +* Default: false + +Display only the dependency tree for packages in `dependencies`. + +### dev + +* Type: Boolean +* Default: false + +Display only the dependency tree for packages in `devDependencies`. + ## SEE ALSO * npm-config(1) diff --git a/lib/ls.js b/lib/ls.js index eee4f2772..05166ee6a 100644 --- a/lib/ls.js +++ b/lib/ls.js @@ -40,6 +40,7 @@ function ls (args, silent, cb) { var opt = { depth: depth, log: log.warn, dev: true } readInstalled(dir, opt, function (er, data) { pruneNestedExtraneous(data) + filterByEnv(data) var bfs = bfsify(data, args) , lite = getLite(bfs) @@ -88,6 +89,21 @@ function pruneNestedExtraneous (data, visited) { } } +function filterByEnv (data) { + var dev = npm.config.get("dev") + var production = npm.config.get("production") + if (dev === production) return + var dependencies = {} + var devDependencies = data.devDependencies || [] + Object.keys(data.dependencies).forEach(function (name) { + var keys = Object.keys(devDependencies) + if (production && keys.indexOf(name) !== -1) return + if (dev && keys.indexOf(name) === -1) return + dependencies[name] = data.dependencies[name] + }) + data.dependencies = dependencies +} + function alphasort (a, b) { a = a.toLowerCase() b = b.toLowerCase() diff --git a/test/tap/ls-env.js b/test/tap/ls-env.js new file mode 100644 index 000000000..5ff0618f4 --- /dev/null +++ b/test/tap/ls-env.js @@ -0,0 +1,77 @@ +var common = require('../common-tap') +var test = require('tap').test +var path = require('path') +var rimraf = require('rimraf') +var osenv = require('osenv') +var mkdirp = require('mkdirp') +var pkg = path.resolve(__dirname, 'ls-depth') +var mr = require('npm-registry-mock') +var opts = {cwd: pkg} + +function cleanup () { + process.chdir(osenv.tmpdir()) + rimraf.sync(pkg + '/cache') + rimraf.sync(pkg + '/tmp') + rimraf.sync(pkg + '/node_modules') +} + +test('setup', function (t) { + cleanup() + mkdirp.sync(pkg + '/cache') + mkdirp.sync(pkg + '/tmp') + mr({port: common.port}, function (er, s) { + common.npm( + [ + 'install', + '--registry', common.registry + ], + opts, + function (er, c) { + t.ifError(er, 'install ran without issue') + t.equal(c, 0) + s.close() + t.end() + } + ) + }) +}) + +test('npm ls --dev', function (t) { + common.npm(['ls', '--dev'], opts, function (er, code, stdout) { + t.ifError(er, 'ls --dev ran without issue') + t.equal(code, 0) + t.has(stdout, /(empty)/, 'output contains (empty)') + t.end() + }) +}) + +test('npm ls --production', function (t) { + common.npm(['ls', '--production'], opts, function (er, code, stdout) { + t.ifError(er, 'ls --production ran without issue') + t.notOk(code, 'npm exited ok') + t.has( + stdout, + /test-package-with-one-dep@0\.0\.0/, + 'output contains test-package-with-one-dep@0.0.0' + ) + t.end() + }) +}) + +test('npm ls --prod', function (t) { + common.npm(['ls', '--prod'], opts, function (er, code, stdout) { + t.ifError(er, 'ls --prod ran without issue') + t.notOk(code, 'npm exited ok') + t.has( + stdout, + /test-package-with-one-dep@0\.0\.0/, + 'output contains test-package-with-one-dep@0.0.0' + ) + t.end() + }) +}) + +test('cleanup', function (t) { + cleanup() + t.end() +}) diff --git a/test/tap/shrinkwrap-dev-dependency.js b/test/tap/shrinkwrap-dev-dependency.js index 379a0d908..14eebf19f 100644 --- a/test/tap/shrinkwrap-dev-dependency.js +++ b/test/tap/shrinkwrap-dev-dependency.js @@ -14,7 +14,7 @@ test("shrinkwrap doesn't strip out the dependency", function (t) { t.plan(1) mr({port : common.port}, function (er, s) { - setup({ production: true }, function (err) { + setup({}, function (err) { if (err) return t.fail(err) npm.install(".", function (err) { -- cgit v1.2.3