Skip to content

Commit

Permalink
add yargs to middleware and standardize usage presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pirog committed Jul 12, 2024
1 parent d4d2c95 commit 8ae462c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Internals

* Added `yargs` to task middleware
* Added `appConfig` to `lando` for more powerful tasks
* Added `primary` to `appConfig` for more powerful tasks
* Updated to `[email protected]` for better `--` handling
Expand Down
25 changes: 13 additions & 12 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ module.exports = class Cli {
const $0 = process?.env?.LANDO_ENTRYPOINT_NAME ?? '$0';

// basic usage
const usage = [`Usage: ${$0} <command> [args] [options]`];
const usage = [`${this.chalk.green('Usage:')}\n ${$0} <command> [args] [options]`];
// add experimental mode info
if (userConfig.experimental) usage.push(`${this.makeArt('print', {text: '(experimental mode)', color: 'magenta'})}`);

Expand All @@ -325,9 +325,6 @@ module.exports = class Cli {
.example(`${$0} rebuild --help`, 'Get help about using the lando rebuild command')
.example(`${$0} destroy -y --debug`, 'Run lando destroy non-interactively and with maximum verbosity')
.example(`${$0} --clear`, 'Clear the lando tasks cache')
.middleware([(argv => {
argv._app = config;
})])
.parserConfiguration({'populate--': true})
.recommendCommands()
.wrap(yargs.terminalWidth() * 0.70)
Expand All @@ -339,23 +336,27 @@ module.exports = class Cli {
.option('lando', globalOptions.lando)
.option('help', globalOptions.help)
.option('verbose', globalOptions.verbose)
.version(false);
.version(false)
.middleware([(argv => {
argv._app = config;
argv._yargs = yargs;
})]);

// manually set scriptname if needed
if ($0 !== '$0') yargs.scriptName($0);

// Loop through the tasks and add them to the CLI
_.forEach(_.sortBy(tasks, 'command'), task => {
if (_.has(task, 'handler')) yargs.command(task);
else yargs.command(this.parseToYargs(task, config));
});

// Modify the script name in certain circumstances eg its packaged and being invoked from a symlink
// @NOTE: should we check for argv0 being a symlink?
if (_.has(process, 'pkg') && path.join(process.cwd(), process.argv0) !== process.execPath) {
yargs.scriptName(path.basename(process.argv0));
}

// Loop through the tasks and add them to the CLI
_.forEach(_.sortBy(tasks, 'command'), task => {
if (_.has(task, 'handler')) yargs.command(task);
else yargs.command(this.parseToYargs(task, config));
});

// Show help unless this is a delegation command
const current = _.find(tasks, {command: yargs.argv._[0]});
if ((yargs.argv.help || yargs.argv.lando) && _.get(current, 'delegate', false) === false) {
Expand Down Expand Up @@ -485,7 +486,7 @@ module.exports = class Cli {
}

// and also allow usage
if (usage) yargs.usage(usage);
if (usage) yargs.usage(`${this.chalk.green('Usage:')}\n ${usage}`);
}};
}

Expand Down

0 comments on commit 8ae462c

Please sign in to comment.