Skip to content
Snippets Groups Projects

Resolve "Add zsh, fish, and bash shell completion helper function generation as well as the related command"

1 unresolved thread
3 files
+ 83
81
Compare changes
  • Side-by-side
  • Inline

Files

import { existsSync, writeFileSync } from 'fs';
import { existsSync, writeFileSync } from 'fs';
import inquirer from 'inquirer';
import inquirer from 'inquirer';
import CommanderCommand from '../../CommanderCommand';
import CommanderCommand from '../../CommanderCommand';
import { generateBashCompletion, getRoot } from '../../../helpers/AutoCompletionHelper';
import { generateBashCompletion, getRoot } from '../../../helpers/AutoCompletionHelper';
 
class CompletionBashCommand extends CommanderCommand {
class CompletionBashCommand extends CommanderCommand {
protected commandName: string = 'bash';
protected commandName: string = 'bash';
protected default_filename: string = './dojo_bash_completion.sh'
protected defaultFilename: string = './dojo_bash_completion.sh';
writeFile(filename: string) {
writeFile(filename: string) {
const installInstructions: string = `
const installInstructions: string = `
@@ -16,43 +17,43 @@ overwrite it, if it only contains the 'dojo' completion.
@@ -16,43 +17,43 @@ overwrite it, if it only contains the 'dojo' completion.
This can be performed by either
This can be performed by either
cat ${filename} > ~/.bash_completion # overwrites .bash_completion
cat ${ filename } > ~/.bash_completion # overwrites .bash_completion
cat ${filename} >> ~/.bash_completion # appends to .bash_completion
cat ${ filename } >> ~/.bash_completion # appends to .bash_completion
For more details: <https://github.com/scop/bash-completion/blob/master/README.md>
For more details: <https://github.com/scop/bash-completion/blob/master/README.md>
`
`;
try {
try {
writeFileSync(filename, generateBashCompletion(getRoot(this.command)))
writeFileSync(filename, generateBashCompletion(getRoot(this.command)));
console.log(`Bash completion successfully written.`)
console.log(`Bash completion successfully written.`);
console.log(`${installInstructions}`)
console.log(`${ installInstructions }`);
} catch (error) {
} catch ( error ) {
console.log(`Error: ${error}.`)
console.log(`Error: ${ error }.`);
}
}
}
}
protected defineCommand() {
protected defineCommand() {
this.command
this.command
.description('generate bash completion')
.description('generate bash completion')
.option(`-f, --file <filename>', 'complete path of the filename where the bash completion will be stored (default to ${this.default_filename}).`)
.option('-f, --file <filename>', `complete path of the filename where the bash completion will be stored (default to ${ this.defaultFilename }).`, this.defaultFilename)
.action(this.commandAction.bind(this));
.option('-y, --force', 'don\'t ask for file overwrite confirmation')
 
.action(this.commandAction.bind(this));
}
}
protected async commandAction(options: { file?: string }): Promise<void> {
protected async commandAction(options: { file: string, force: boolean }): Promise<void> {
const filename = options.file ?? this.default_filename
if ( !options.force && existsSync(options.file) ) {
if (existsSync(filename)) {
// File exists in path
// File exists in path
const confirm: boolean = (await inquirer.prompt({
const confirm: boolean = (await inquirer.prompt({
name: 'confirm',
name : 'confirm',
message: `${options.file} is going to be overwritten. Are you sure?`,
message: `${ options.file } is going to be overwritten. Are you sure?`,
type: 'confirm',
type : 'confirm',
default: false
default: false
})).confirm;
})).confirm;
if (confirm) {
if ( confirm ) {
this.writeFile(filename)
this.writeFile(options.file);
}
}
} else {
} else {
this.writeFile(filename)
this.writeFile(options.file);
}
}
}
}
}
}
Loading