@oclif/plugin-autocomplete
Version:
autocomplete plugin for oclif
69 lines (63 loc) • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const chalk = require("chalk");
const base_1 = require("../../base");
const create_1 = require("./create");
class Index extends base_1.AutocompleteBase {
async run() {
const { args, flags } = await this.parse(Index);
const shell = args.shell || this.determineShell(this.config.shell);
core_1.ux.action.start(`${chalk.bold('Building the autocomplete cache')}`);
await create_1.default.run([], this.config);
core_1.ux.action.stop();
if (!flags['refresh-cache']) {
const bin = this.config.bin;
const tabStr = shell === 'bash' ? '<TAB><TAB>' : '<TAB>';
const instructions = shell === 'powershell' ?
`New-Item -Type Directory -Path (Split-Path -Parent $PROFILE) -ErrorAction SilentlyContinue
Add-Content -Path $PROFILE -Value (Invoke-Expression -Command "${bin} autocomplete${this.config.topicSeparator}script ${shell}"); .$PROFILE` :
`$ printf "eval $(${bin} autocomplete${this.config.topicSeparator}script ${shell})" >> ~/.${shell}rc; source ~/.${shell}rc`;
let note = '';
switch (shell) {
case 'zsh':
note = `After sourcing, you can run \`${chalk.cyan('$ compaudit -D')}\` to ensure no permissions conflicts are present`;
break;
case 'bash':
note = 'If your terminal starts as a login shell you may need to print the init script into ~/.bash_profile or ~/.profile.';
break;
case 'powershell':
note = `Use the \`MenuComplete\` mode to get matching completions printed below the command line:\n${chalk.cyan('Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete')}`;
}
this.log(`
${chalk.bold(`Setup Instructions for ${bin.toUpperCase()} CLI Autocomplete ---`)}
1) Add the autocomplete ${shell === 'powershell' ? 'file' : 'env var'} to your ${shell} profile and source it
${chalk.cyan(instructions)}
${chalk.bold('NOTE')}: ${note}
2) Test it out, e.g.:
${chalk.cyan(`$ ${bin} ${tabStr}`)} # Command completion
${chalk.cyan(`$ ${bin} command --${tabStr}`)} # Flag completion
Enjoy!
`);
}
}
}
exports.default = Index;
Index.description = 'display autocomplete installation instructions';
Index.args = {
shell: core_1.Args.string({
description: 'Shell type',
options: ['zsh', 'bash', 'powershell'],
required: false,
}),
};
Index.flags = {
'refresh-cache': core_1.Flags.boolean({ description: 'Refresh cache (ignores displaying instructions)', char: 'r' }),
};
Index.examples = [
'$ <%= config.bin %> autocomplete',
'$ <%= config.bin %> autocomplete bash',
'$ <%= config.bin %> autocomplete zsh',
'$ <%= config.bin %> autocomplete powershell',
'$ <%= config.bin %> autocomplete --refresh-cache',
];