Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add yargs to middleware and standardize usage presentation #275

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading