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

Commit

Permalink
module-name: Factor out module name reading
Browse files Browse the repository at this point in the history
Bring consistent guarding against null/undefined and consistent
business logic.

PR-URL: //github.com//pull/9890
Fixes: #9766
  • Loading branch information
iarna committed Oct 8, 2015
1 parent 2289234 commit b78fec9
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 55 deletions.
7 changes: 4 additions & 3 deletions lib/dedupe.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var loadExtraneous = require('./install/deps.js').loadExtraneous
var filterInvalidActions = require('./install/filter-invalid-actions.js')
var recalculateMetadata = require('./install/deps.js').recalculateMetadata
var sortActions = require('./install/diff-trees.js').sortActions
var moduleName = require('./utils/module-name.js')

module.exports = dedupe
module.exports.Deduper = Deduper
Expand Down Expand Up @@ -48,7 +49,7 @@ Deduper.prototype.normalizeTree = function (log, cb) {
if (npm.config.get('global')) {
var args = this.args
this.currentTree.children = this.currentTree.children.filter(function (child) {
return args.filter(function (arg) { return arg === child.package.name }).length
return args.filter(function (arg) { return arg === moduleName(child) }).length
})
}
Installer.prototype.normalizeTree.call(this, log, cb)
Expand Down Expand Up @@ -97,7 +98,7 @@ function move (node, hoistTo, diff) {
node.parent.children = without(node.parent.children, node)
hoistTo.children.push(node)
node.fromPath = node.path
node.path = path.resolve(hoistTo.path, 'node_modules', node.package.name)
node.path = path.resolve(hoistTo.path, 'node_modules', moduleName(node))
node.parent = hoistTo
if (!diff.filter(function (action) { return action[0] === 'move' && action[1] === node }).length) {
diff.push(['move', node])
Expand Down Expand Up @@ -135,7 +136,7 @@ function hoistChildren_ (tree, diff, seen, next) {
seen[tree.path] = true
asyncMap(tree.children, function (child, done) {
if (!tree.parent) return hoistChildren_(child, diff, seen, done)
var better = findRequirement(tree.parent, child.package.name, child.package._requested || npa(child.package.name + '@' + child.package.version))
var better = findRequirement(tree.parent, moduleName(child), child.package._requested || npa(child.package.name + '@' + child.package.version))
if (better) {
return chain([
[remove, child, diff],
Expand Down
3 changes: 2 additions & 1 deletion lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ var doReverseSerialActions = require('./install/actions.js').doReverseSerial
var doParallelActions = require('./install/actions.js').doParallel
var doOneAction = require('./install/actions.js').doOne
var getPackageId = require('./install/get-package-id.js')
var moduleName = require('./utils/module-name.js')

function unlockCB (lockPath, name, cb) {
validate('SSF', arguments)
Expand Down Expand Up @@ -428,7 +429,7 @@ Installer.prototype.computeLinked = function (cb) {
}

function isLinkable (pkg, cb) {
var globalPackage = path.resolve(npm.globalPrefix, 'lib', 'node_modules', pkg.package.name)
var globalPackage = path.resolve(npm.globalPrefix, 'lib', 'node_modules', moduleName(pkg))
var globalPackageJson = path.resolve(globalPackage, 'package.json')
fs.stat(globalPackage, function (er) {
if (er) return cb(true, true)
Expand Down
3 changes: 2 additions & 1 deletion lib/install/action/fetch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'
// var cache = require('../../cache.js')
// var moduleName = require('../../utils/module-name.js')

module.exports = function (top, buildpath, pkg, log, next) {
next()
Expand All @@ -8,7 +9,7 @@ module.exports = function (top, buildpath, pkg, log, next) {
// is progressively seeming to be likely for the indefinite future.
// ALSO fails for local deps specified with relative URLs outside of the top level.
var name = pkg.package.name
var name = moduleName(pkg)
var version
switch (pkg.package._requested.type) {
case 'version':
Expand Down
5 changes: 3 additions & 2 deletions lib/install/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var log = require('npmlog')
var andFinishTracker = require('./and-finish-tracker.js')
var andAddParentToErrors = require('./and-add-parent-to-errors.js')
var failedDependency = require('./deps.js').failedDependency
var moduleName = require('../utils/module-name.js')

var actions = {}

Expand Down Expand Up @@ -81,9 +82,9 @@ function prepareAction (staging, log) {
var cmd = action[0]
var pkg = action[1]
if (!actions[cmd]) throw new Error('Unknown decomposed command "' + cmd + '" (is it new?)')
var buildpath = uniqueFilename(staging, pkg.package.name, pkg.realpath)
var buildpath = uniqueFilename(staging, moduleName(pkg), pkg.realpath)
var top = path.resolve(staging, '../..')
return [actions[cmd], top, buildpath, pkg, log.newGroup(cmd + ':' + pkg.package.name)]
return [actions[cmd], top, buildpath, pkg, log.newGroup(cmd + ':' + moduleName(pkg))]
}
}

Expand Down
39 changes: 23 additions & 16 deletions lib/install/deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ var resetMetadata = require('./node.js').reset
var andIgnoreErrors = require('./and-ignore-errors.js')
var isInstallable = require('./validate-args.js').isInstallable
var getPackageId = require('./get-package-id.js')
var moduleName = require('../utils/module-name.js')

// The export functions in this module mutate a dependency tree, adding
// items to them.

function isDep (tree, child) {
if (child.fromShrinkwrap) return true
var requested = isProdDep(tree, child.package.name)
var name = moduleName(child)
var requested = isProdDep(tree, name)
var matches
if (requested) matches = doesChildVersionMatch(child, requested)
if (matches) return matches
requested = isDevDep(tree, child.package.name)
requested = isDevDep(tree, name)
if (!requested) return
return doesChildVersionMatch(child, requested)
}
Expand Down Expand Up @@ -122,7 +124,7 @@ function recalculateMetadata (tree, log, seen, next) {

function addRequiredDep (tree, child) {
if (!isDep(tree, child)) return false
var name = isProdDep(tree, child.package.name) ? flatNameFromTree(tree) : '#DEV:' + flatNameFromTree(tree)
var name = isProdDep(tree, moduleName(child)) ? flatNameFromTree(tree) : '#DEV:' + flatNameFromTree(tree)
child.package._requiredBy = union(child.package._requiredBy || [], [name])
child.requiredBy = union(child.requiredBy || [], [tree])
tree.requires = union(tree.requires || [], [child])
Expand Down Expand Up @@ -174,20 +176,22 @@ exports.loadRequestedDeps = function (args, tree, saveToDependencies, log, next)
validate('AOOF', [args, tree, log, next])
asyncMap(args, function (pkg, done) {
var depLoaded = andAddParentToErrors(tree, done)
var pkgName = moduleName(pkg)
tree.children = tree.children.filter(function (child) {
return child.package.name !== pkg.name
return moduleName(child) !== pkgName
})
resolveWithNewModule(pkg, tree, log.newGroup('loadRequestedDeps'), iferr(depLoaded, function (child, tracker) {
validate('OO', arguments)
if (npm.config.get('global')) {
child.isGlobal = true
}
var childName = moduleName(child)
if (saveToDependencies) {
tree.package[saveToDependencies][child.package.name] =
tree.package[saveToDependencies][childName] =
child.package._requested.rawSpec || child.package._requested.spec
}
if (saveToDependencies && saveToDependencies !== 'devDependencies') {
tree.package.dependencies[child.package.name] =
tree.package.dependencies[childName] =
child.package._requested.rawSpec || child.package._requested.spec
}
child.directlyRequested = true
Expand All @@ -210,14 +214,15 @@ exports.loadRequestedDeps = function (args, tree, saveToDependencies, log, next)
exports.removeDeps = function (args, tree, saveToDependencies, log, next) {
validate('AOOF', [args, tree, log, next])
args.forEach(function (pkg) {
var pkgName = moduleName(pkg)
if (saveToDependencies) {
var toRemove = tree.children.filter(function (child) { return child.package.name === pkg.name })
var toRemove = tree.children.filter(function (child) { return moduleName(child) === pkgName })
tree.removed = union(tree.removed || [], toRemove)
toRemove.forEach(function (parent) {
parent.save = saveToDependencies
})
}
tree.children = tree.children.filter(function (child) { return child.package.name !== pkg.name })
tree.children = tree.children.filter(function (child) { return moduleName(child) !== pkgName })
})
log.finish()
next()
Expand All @@ -238,7 +243,7 @@ function andForEachChild (load, next) {
cmds.push([load, children[ii], logs[ii]])
}
var sortedCmds = cmds.sort(function installOrder (aa, bb) {
return aa[1].package.name.localeCompare(bb[1].package.name)
return moduleName(aa[1]).localeCompare(moduleName(bb[1]))
})
chain(sortedCmds, next)
}
Expand All @@ -256,10 +261,10 @@ var failedDependency = exports.failedDependency = function (tree, name_pkg) {
name = name_pkg
} else {
pkg = name_pkg
name = pkg.name || pkg.package.name
name = moduleName(pkg)
}

tree.children = tree.children.filter(function (child) { return child.package.name !== name })
tree.children = tree.children.filter(function (child) { return moduleName(child) !== name })

if (isDepOptional(tree, name)) {
return false
Expand All @@ -285,7 +290,7 @@ function andHandleOptionalErrors (log, tree, name, done) {
if (!er) return done(er, child, childLog)
var isFatal = failedDependency(tree, name)
if (er && !isFatal) {
tree.children = tree.children.filter(function (child) { return child.package.name !== name })
tree.children = tree.children.filter(function (child) { return moduleName(child) !== name })
log.warn('install', "Couldn't install optional dependency:", er.message)
log.verbose('install', er.stack)
return done()
Expand Down Expand Up @@ -382,7 +387,7 @@ var updatePhantomChildren = exports.updatePhantomChildren = function (current, c
while (current && current !== child.parent) {
// FIXME: phantomChildren doesn't actually belong in the package.json
if (!current.package._phantomChildren) current.package._phantomChildren = {}
current.package._phantomChildren[child.package.name] = child.package.version
current.package._phantomChildren[moduleName(child)] = child.package.version
current = current.parent
}
}
Expand Down Expand Up @@ -411,6 +416,8 @@ function resolveWithNewModule (pkg, tree, log, next) {
}))
}

var pkgName = moduleName(pkg)

if (!pkg._from) {
pkg._from = pkg._requested.name + '@' + pkg._requested.spec
}
Expand All @@ -426,7 +433,7 @@ function resolveWithNewModule (pkg, tree, log, next) {
isLink: tree.isLink
})

parent.children = parent.children.filter(function (pkg) { return pkg.package.name !== child.package.name })
parent.children = parent.children.filter(function (pkg) { return pkgName !== moduleName(pkg) })
parent.children.push(child)
addRequiredDep(tree, child)
pkg._location = flatNameFromTree(child)
Expand Down Expand Up @@ -474,7 +481,7 @@ function validateAllPeerDeps (tree, onInvalid, seen) {
var findRequirement = exports.findRequirement = function (tree, name, requested) {
validate('OSO', arguments)
var nameMatch = function (child) {
return child.package.name === name && child.parent
return moduleName(child) === name && child.parent
}
var versionMatch = function (child) {
return doesChildVersionMatch(child, requested)
Expand Down Expand Up @@ -503,7 +510,7 @@ var findRequirement = exports.findRequirement = function (tree, name, requested)
var earliestInstallable = exports.earliestInstallable = function (requiredBy, tree, pkg) {
validate('OOO', arguments)
var nameMatch = function (child) {
return child.package.name === pkg.name
return moduleName(child) === pkg.name
}

if (tree.children.some(nameMatch)) return null
Expand Down
3 changes: 2 additions & 1 deletion lib/install/flatten-tree.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'
var validate = require('aproba')
var moduleName = require('../utils/module-name.js')

module.exports = function (tree) {
validate('O', arguments)
Expand All @@ -25,5 +26,5 @@ module.exports = function (tree) {

var flatName = module.exports.flatName = function (path, child) {
validate('SO', arguments)
return path + (child.package.name || 'TOP')
return path + (moduleName(child) || 'TOP')
}
12 changes: 4 additions & 8 deletions lib/install/get-package-id.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
'use strict'
var path = require('path')
var moduleName = require('../utils/module-name.js')

module.exports = function (tree) {
var pkg = tree.package || tree
// FIXME: Excluding the '@' here is cleaning up after the mess that
// read-package-json makes. =(
if (pkg._id && pkg._id !== '@') return pkg._id
var name = pkg.name || (tree && tree.logical && path.basename(tree.logical))
if (name && pkg.version) {
var name = moduleName(tree)
if (pkg.version) {
return name + '@' + pkg.version
} else if (tree) {
return tree.path
} else if (name) {
return name
} else {
return ''
return name
}
}
5 changes: 3 additions & 2 deletions lib/install/inflate-bundled.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict'
var path = require('path')
var validate = require('aproba')
var moduleName = require('../utils/module-name.js')

module.exports = function inflateBundled (parent, children) {
validate('OA', arguments)
children.forEach(function (child) {
child.fromBundle = true
child.parent = parent
child.path = path.join(parent.path, child.package.name)
child.realpath = path.resolve(parent.realpath, child.package.name)
child.path = path.join(parent.path, moduleName(child))
child.realpath = path.resolve(parent.realpath, moduleName(child))
child.isLink = child.isLink || parent.isLink || parent.target
inflateBundled(child, child.children)
})
Expand Down
3 changes: 2 additions & 1 deletion lib/install/inflate-shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ var addBundled = require('../fetch-package-metadata.js').addBundled
var inflateBundled = require('./inflate-bundled.js')
var npm = require('../npm.js')
var createChild = require('./node.js').create
var moduleName = require('../utils/module-name.js')

var inflateShrinkwrap = module.exports = function (tree, swdeps, finishInflating) {
validate('OOF', arguments)
if (!npm.config.get('shrinkwrap')) return finishInflating()
var onDisk = {}
tree.children.forEach(function (child) { onDisk[child.package.name] = child })
tree.children.forEach(function (child) { onDisk[moduleName(child)] = child })
tree.children = []
asyncMap(Object.keys(swdeps), function (name, next) {
var sw = swdeps[name]
Expand Down
7 changes: 4 additions & 3 deletions lib/install/mutate-into-logical-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var flattenTree = require('./flatten-tree.js')
var isExtraneous = require('./is-extraneous.js')
var validateAllPeerDeps = require('./deps.js').validateAllPeerDeps
var getPackageId = require('./get-package-id.js')
var moduleName = require('../utils/module-name.js')

var mutateIntoLogicalTree = module.exports = function (tree) {
validate('O', arguments)
Expand All @@ -29,8 +30,8 @@ var mutateIntoLogicalTree = module.exports = function (tree) {
var requiredByNames = requiredBy.filter(function (parentFlatname) {
var parentNode = getNode(parentFlatname)
if (!parentNode) return false
return parentNode.package.dependencies[node.package.name] ||
(parentNode.package.devDependencies && parentNode.package.devDependencies[node.package.name])
return parentNode.package.dependencies[moduleName(node)] ||
(parentNode.package.devDependencies && parentNode.package.devDependencies[moduleName(node)])
})
requiredBy = requiredByNames.map(getNode)

Expand Down Expand Up @@ -67,7 +68,7 @@ function translateTree_ (tree, seen) {
pkg._dependencies = pkg.dependencies
pkg.dependencies = {}
tree.children.forEach(function (child) {
pkg.dependencies[child.package.name] = translateTree_(child, seen)
pkg.dependencies[moduleName(child)] = translateTree_(child, seen)
})
Object.keys(tree.missingDeps).forEach(function (name) {
if (pkg.dependencies[name]) {
Expand Down
5 changes: 3 additions & 2 deletions lib/install/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var without = require('lodash.without')
var npm = require('../npm.js')
var deepSortObject = require('../utils/deep-sort-object.js')
var parseJSON = require('../utils/parse-json.js')
var moduleName = require('../utils/module-name.js')

// if the -S|--save option is specified, then write installed packages
// as dependencies to a package.json file.
Expand Down Expand Up @@ -171,7 +172,7 @@ function getThingsToSave (tree) {
return child.save
}).map(function (child) {
return {
name: child.package.name,
name: moduleName(child),
spec: computeVersionSpec(child),
save: child.save
}
Expand All @@ -184,7 +185,7 @@ function getThingsToRemove (args, tree) {
if (!tree.removed) return []
var toRemove = tree.removed.map(function (child) {
return {
name: child.package.name,
name: moduleName(child),
save: child.save
}
})
Expand Down
2 changes: 1 addition & 1 deletion lib/install/validate-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var isInstallable = module.exports.isInstallable = function (pkg, next) {
}

function checkSelf (idealTree, pkg, force, next) {
if (idealTree.package.name !== pkg.name) return next()
if (idealTree.package && idealTree.package.name !== pkg.name) return next()
if (force) {
var warn = new Error("Wouldn't install " + pkg.name + ' as a dependency of itself, but being forced')
warn.code = 'ENOSELF'
Expand Down
Loading

0 comments on commit b78fec9

Please sign in to comment.