Skip to content
Snippets Groups Projects
Commit bfa3332b authored by michael.minelli's avatar michael.minelli
Browse files

Completion => Remove bash|zsh|fish subcommands for create|update command

parent f58d64ad
No related branches found
No related tags found
No related merge requests found
Pipeline #30171 passed
import CommanderCommand from '../CommanderCommand';
import CompletionBashCommand from './subcommands/CompletionBashCommand';
import CompletionFishCommand from './subcommands/CompletionFishCommand';
import CompletionZshCommand from './subcommands/CompletionZshCommand';
import CompletionCreateUpdateCommand from './subcommands/CompletionCreateUpdateCommand';
import CompletionGetCommand from './subcommands/CompletionGetCommand';
import CompletionScriptCommand from './subcommands/CompletionScriptCommand';
......@@ -15,10 +13,7 @@ class CompletionCommand extends CommanderCommand {
}
protected defineSubCommands() {
CompletionBashCommand.registerOnCommand(this.command);
CompletionFishCommand.registerOnCommand(this.command);
CompletionZshCommand.registerOnCommand(this.command);
CompletionCreateUpdateCommand.registerOnCommand(this.command);
CompletionGetCommand.registerOnCommand(this.command);
CompletionScriptCommand.registerOnCommand(this.command);
}
......
import CommanderCommand from '../../CommanderCommand';
import path from 'path';
import os from 'os';
import { updateRcFile } from '../../../helpers/AutoCompletionHelper';
class CompletionBashCommand extends CommanderCommand {
protected commandName: string = 'bash';
private readonly bashrcPath = path.join(os.homedir(), '.bashrc');
private readonly completionCommand = `
# Added by DojoCLI
eval "$(dojo completion script bash)"
`;
protected defineCommand() {
this.command.description('generate bash completion')
.action(this.commandAction.bind(this));
}
protected async commandAction(): Promise<void> {
updateRcFile('bash', this.bashrcPath, this.completionCommand);
}
}
export default new CompletionBashCommand();
\ No newline at end of file
import { generateFishCompletion, getRoot, tryRenameFile } from '../../../helpers/AutoCompletionHelper';
import CommanderCommand from '../../CommanderCommand';
import ora from 'ora';
import TextStyle from '../../../types/TextStyle';
import { Option } from 'commander';
import { generateFishCompletion, getRoot, tryRenameFile, updateRcFile } from '../../../helpers/AutoCompletionHelper';
import os, { homedir } from 'os';
import path from 'path';
import os from 'os';
import ora from 'ora';
import fs from 'fs-extra';
import GlobalHelper from '../../../helpers/GlobalHelper';
import TextStyle from '../../../types/TextStyle';
class CompletionFishCommand extends CommanderCommand {
protected commandName: string = 'fish';
class CompletionCreateUpdateCommand extends CommanderCommand {
protected commandName: string = 'create';
protected aliasNames: Array<string> = [ 'update' ];
private readonly installPath = path.join(os.homedir(), '.config/fish/completions/dojo.fish');
protected defineCommand() {
GlobalHelper.completionCommandDefinition(this.command)
.description('generate fish completion')
this.command.description('generate shell completion')
.addOption(new Option('-s, --shell <shell>', 'shell type').choices([ 'bash', 'zsh', 'fish' ]).makeOptionMandatory(true))
.addOption(new Option('-f, --file <filename>', '(only for fish shell)').implies({ shell: 'fish' }))
.addOption(new Option('-y, --force', 'don\'t ask for file overwrite confirmation (only for fish shell)').implies({ shell: 'fish' }))
.action(this.commandAction.bind(this));
}
private writeFile(filename: string, showInstructions: boolean) {
const spinner: ora.Ora = ora(`Writing fish completion in ${ filename }...`).start();
try {
fs.mkdirsSync(path.dirname(filename));
fs.writeFileSync(filename, generateFishCompletion(getRoot(this.command)));
spinner.succeed(`Fish completion successfully written in ${ filename }.`);
if ( showInstructions ) {
const cpCommand = ` cp -i ${ filename } ~/.config/fish/completions # interactive cp to avoid accidents `;
console.log(`
The easiest way to install the completion is to copy the ${ TextStyle.CODE(filename) } into the ${ TextStyle.CODE('~/.config/fish/completions') } directory.
${ TextStyle.CODE(cpCommand) }`);
}
} catch ( error ) {
spinner.fail(`Fish completion error: ${ error }.`);
}
private bash() {
const completionCommand = `
# Added by DojoCLI
eval "$(dojo completion script bash)"
`;
updateRcFile('bash', path.join(os.homedir(), '.bashrc'), completionCommand);
}
private zsh() {
const completionCommand = `
# Added by DojoCLI
source <(dojo completion script zsh)
`;
updateRcFile('zsh', path.join(homedir(), '.zshrc'), completionCommand);
}
/* The completion command must do the following:
- if a file is provided:
......@@ -56,15 +52,50 @@ ${ TextStyle.CODE(cpCommand) }`);
- else
- create the file containing the completion
*/
protected async commandAction(options: { file: string, force: boolean }): Promise<void> {
const filePath = path.resolve(options.file ?? this.installPath); // change that if file is empty
private async fish(options: { file: string, force: boolean }) {
const filePath = path.resolve(options.file ?? path.join(os.homedir(), '.config/fish/completions/dojo.fish'));
const showInstructions = !!options.file;
if ( !(await tryRenameFile(filePath, options.force)) ) { // means renaming was interrupted
return;
}
this.writeFile(filePath, showInstructions);
const spinner: ora.Ora = ora(`Writing fish completion in ${ filePath }...`).start();
try {
fs.mkdirsSync(path.dirname(filePath));
fs.writeFileSync(filePath, generateFishCompletion(getRoot(this.command)));
spinner.succeed(`Fish completion successfully written in ${ filePath }.`);
if ( showInstructions ) {
const cpCommand = ` cp -i ${ filePath } ~/.config/fish/completions # interactive cp to avoid accidents `;
console.log(`
The easiest way to install the completion is to copy the ${ TextStyle.CODE(filePath) } into the ${ TextStyle.CODE('~/.config/fish/completions') } directory.
${ TextStyle.CODE(cpCommand) }`);
}
} catch ( error ) {
spinner.fail(`Fish completion error: ${ error }.`);
}
}
protected async commandAction(options: { shell: 'bash' | 'zsh' | 'fish', file: string, force: boolean }): Promise<void> {
switch ( options.shell ) {
case 'bash':
this.bash();
break;
case 'zsh':
this.zsh();
break;
case 'fish':
await this.fish(options);
break;
default:
console.error('Unsupported shell.');
break;
}
}
}
export default new CompletionFishCommand();
\ No newline at end of file
export default new CompletionCreateUpdateCommand();
\ No newline at end of file
import CommanderCommand from '../../CommanderCommand';
import path from 'path';
import { homedir } from 'os';
import { updateRcFile } from '../../../helpers/AutoCompletionHelper';
class CompletionZshCommand extends CommanderCommand {
protected commandName: string = 'zsh';
private readonly zshrcPath: string = path.join(homedir(), '.zshrc');
private readonly completionCommand = `
# Added by DojoCLI
source <(dojo completion script zsh)
`;
protected defineCommand() {
this.command.description('generate zsh completion')
.action(this.commandAction.bind(this));
}
protected async commandAction(): Promise<void> {
updateRcFile('zsh', this.zshrcPath, this.completionCommand);
}
}
export default new CompletionZshCommand();
\ No newline at end of file
......@@ -12,14 +12,6 @@ class GlobalHelper {
return command;
}
public completionCommandDefinition(command: Command) {
command
.option('-f, --file <filename>')
.option('-y, --force', 'don\'t ask for file overwrite confirmation');
return command;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment