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

Commit

Permalink
child-path: Compute the path of a new child module in only one place
Browse files Browse the repository at this point in the history
PR-URL: //github.com//pull/9890
Fixes: #9766
  • Loading branch information
iarna committed Oct 8, 2015
1 parent 5a6e873 commit 4a97440
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/dedupe.js
Expand Up @@ -18,6 +18,7 @@ var recalculateMetadata = require('./install/deps.js').recalculateMetadata
var sortActions = require('./install/diff-trees.js').sortActions
var moduleName = require('./utils/module-name.js')
var packageId = require('./utils/package-id.js')
var childPath = require('./utils/child-path.js')

module.exports = dedupe
module.exports.Deduper = Deduper
Expand Down Expand Up @@ -99,7 +100,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', moduleName(node))
node.path = childPath(hoistTo.path, node)
node.parent = hoistTo
if (!diff.filter(function (action) { return action[0] === 'move' && action[1] === node }).length) {
diff.push(['move', node])
Expand Down
7 changes: 3 additions & 4 deletions lib/install/inflate-bundled.js
@@ -1,15 +1,14 @@
'use strict'
var path = require('path')
var validate = require('aproba')
var moduleName = require('../utils/module-name.js')
var childPath = require('../utils/child-path.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, moduleName(child))
child.realpath = path.resolve(parent.realpath, moduleName(child))
child.path = childPath(parent.path, child)
child.realpath = childPath(parent.path, child)
child.isLink = child.isLink || parent.isLink || parent.target
inflateBundled(child, child.children)
})
Expand Down
6 changes: 3 additions & 3 deletions lib/install/inflate-shrinkwrap.js
@@ -1,5 +1,4 @@
'use strict'
var path = require('path')
var url = require('url')
var asyncMap = require('slide').asyncMap
var validate = require('aproba')
Expand All @@ -11,6 +10,7 @@ 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 childPath = require('../utils/child-path.js')

var inflateShrinkwrap = module.exports = function (tree, swdeps, finishInflating) {
validate('OOF', arguments)
Expand Down Expand Up @@ -43,8 +43,8 @@ var inflateShrinkwrap = module.exports = function (tree, swdeps, finishInflating
loaded: false,
parent: tree,
fromShrinkwrap: spec,
path: path.join(tree.path, 'node_modules', pkg.name),
realpath: path.resolve(tree.realpath, 'node_modules', pkg.name),
path: childPath(tree.path, pkg),
realpath: childPath(tree.realpath, pkg),
children: pkg._bundled || []
})
tree.children.push(child)
Expand Down
10 changes: 10 additions & 0 deletions lib/utils/child-path.js
@@ -0,0 +1,10 @@
'use strict'
var path = require('path')
var validate = require('aproba')
var moduleName = require('../utils/module-name.js')

module.exports = childPath
function childPath (parentPath, child) {
validate('SO', arguments)
return path.join(parentPath, 'node_modules', moduleName(child))
}
9 changes: 9 additions & 0 deletions test/tap/unit-child-path.js
@@ -0,0 +1,9 @@
'use strict'
var test = require('tap').test
var childPath = require('../../lib/utils/child-path.js')

test('childPath', function (t) {
t.is(childPath('/path/to', {name: 'abc'}), '/path/to/node_modules/abc', 'basic use')
t.is(childPath('/path/to', {package: {name: '@zed/abc'}}), '/path/to/node_modules/@zed/abc', 'scoped use')
t.end()
})

0 comments on commit 4a97440

Please sign in to comment.