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

Commit

Permalink
install: Filter optional deps during diff-trees
Browse files Browse the repository at this point in the history
This allows `--no-optional` runs to include optional dependencies in the
lockfile while excluding them from installation on disk.
  • Loading branch information
iarna committed Apr 12, 2018
1 parent 471ee1c commit b3f98d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
6 changes: 0 additions & 6 deletions lib/install/deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,6 @@ function loadDeps (tree, log, next) {
if (!tree.package.dependencies) tree.package.dependencies = {}
asyncMap(Object.keys(tree.package.dependencies), function (dep, done) {
var version = tree.package.dependencies[dep]
if (tree.package.optionalDependencies &&
tree.package.optionalDependencies[dep] &&
!npm.config.get('optional')) {
return done()
}

addDependency(dep, version, tree, log.newGroup('loadDep:' + dep), andHandleOptionalErrors(log, tree, dep, done))
}, andForEachChild(loadDeps, andFinishTracker(log, next)))
}
Expand Down
27 changes: 18 additions & 9 deletions lib/install/diff-trees.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var log = require('npmlog')
var path = require('path')
var ssri = require('ssri')
var moduleName = require('../utils/module-name.js')
var isOnlyOptional = require('./is-only-optional.js')

// we don't use get-requested because we're operating on files on disk, and
// we don't want to extropolate from what _should_ be there.
Expand Down Expand Up @@ -234,18 +235,26 @@ var diffTrees = module.exports._diffTrees = function (oldTree, newTree) {
.map((flatname) => toRemove[flatname])
.forEach((pkg) => setAction(differences, 'remove', pkg))

return filterActions(differences)
}

function filterActions (differences) {
const includeOpt = npm.config.get('optional')
const includeDev = npm.config.get('dev') ||
(!/^prod(uction)?$/.test(npm.config.get('only')) && !npm.config.get('production')) ||
/^dev(elopment)?$/.test(npm.config.get('only')) ||
/^dev(elopment)?$/.test(npm.config.get('also'))
const includeProd = !/^dev(elopment)?$/.test(npm.config.get('only'))
if (!includeProd || !includeDev) {
log.silly('diff-trees', 'filtering actions:', 'includeDev', includeDev, 'includeProd', includeProd)
differences = differences.filter((diff) => {
const pkg = diff[1]
const pkgIsOnlyDev = isOnlyDev(pkg)
return (!includeProd && pkgIsOnlyDev) || (includeDev && pkgIsOnlyDev) || (includeProd && !pkgIsOnlyDev)
})
}
return differences
if (includeProd && includeDev && includeOpt) return differences

log.silly('diff-trees', 'filtering actions:', 'includeDev', includeDev, 'includeProd', includeProd, 'includeOpt', includeOpt)
return differences.filter((diff) => {
const pkg = diff[1]
const pkgIsOnlyDev = isOnlyDev(pkg)
const pkgIsOnlyOpt = isOnlyOptional(pkg)
if (!includeProd && pkgIsOnlyDev) return true
if (includeDev && pkgIsOnlyDev) return true
if (includeProd && !pkgIsOnlyDev && (includeOpt || !pkgIsOnlyOpt)) return true
return false
})
}

0 comments on commit b3f98d8

Please sign in to comment.