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
Compare and
6 files
+ 311
7
Compare changes
  • Side-by-side
  • Inline
Files
6
 
import { existsSync, writeFileSync } from 'fs';
 
import inquirer from 'inquirer';
 
import CommanderCommand from '../../CommanderCommand';
 
import { generateBashCompletion, getRoot } from '../../../helpers/AutoCompletionHelper';
 
 
class CompletionBashCommand extends CommanderCommand {
 
protected commandName: string = 'bash';
 
 
protected default_filename: string = './dojo_bash_completion.sh'
 
 
writeFile(filename: string) {
 
const installInstructions: string = `
 
The easiest way to install the completion is to append the content of the
 
generated file to the end of the '~/.bash_completion' file or to
 
overwrite it, if it only contains the 'dojo' completion.
 
 
This can be performed by either
 
 
cat ${filename} > ~/.bash_completion # overwrites .bash_completion
 
cat ${filename} >> ~/.bash_completion # appends to .bash_completion
 
 
For more details: <https://github.com/scop/bash-completion/blob/master/README.md>
 
`
 
try {
 
writeFileSync(filename, generateBashCompletion(getRoot(this.command)))
 
console.log(`Bash completion successfully written.`)
 
console.log(`${installInstructions}`)
 
} catch (error) {
 
console.log(`Error: ${error}, failed to write ${filename}`)
 
}
 
}
 
 
protected defineCommand() {
 
this.command
 
.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}).`)
 
.action(this.commandAction.bind(this));
 
}
 
 
protected async commandAction(options: { file?: string }): Promise<void> {
 
const filename = options.file ?? this.default_filename
 
if (existsSync(filename)) {
 
// File exists in path
 
const confirm: boolean = (await inquirer.prompt({
 
name: 'confirm',
 
message: `${options.file} is going to be overwritten. Are you sure?`,
 
type: 'confirm',
 
default: false
 
})).confirm;
 
 
if (confirm) {
 
this.writeFile(filename)
 
}
 
} else {
 
this.writeFile(filename)
 
}
 
}
 
}
 
 
 
export default new CompletionBashCommand();
 
\ No newline at end of file
Loading