From 32357f855e0b37e23b0ecac7daf5f119dcecc5ef Mon Sep 17 00:00:00 2001 From: Daniel Stockman Date: Mon, 20 Aug 2018 13:57:00 -0700 Subject: [PATCH] fix: Setup instance.filteredPackages explicitly --- commands/add/index.js | 6 +++++ commands/bootstrap/index.js | 45 ++++++++++++++++++++++++------------- commands/clean/index.js | 28 ++++++++++++++--------- commands/exec/index.js | 19 +++++++++++----- commands/list/index.js | 10 ++++++++- commands/run/index.js | 35 +++++++++++++++++------------ 6 files changed, 97 insertions(+), 46 deletions(-) diff --git a/commands/add/index.js b/commands/add/index.js index 10904d7c17..a6a801b87b 100644 --- a/commands/add/index.js +++ b/commands/add/index.js @@ -11,6 +11,7 @@ const Command = require("@lerna/command"); const npmConf = require("@lerna/npm-conf"); const bootstrap = require("@lerna/bootstrap"); const ValidationError = require("@lerna/validation-error"); +const { getFilteredPackages } = require("@lerna/filter-options"); const getRangeToReference = require("./lib/get-range-to-reference"); module.exports = factory; @@ -52,6 +53,11 @@ class AddCommand extends Command { this.spec.version = version; }); + chain = chain.then(() => getFilteredPackages(this.packageGraph, this.execOpts, this.options)); + chain = chain.then(filteredPackages => { + this.filteredPackages = filteredPackages; + }); + chain = chain.then(() => this.collectPackagesToChange()); chain = chain.then(packagesToChange => { this.packagesToChange = packagesToChange; diff --git a/commands/bootstrap/index.js b/commands/bootstrap/index.js index fb776efc6a..5864a2ae55 100644 --- a/commands/bootstrap/index.js +++ b/commands/bootstrap/index.js @@ -20,6 +20,7 @@ const runParallelBatches = require("@lerna/run-parallel-batches"); const symlinkBinary = require("@lerna/symlink-binary"); const symlinkDependencies = require("@lerna/symlink-dependencies"); const ValidationError = require("@lerna/validation-error"); +const { getFilteredPackages } = require("@lerna/filter-options"); const hasDependencyInstalled = require("./lib/has-dependency-installed"); const isHoistedPackage = require("./lib/is-hoisted-package"); @@ -35,9 +36,7 @@ class BootstrapCommand extends Command { } initialize() { - const { registry, rejectCycles, npmClient = "npm", npmClientArgs, mutex, hoist } = this.options; - const filteredLength = this.filteredPackages.length; - this.packageCountLabel = `${filteredLength} package${filteredLength > 1 ? "s" : ""}`; + const { registry, npmClient = "npm", npmClientArgs, mutex, hoist } = this.options; if (npmClient === "yarn" && hoist) { throw new ValidationError( @@ -81,20 +80,31 @@ class BootstrapCommand extends Command { this.npmConfig.npmClientArgs = [...(npmClientArgs || []), ...doubleDashArgs]; } - this.batchedPackages = this.toposort - ? batchPackages(this.filteredPackages, rejectCycles) - : [this.filteredPackages]; + let chain = Promise.resolve(); - if (npmClient === "yarn" && !mutex) { - return getPort({ port: 42424, host: "0.0.0.0" }).then(port => { - this.npmConfig.mutex = `network:${port}`; - this.logger.silly("npmConfig", this.npmConfig); - }); - } + chain = chain.then(() => getFilteredPackages(this.packageGraph, this.execOpts, this.options)); + chain = chain.then(filteredPackages => { + this.filteredPackages = filteredPackages; + }); + + chain = chain.then(() => this.validatePackageNames()); + + chain = chain.then(() => { + this.batchedPackages = this.toposort + ? batchPackages(this.filteredPackages, this.options.rejectCycles) + : [this.filteredPackages]; - this.validatePackageNames(); + if (npmClient === "yarn" && !mutex) { + return getPort({ port: 42424, host: "0.0.0.0" }).then(port => { + this.npmConfig.mutex = `network:${port}`; + this.logger.silly("npmConfig", this.npmConfig); + }); + } + + this.logger.silly("npmConfig", this.npmConfig); + }); - this.logger.silly("npmConfig", this.npmConfig); + return chain; } execute() { @@ -102,9 +112,12 @@ class BootstrapCommand extends Command { return this.installRootPackageOnly(); } + const filteredLength = this.filteredPackages.length; + const packageCountLabel = `${filteredLength} package${filteredLength > 1 ? "s" : ""}`; + // root install does not need progress bar this.enableProgressBar(); - this.logger.info("", `Bootstrapping ${this.packageCountLabel}`); + this.logger.info("", `Bootstrapping ${packageCountLabel}`); const tasks = [ () => this.getDependenciesToInstall(), @@ -124,7 +137,7 @@ class BootstrapCommand extends Command { } return pWaterfall(tasks).then(() => { - this.logger.success("", `Bootstrapped ${this.packageCountLabel}`); + this.logger.success("", `Bootstrapped ${packageCountLabel}`); }); } diff --git a/commands/clean/index.js b/commands/clean/index.js index 182da1212c..7ea7da9f06 100644 --- a/commands/clean/index.js +++ b/commands/clean/index.js @@ -6,6 +6,7 @@ const pMap = require("p-map"); const Command = require("@lerna/command"); const rimrafDir = require("@lerna/rimraf-dir"); const PromptUtilities = require("@lerna/prompt"); +const { getFilteredPackages } = require("@lerna/filter-options"); module.exports = factory; @@ -19,19 +20,26 @@ class CleanCommand extends Command { } initialize() { - this.directoriesToDelete = this.filteredPackages.map(pkg => pkg.nodeModulesLocation); + let chain = Promise.resolve(); - if (this.options.yes) { - return true; - } + chain = chain.then(() => getFilteredPackages(this.packageGraph, this.execOpts, this.options)); + chain = chain.then(filteredPackages => { + this.directoriesToDelete = filteredPackages.map(pkg => pkg.nodeModulesLocation); + }); + + return chain.then(() => { + if (this.options.yes) { + return true; + } - this.logger.info("", "Removing the following directories:"); - this.logger.info( - "clean", - this.directoriesToDelete.map(dir => path.relative(this.project.rootPath, dir)).join("\n") - ); + this.logger.info("", "Removing the following directories:"); + this.logger.info( + "clean", + this.directoriesToDelete.map(dir => path.relative(this.project.rootPath, dir)).join("\n") + ); - return PromptUtilities.confirm("Proceed?"); + return PromptUtilities.confirm("Proceed?"); + }); } execute() { diff --git a/commands/exec/index.js b/commands/exec/index.js index c3ab621164..a56a277830 100644 --- a/commands/exec/index.js +++ b/commands/exec/index.js @@ -5,6 +5,7 @@ const Command = require("@lerna/command"); const batchPackages = require("@lerna/batch-packages"); const runParallelBatches = require("@lerna/run-parallel-batches"); const ValidationError = require("@lerna/validation-error"); +const { getFilteredPackages } = require("@lerna/filter-options"); module.exports = factory; @@ -27,8 +28,6 @@ class ExecCommand extends Command { throw new ValidationError("ENOCOMMAND", "A command to execute is required"); } - this.count = this.filteredPackages.length; - // inverted boolean options this.bail = this.options.bail !== false; this.prefix = this.options.prefix !== false; @@ -37,9 +36,19 @@ class ExecCommand extends Command { // so cache it here to reduce churn during tighter loops this.env = Object.assign({}, process.env); - this.batchedPackages = this.toposort - ? batchPackages(this.filteredPackages, this.options.rejectCycles) - : [this.filteredPackages]; + let chain = Promise.resolve(); + + chain = chain.then(() => getFilteredPackages(this.packageGraph, this.execOpts, this.options)); + chain = chain.then(filteredPackages => { + this.filteredPackages = filteredPackages; + }); + + return chain.then(() => { + this.count = this.filteredPackages.length; + this.batchedPackages = this.toposort + ? batchPackages(this.filteredPackages, this.options.rejectCycles) + : [this.filteredPackages]; + }); } execute() { diff --git a/commands/list/index.js b/commands/list/index.js index 84580a9ce8..744d3b4a6e 100644 --- a/commands/list/index.js +++ b/commands/list/index.js @@ -3,6 +3,7 @@ const Command = require("@lerna/command"); const listable = require("@lerna/listable"); const output = require("@lerna/output"); +const { getFilteredPackages } = require("@lerna/filter-options"); module.exports = factory; @@ -16,7 +17,14 @@ class ListCommand extends Command { } initialize() { - this.result = listable.format(this.filteredPackages, this.options); + let chain = Promise.resolve(); + + chain = chain.then(() => getFilteredPackages(this.packageGraph, this.execOpts, this.options)); + chain = chain.then(filteredPackages => { + this.result = listable.format(filteredPackages, this.options); + }); + + return chain; } execute() { diff --git a/commands/run/index.js b/commands/run/index.js index 338a370afe..896c7b77bc 100644 --- a/commands/run/index.js +++ b/commands/run/index.js @@ -8,6 +8,7 @@ const batchPackages = require("@lerna/batch-packages"); const runParallelBatches = require("@lerna/run-parallel-batches"); const output = require("@lerna/output"); const ValidationError = require("@lerna/validation-error"); +const { getFilteredPackages } = require("@lerna/filter-options"); module.exports = factory; @@ -35,24 +36,30 @@ class RunCommand extends Command { this.bail = this.options.bail !== false; this.prefix = this.options.prefix !== false; - if (script === "env") { - this.packagesWithScript = this.filteredPackages; - } else { - this.packagesWithScript = this.filteredPackages.filter(pkg => pkg.scripts && pkg.scripts[script]); - } + let chain = Promise.resolve(); + + chain = chain.then(() => getFilteredPackages(this.packageGraph, this.execOpts, this.options)); + chain = chain.then(filteredPackages => { + this.packagesWithScript = + script === "env" + ? filteredPackages + : filteredPackages.filter(pkg => pkg.scripts && pkg.scripts[script]); + }); - this.count = this.packagesWithScript.length; + return chain.then(() => { + this.count = this.packagesWithScript.length; - if (!this.count) { - this.logger.success("run", `No packages found with the lifecycle script '${script}'`); + if (!this.count) { + this.logger.success("run", `No packages found with the lifecycle script '${script}'`); - // still exits zero, aka "ok" - return false; - } + // still exits zero, aka "ok" + return false; + } - this.batchedPackages = this.toposort - ? batchPackages(this.packagesWithScript, this.options.rejectCycles) - : [this.packagesWithScript]; + this.batchedPackages = this.toposort + ? batchPackages(this.packagesWithScript, this.options.rejectCycles) + : [this.packagesWithScript]; + }); } execute() {