Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
ls: allow filtering by --dev / --prod[uction]
Browse files Browse the repository at this point in the history
  • Loading branch information
watilde authored and othiym23 committed Feb 27, 2015
1 parent 536b2b6 commit 448efd0
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
14 changes: 14 additions & 0 deletions doc/cli/npm-ls.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions lib/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
77 changes: 77 additions & 0 deletions test/tap/ls-env.js
Original file line number Diff line number Diff line change
@@ -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 [email protected]'
)
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 [email protected]'
)
t.end()
})
})

test('cleanup', function (t) {
cleanup()
t.end()
})
2 changes: 1 addition & 1 deletion test/tap/shrinkwrap-dev-dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 448efd0

Please sign in to comment.