From cdc8978e55dfbf19916c0aa9c1f93316c9be090b Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 4 Dec 2021 11:58:04 -0800 Subject: [PATCH] [Refactor] use `lockfile-info` --- get-tree.js | 36 ++++++++++++++----------- npm-info.js | 76 ---------------------------------------------------- package.json | 1 + 3 files changed, 22 insertions(+), 91 deletions(-) delete mode 100644 npm-info.js diff --git a/get-tree.js b/get-tree.js index 96160df..154d28d 100644 --- a/get-tree.js +++ b/get-tree.js @@ -6,7 +6,7 @@ const pacote = require('pacote'); const arb = new Arborist(); -const npmInfo = require('./npm-info'); +const lockfileInfo = require('lockfile-info'); function prune(tree, keepDev, keepProduction, keepPeer) { if (!keepDev || !keepProduction) { @@ -19,19 +19,23 @@ function prune(tree, keepDev, keepProduction, keepPeer) { return tree; } -async function getBaseTree(mode, logger) { - const { hasNodeModules, hasLockfile, hasPackage, lockfileVersion } = await npmInfo(mode); +async function getBaseTree(mode, logger) { // eslint-disable-line consistent-return + const { hasNodeModulesDir, hasLockfile, hasPackageJSON, lockfileVersion } = await lockfileInfo(); - if (mode === 'actual' || hasNodeModules) { + const ideal = mode === 'ideal' || (mode === 'auto' && !hasNodeModulesDir && !hasLockfile); + const virtual = mode === 'virtual' || (mode === 'auto' && hasLockfile); + const actual = mode === 'actual' || (mode === 'auto' && hasNodeModulesDir); + + if (ideal) { const messages = [].concat( - hasNodeModules ? `\`${chalk.gray('node_modules')}\` found` : [], - mode === 'actual' ? 'mode is “actual”' : [], + `\`${chalk.gray('package.json')}\` ${hasPackageJSON ? '' : 'not '}found`, + mode === 'ideal' ? 'mode is “ideal”' : [], ); - logger(chalk.green(`${messages.join(', ')}; loading tree from disk...`)); - return arb.loadActual(); + logger(chalk.green(`${messages.join(', ')}; building ideal tree from \`${chalk.gray('package.json')}\`...`)); + return arb.buildIdealTree({ fullMetadata: true, update: { all: true } }); } - if (mode === 'virtual' || hasLockfile) { + if (virtual) { if (hasLockfile && lockfileVersion < 2) { const messages = ['v1 lockfile found'].concat(mode === 'virtual' ? 'mode is “virtual”' : []); logger(chalk.green(`${messages.join(', ')}; loading ideal tree from lockfile...`)); @@ -53,12 +57,14 @@ async function getBaseTree(mode, logger) { return arb.loadVirtual({ fullMetadata: true }); } - const messages = [].concat( - `\`${chalk.gray('package.json')}\` ${hasPackage ? '' : 'not '}found`, - mode === 'ideal' ? 'mode is “ideal”' : [], - ); - logger(chalk.green(`${messages.join(', ')}; building ideal tree from \`${chalk.gray('package.json')}\`...`)); - return arb.buildIdealTree({ fullMetadata: true, update: { all: true } }); + if (actual) { + const messages = [].concat( + hasNodeModulesDir ? `\`${chalk.gray('node_modules')}\` found` : [], + mode === 'actual' ? 'mode is “actual”' : [], + ); + logger(chalk.green(`${messages.join(', ')}; loading tree from disk...`)); + return arb.loadActual(); + } } module.exports = async function getTree(mode, { dev, logger = (x) => console.log(x), peer, production } = {}) { diff --git a/npm-info.js b/npm-info.js deleted file mode 100644 index 36b5958..0000000 --- a/npm-info.js +++ /dev/null @@ -1,76 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const path = require('path'); - -function stat(file) { - return new Promise((resolve, reject) => { - fs.stat(file, (err, result) => { - if (err) { - reject(err); - } else { - resolve(result); - } - }); - }); -} - -module.exports = async function npmInfo(mode) { - const pHasNodeModules = mode === 'auto' || mode === 'actual' - ? stat(path.join(process.cwd(), 'node_modules')).catch(() => false) - : false; - - const packagePath = path.join(process.cwd(), 'package.json'); - const packageLockPath = path.join(process.cwd(), 'package-lock.json'); - const shrinkwrapPath = path.join(process.cwd(), 'npm-shrinkwrap.json'); - const pHasPackageLock = stat(packageLockPath).then(() => true, () => false); - - const pHasLockfile = mode === 'auto' || mode === 'virtual' - ? pHasPackageLock.catch(async (hasPackageLock) => { - if (!hasPackageLock) { - return false; - } - return stat(shrinkwrapPath).then(() => true, () => false); - }) - : false; - - const pHasPackage = mode === 'auto' || mode === 'ideal' - ? stat(packagePath).catch(() => false) - : false; - - /* eslint-disable consistent-return, global-require */ - const pLockfileVersion = Promise.all([ - pHasLockfile, - pHasPackageLock, - ]).then(async ([ - hasLockfile, - hasPackageLock, - ]) => { - if (hasPackageLock) { - return require(packageLockPath).lockfileVersion; - } - if (hasLockfile) { - return require(shrinkwrapPath).lockfileVersion; - } - }); - /* eslint-enable consistent-return, global-require */ - - const [ - hasLockfile, - hasNodeModules, - hasPackage, - lockfileVersion, - ] = await Promise.all([ - pHasLockfile, - pHasNodeModules, - pHasPackage, - pLockfileVersion, - ]); - - return { - hasLockfile, - hasNodeModules, - hasPackage, - lockfileVersion, - }; -}; diff --git a/package.json b/package.json index d8434b6..20e12c2 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "fast_array_intersect": "^1.1.0", "get-json": "^1.0.1", "json-file-plus": "^3.3.1", + "lockfile-info": "^1.0.0", "pacote": "^12.0.2", "promise.allsettled": "^1.0.5", "semver": "^7.3.5",