From 7200fd010a67cdea6b541117e68a0a3c6694e720 Mon Sep 17 00:00:00 2001 From: Daniel Stockman Date: Tue, 21 Aug 2018 17:14:04 -0700 Subject: [PATCH] feat(cli): Configure commands in root package, all other bits in cli package This moves a step closer to the ideal of enabling third-party commands in the git manner. Refs #1584 --- core/cli/index.js | 34 ++-------------------- core/cli/package.json | 14 --------- core/lerna/cli.js | 4 +-- core/lerna/index.js | 45 +++++++++++++++++++++++++++++ core/lerna/package.json | 15 ++++++++++ helpers/command-runner/index.js | 10 +++---- helpers/command-runner/package.json | 3 +- package-lock.json | 31 ++++++++++---------- 8 files changed, 83 insertions(+), 73 deletions(-) create mode 100644 core/lerna/index.js diff --git a/core/cli/index.js b/core/cli/index.js index 3e7fc88a23..61d92acebf 100644 --- a/core/cli/index.js +++ b/core/cli/index.js @@ -5,27 +5,11 @@ const log = require("npmlog"); const yargs = require("yargs/yargs"); const globalOptions = require("@lerna/global-options"); -const addCmd = require("@lerna/add/command"); -const bootstrapCmd = require("@lerna/bootstrap/command"); -const changedCmd = require("@lerna/changed/command"); -const cleanCmd = require("@lerna/clean/command"); -const createCmd = require("@lerna/create/command"); -const diffCmd = require("@lerna/diff/command"); -const execCmd = require("@lerna/exec/command"); -const importCmd = require("@lerna/import/command"); -const initCmd = require("@lerna/init/command"); -const linkCmd = require("@lerna/link/command"); -const listCmd = require("@lerna/list/command"); -const publishCmd = require("@lerna/publish/command"); -const runCmd = require("@lerna/run/command"); -const versionCmd = require("@lerna/version/command"); - module.exports = lernaCLI; /** - * Essentially a factory that returns a yargs() instance that can - * be used to call parse() immediately (as in ../lerna) or by - * unit tests to encapsulate instantiation with "real" arguments. + * A factory that returns a yargs() instance configured with everything except commands. + * Chain .parse() from this method to invoke. * * @param {Array = []} argv * @param {String = process.cwd()} cwd @@ -35,20 +19,6 @@ function lernaCLI(argv, cwd) { return globalOptions(cli) .usage("Usage: $0 [options]") - .command(addCmd) - .command(bootstrapCmd) - .command(changedCmd) - .command(cleanCmd) - .command(createCmd) - .command(diffCmd) - .command(execCmd) - .command(importCmd) - .command(initCmd) - .command(linkCmd) - .command(listCmd) - .command(publishCmd) - .command(runCmd) - .command(versionCmd) .demandCommand(1, "A command is required. Pass --help to see all available commands and options.") .recommendCommands() .strict() diff --git a/core/cli/package.json b/core/cli/package.json index 023850ccd5..8135710835 100644 --- a/core/cli/package.json +++ b/core/cli/package.json @@ -33,21 +33,7 @@ "populate--": true }, "dependencies": { - "@lerna/add": "file:../../commands/add", - "@lerna/bootstrap": "file:../../commands/bootstrap", - "@lerna/changed": "file:../../commands/changed", - "@lerna/clean": "file:../../commands/clean", - "@lerna/create": "file:../../commands/create", - "@lerna/diff": "file:../../commands/diff", - "@lerna/exec": "file:../../commands/exec", "@lerna/global-options": "file:../global-options", - "@lerna/import": "file:../../commands/import", - "@lerna/init": "file:../../commands/init", - "@lerna/link": "file:../../commands/link", - "@lerna/list": "file:../../commands/list", - "@lerna/publish": "file:../../commands/publish", - "@lerna/run": "file:../../commands/run", - "@lerna/version": "file:../../commands/version", "dedent": "^0.7.0", "npmlog": "^4.1.2", "yargs": "^12.0.1" diff --git a/core/lerna/cli.js b/core/lerna/cli.js index a489adf76c..8ef78c7973 100755 --- a/core/lerna/cli.js +++ b/core/lerna/cli.js @@ -8,7 +8,5 @@ const importLocal = require("import-local"); if (importLocal(__filename)) { require("npmlog").info("cli", "using local version of lerna"); } else { - const pkg = require("./package.json"); - - require("@lerna/cli")().parse(process.argv.slice(2), { lernaVersion: pkg.version }); + require(".")(process.argv.slice(2)); } diff --git a/core/lerna/index.js b/core/lerna/index.js new file mode 100644 index 0000000000..a46caba313 --- /dev/null +++ b/core/lerna/index.js @@ -0,0 +1,45 @@ +"use strict"; + +const cli = require("@lerna/cli"); + +const addCmd = require("@lerna/add/command"); +const bootstrapCmd = require("@lerna/bootstrap/command"); +const changedCmd = require("@lerna/changed/command"); +const cleanCmd = require("@lerna/clean/command"); +const createCmd = require("@lerna/create/command"); +const diffCmd = require("@lerna/diff/command"); +const execCmd = require("@lerna/exec/command"); +const importCmd = require("@lerna/import/command"); +const initCmd = require("@lerna/init/command"); +const linkCmd = require("@lerna/link/command"); +const listCmd = require("@lerna/list/command"); +const publishCmd = require("@lerna/publish/command"); +const runCmd = require("@lerna/run/command"); +const versionCmd = require("@lerna/version/command"); + +const pkg = require("./package.json"); + +module.exports = main; + +function main(argv) { + const context = { + lernaVersion: pkg.version, + }; + + return cli() + .command(addCmd) + .command(bootstrapCmd) + .command(changedCmd) + .command(cleanCmd) + .command(createCmd) + .command(diffCmd) + .command(execCmd) + .command(importCmd) + .command(initCmd) + .command(linkCmd) + .command(listCmd) + .command(publishCmd) + .command(runCmd) + .command(versionCmd) + .parse(argv, context); +} diff --git a/core/lerna/package.json b/core/lerna/package.json index 1d12c605ca..f0ff65f3c2 100644 --- a/core/lerna/package.json +++ b/core/lerna/package.json @@ -17,6 +17,7 @@ "lerna": "cli.js" }, "files": [ + "index.js", "cli.js" ], "engines": { @@ -33,7 +34,21 @@ "populate--": true }, "dependencies": { + "@lerna/add": "file:../../commands/add", + "@lerna/bootstrap": "file:../../commands/bootstrap", + "@lerna/changed": "file:../../commands/changed", + "@lerna/clean": "file:../../commands/clean", "@lerna/cli": "file:../cli", + "@lerna/create": "file:../../commands/create", + "@lerna/diff": "file:../../commands/diff", + "@lerna/exec": "file:../../commands/exec", + "@lerna/import": "file:../../commands/import", + "@lerna/init": "file:../../commands/init", + "@lerna/link": "file:../../commands/link", + "@lerna/list": "file:../../commands/list", + "@lerna/publish": "file:../../commands/publish", + "@lerna/run": "file:../../commands/run", + "@lerna/version": "file:../../commands/version", "import-local": "^1.0.0", "npmlog": "^4.1.2" } diff --git a/helpers/command-runner/index.js b/helpers/command-runner/index.js index cfc7c60ef3..781df1b1f6 100644 --- a/helpers/command-runner/index.js +++ b/helpers/command-runner/index.js @@ -1,8 +1,7 @@ "use strict"; const path = require("path"); -const yargs = require("yargs/yargs"); -const globalOptions = require("@lerna/global-options"); +const lernaCLI = require("@lerna/cli"); module.exports = commandRunner; @@ -22,16 +21,13 @@ function commandRunner(commandModule) { return cwd => { // create a _new_ yargs instance every time cwd changes to avoid singleton pollution - const cli = yargs([], cwd) - .strict() + const cli = lernaCLI([], cwd) .exitProcess(false) .detectLocale(false) .showHelpOnFail(false) .wrap(null) .command(commandModule); - globalOptions(cli, { loglevel: "silent", progress: false }); - return (...args) => new Promise((resolve, reject) => { const yargsMeta = {}; @@ -39,6 +35,8 @@ function commandRunner(commandModule) { const context = { cwd, lernaVersion: "__TEST_VERSION__", + loglevel: "silent", + progress: false, onResolved: result => { // success resolves the result, if any, returned from execute() resolve(Object.assign({}, result, yargsMeta)); diff --git a/helpers/command-runner/package.json b/helpers/command-runner/package.json index 863ec1377a..fc53573168 100644 --- a/helpers/command-runner/package.json +++ b/helpers/command-runner/package.json @@ -6,7 +6,6 @@ "private": true, "license": "MIT", "dependencies": { - "@lerna/global-options": "file:../../core/global-options", - "yargs": "^12.0.1" + "@lerna/cli": "file:../../core/cli" } } diff --git a/package-lock.json b/package-lock.json index a515120209..808ad63f18 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,8 +50,7 @@ "version": "file:helpers/command-runner", "dev": true, "requires": { - "@lerna/global-options": "file:core/global-options", - "yargs": "^12.0.1" + "@lerna/cli": "file:core/cli" } }, "@lerna-test/commit-change-to-package": { @@ -330,21 +329,7 @@ "@lerna/cli": { "version": "file:core/cli", "requires": { - "@lerna/add": "file:commands/add", - "@lerna/bootstrap": "file:commands/bootstrap", - "@lerna/changed": "file:commands/changed", - "@lerna/clean": "file:commands/clean", - "@lerna/create": "file:commands/create", - "@lerna/diff": "file:commands/diff", - "@lerna/exec": "file:commands/exec", "@lerna/global-options": "file:core/global-options", - "@lerna/import": "file:commands/import", - "@lerna/init": "file:commands/init", - "@lerna/link": "file:commands/link", - "@lerna/list": "file:commands/list", - "@lerna/publish": "file:commands/publish", - "@lerna/run": "file:commands/run", - "@lerna/version": "file:commands/version", "dedent": "^0.7.0", "npmlog": "^4.1.2", "yargs": "^12.0.1" @@ -6114,7 +6099,21 @@ "lerna": { "version": "file:core/lerna", "requires": { + "@lerna/add": "file:commands/add", + "@lerna/bootstrap": "file:commands/bootstrap", + "@lerna/changed": "file:commands/changed", + "@lerna/clean": "file:commands/clean", "@lerna/cli": "file:core/cli", + "@lerna/create": "file:commands/create", + "@lerna/diff": "file:commands/diff", + "@lerna/exec": "file:commands/exec", + "@lerna/import": "file:commands/import", + "@lerna/init": "file:commands/init", + "@lerna/link": "file:commands/link", + "@lerna/list": "file:commands/list", + "@lerna/publish": "file:commands/publish", + "@lerna/run": "file:commands/run", + "@lerna/version": "file:commands/version", "import-local": "^1.0.0", "npmlog": "^4.1.2" }