forked from npm/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bundles: Refactor the bundled module model (again)
This rejiggers bundled modules, such that they're moved out of the bundling module's folder at extract time and places in staging by themselves like any other module. This allows the rest of npm to pretend they're normal modules, which lets us remove a whole host of special cases and overall simplifies our code. Fixes: #10482 So if you had: ``` a bundles b requres c ``` And the version of bundled `c` in `b` doesn't match the `package.json` it will end up installing a new copy of `c` under `b` and ditching the contents of `b`. PR-URL: npm/npm#10487 Credit: @iarna Reviewed By: @othiym23
- Loading branch information
Showing
7 changed files
with
161 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,77 @@ | ||
'use strict' | ||
var path = require('path') | ||
var iferr = require('iferr') | ||
var asyncMap = require('slide').asyncMap | ||
var rename = require('graceful-fs').rename | ||
var gentlyRm = require('../../utils/gently-rm.js') | ||
var updatePackageJson = require('../update-package-json') | ||
var npm = require('../../npm.js') | ||
var moduleName = require('../../utils/module-name.js') | ||
var packageId = require('../../utils/package-id.js') | ||
var cache = require('../../cache.js') | ||
var buildPath = require('../build-path.js') | ||
|
||
module.exports = function (top, buildpath, pkg, log, next) { | ||
log.silly('extract', packageId(pkg)) | ||
var up = npm.config.get('unsafe-perm') | ||
var user = up ? null : npm.config.get('user') | ||
var group = up ? null : npm.config.get('group') | ||
cache.unpack(pkg.package.name, pkg.package.version | ||
, buildpath | ||
, null, null, user, group, | ||
function (er) { | ||
if (er) return next(er) | ||
updatePackageJson(pkg, buildpath, next) | ||
}) | ||
cache.unpack(pkg.package.name, pkg.package.version, buildpath, null, null, user, group, | ||
andUpdatePackageJson(pkg, buildpath, andStageBundledChildren(pkg, buildpath, log, next))) | ||
} | ||
|
||
function andUpdatePackageJson (pkg, buildpath, next) { | ||
return iferr(next, function () { | ||
updatePackageJson(pkg, buildpath, next) | ||
}) | ||
} | ||
|
||
function andStageBundledChildren (pkg, buildpath, log, next) { | ||
var staging = path.resolve(buildpath, '..') | ||
return iferr(next, function () { | ||
asyncMap(pkg.children, andStageBundledModule(pkg, staging, buildpath), cleanupBundled) | ||
}) | ||
function cleanupBundled () { | ||
gentlyRm(path.join(buildpath, 'node_modules'), next) | ||
} | ||
} | ||
|
||
function andStageBundledModule (bundler, staging, parentPath) { | ||
return function (child, next) { | ||
stageBundledModule(bundler, child, staging, parentPath, next) | ||
} | ||
} | ||
|
||
function getTree (pkg) { | ||
while (pkg.parent) pkg = pkg.parent | ||
return pkg | ||
} | ||
|
||
function warn (pkg, code, msg) { | ||
var tree = getTree(pkg) | ||
var err = new Error(msg) | ||
err.code = code | ||
tree.warnings.push(err) | ||
} | ||
|
||
function stageBundledModule (bundler, child, staging, parentPath, next) { | ||
var stageFrom = path.join(parentPath, 'node_modules', child.package.name) | ||
var stageTo = buildPath(staging, child) | ||
|
||
asyncMap(child.children, andStageBundledModule(bundler, staging, stageFrom), iferr(next, moveModule)) | ||
|
||
function moveModule () { | ||
if (child.fromBundle) { | ||
return rename(stageFrom, stageTo, iferr(next, updateMovedPackageJson)) | ||
} else { | ||
warn(bundler, 'EBUNDLEOVERRIDE', 'In ' + packageId(bundler) + | ||
' replacing bundled version of ' + moduleName(child) + | ||
' with ' + packageId(child)) | ||
return gentlyRm(stageFrom, next) | ||
} | ||
} | ||
|
||
function updateMovedPackageJson () { | ||
updatePackageJson(child, stageTo, next) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict' | ||
var uniqueFilename = require('unique-filename') | ||
var moduleName = require('../utils/module-name.js') | ||
|
||
module.exports = buildPath | ||
function buildPath (staging, pkg) { | ||
return uniqueFilename(staging, moduleName(pkg), pkg.realpath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters