Skip to content

Commit

Permalink
feat(cli): Configure commands in root package, all other bits in cli …
Browse files Browse the repository at this point in the history
…package

This moves a step closer to the ideal of enabling third-party commands in the git manner.

Refs #1584
  • Loading branch information
evocateur committed Aug 22, 2018
1 parent 37ef5f2 commit 7200fd0
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 73 deletions.
34 changes: 2 additions & 32 deletions core/cli/index.js
Expand Up @@ -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
Expand All @@ -35,20 +19,6 @@ function lernaCLI(argv, cwd) {

return globalOptions(cli)
.usage("Usage: $0 <command> [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()
Expand Down
14 changes: 0 additions & 14 deletions core/cli/package.json
Expand Up @@ -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"
Expand Down
4 changes: 1 addition & 3 deletions core/lerna/cli.js
Expand Up @@ -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));
}
45 changes: 45 additions & 0 deletions 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);
}
15 changes: 15 additions & 0 deletions core/lerna/package.json
Expand Up @@ -17,6 +17,7 @@
"lerna": "cli.js"
},
"files": [
"index.js",
"cli.js"
],
"engines": {
Expand All @@ -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"
}
Expand Down
10 changes: 4 additions & 6 deletions 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;

Expand All @@ -22,23 +21,22 @@ 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 = {};

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));
Expand Down
3 changes: 1 addition & 2 deletions helpers/command-runner/package.json
Expand Up @@ -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"
}
}
31 changes: 15 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7200fd0

Please sign in to comment.