diff options
author | Michael Perrotte <mike@npmjs.com> | 2020-02-20 01:54:32 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2020-05-08 04:11:52 +0300 |
commit | edfc8167dfb5c797d73cae87c6a333275d3e8ed5 (patch) | |
tree | 33f326eb8744f52037ba6e548b1cc80ac3a3bd19 /lib | |
parent | cc633182272cb54e50ae6237829c307cc20bbb63 (diff) |
wip: Use @npmcli/arborist to install packages locally
Still a lot to do, and this has a lot of very rought edges.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/config/flat-options.js | 1 | ||||
-rw-r--r-- | lib/install.js | 72 |
2 files changed, 51 insertions, 22 deletions
diff --git a/lib/config/flat-options.js b/lib/config/flat-options.js index b035bf4ff..95bbceb39 100644 --- a/lib/config/flat-options.js +++ b/lib/config/flat-options.js @@ -33,6 +33,7 @@ const buildOmitList = npm => { return [...omit] } +// scriptShell is a thing const flatOptions = npm => npm.flatOptions || Object.freeze({ // Note that many of these do not come from configs or cli flags // per se, though they may be implied or defined by them. diff --git a/lib/install.js b/lib/install.js index ea14842ae..401f85be0 100644 --- a/lib/install.js +++ b/lib/install.js @@ -15,7 +15,6 @@ // added, and then that's passed to the next generation of installation. module.exports = install -module.exports.Installer = Installer var usage = require('./utils/usage') @@ -35,6 +34,8 @@ install.usage = usage( '[--save-prod|--save-dev|--save-optional] [--save-exact] [--no-save]' ) +const npa = require('npm-package-arg') + install.completion = function (opts, cb) { validate('OF', arguments) // install can complete to a folder with a package.json, or any package. @@ -98,11 +99,10 @@ const Arborist = require('@npmcli/arborist') // dependencies var log = require('npmlog') -var archy = require('archy') +// const sillyLogTree = require('./util/silly-log-tree.js') // npm internal utils var npm = require('./npm.js') -var parseJSON = require('./utils/parse-json.js') var output = require('./utils/output.js') var saveMetrics = require('./utils/metrics.js').save @@ -114,6 +114,8 @@ var { } = require('./install/fund.js') var errorMessage = require('./utils/error-message.js') +const path = require('path') + function install (where, args, cb) { if (!cb) { cb = args @@ -127,7 +129,7 @@ function install (where, args, cb) { ? globalTop : npm.prefix } - var dryrun = npm.flatOptions.dryRun + const {dryRun} = npm.flatOptions.dryRun // TODO: Add warnings for other deprecated flags if (npm.config.get('dev')) { @@ -137,26 +139,52 @@ function install (where, args, cb) { if (where === globalTop && !args.length) { args = ['.'] } - args = args.filter(function (a) { - return path.resolve(a) !== npm.prefix - }) + args = args.filter(a => path.resolve(a) !== npm.prefix) + + const saveWhere = npm.flatOptions.saveProd ? 'dependencies' + : npm.flatOptions.saveDev ? 'devDependencies' + : npm.flatOptions.saveOptional ? 'optionalDependencies' + : npm.flatOptions.savePeer ? 'peerDependencies' + : 'dependencies' + + const {saveBundled} = npm.flatOptions + const add = !args.length ? null : args.reduce((add, a) => { + // npm install foo + // npm install @foo/bar + // npm install foo@bar + // npm install @foo/bar@baz + // npm install git+ssh://foogit.com:bar/baz#boo + const spec = npa(a, where) + add[saveWhere].push(spec) + + // XXX need to fix this npm install http://foo.com/bar.tgz --save-bundled + // TODO: arborist should take saveBundled boolean + // Point of research: can you bundle any dep other than production? If not, + // we can pass `bundleDependencies: [array, of, specs]` to arborist, + // it'll do {dependencies:{name:spec, ...}, bundleDependencies:[name,...]} + if (saveBundled && spec.name) { + add.bundleDependencies.push(spec.name) + } + return add + }, { [saveWhere]: [], bundleDependencies: [] }) - /* used to build up the appropriate {add:{...}} options to Arborist.reify - save: npm.config.get('save'), - saveBundle: npm.config.get('save-bundle'), - saveDev: npm.config.get('save-dev'), - saveOptional: npm.config.get('save-optional'), - savePeer: npm.config.get('save-peer'), - saveProd: npm.config.get('save-prod'), - saveExact: npm.config.get('save-exact'), - savePrefix: npm.config.get('save-prefix'),*/ + const arb = new Arborist({ + ...this.flatOptions, + path: where, + }) - const opts = { - ...npm.flatOptions, - add: { + // TODO: + // - audit + // - funding + // - more logging (archy-ize the tree for silly logging) + // - global installs in Arborist - } + const opt = { + ...this.flatOptions, + add, } - + arb[dryRun ? 'buildIdealTree' : 'reify'](opt).then(tree => { + output('TREEEEEEEE', tree) + cb() + }, er => cb(er)) } - |