imq-cli
Version:
Command Line Interface for IMQ
86 lines • 2.96 kB
JavaScript
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const lib_1 = require("../../lib");
const chalk_1 = require("chalk");
let PROGRAM = '';
let RX_EXISTS;
// istanbul ignore next
/**
* Prints add script success message to user
*
* @access private
* @param {string} rcFilename - path to shell rc file modified
*/
function printAdded(rcFilename) {
process.stdout.write(chalk_1.default.green(`Completion script added to `) +
chalk_1.default.cyan(rcFilename) + '\n' +
'To have these changes to take effect, please, run:\n\n' +
' $ ' + chalk_1.default.cyan(`source ${rcFilename}`) + '\n\n');
}
// istanbul ignore next
/**
* Returns completions script for user's shell
*
* @param {string} zshScript - zsh based script part
* @return {string}
*/
function getScript(zshScript) {
return `
###-begin-${PROGRAM}-completions-###
${zshScript}
_yargs_completions() {
local cur_word args type_list
cur_word="\${COMP_WORDS[COMP_CWORD]}"
args=("\${COMP_WORDS[@]}")
type_list=$(${PROGRAM} --get-yargs-completions "\${args[@]}")
COMPREPLY=( $(compgen -W "\${type_list}" -- \${cur_word}) )
if [ \${#COMPREPLY[@]} -eq 0 ]; then
COMPREPLY=( $(compgen -f -- "\${cur_word}" ) )
fi
return 0
}
complete -F _yargs_completions ${PROGRAM}
###-end-${PROGRAM}-completions-###\n`;
}
// istanbul ignore next
/**
* Prints script exists message to the user
*
* @param {string} rcFilename - path to shell rc file containing script
*/
function printExists(rcFilename) {
process.stdout.write(chalk_1.default.yellow.bold(`Completion script already exists in your ${rcFilename}.`) + '\n' +
'If it does not work, please try one of:\n\n' +
chalk_1.default.cyan(' 1. Reload your shell\n') +
chalk_1.default.cyan(` 2. Run source ${rcFilename}\n\n`));
}
// noinspection JSUnusedGlobalSymbols
_a = {
command: 'on',
describe: 'Enables completions for this program in your shell',
builder(yargs) {
PROGRAM = yargs.argv.$0;
RX_EXISTS = new RegExp(`###-begin-${PROGRAM}-completions-###`);
},
handler() {
try {
const zScript = lib_1.IS_ZSH ? 'autoload bashcompinit\nbashcompinit' : '';
const rcFilename = lib_1.IS_ZSH ? '~/.zshrc' : '~/.bashrc';
const rcFile = lib_1.resolve(rcFilename);
const rcText = fs_1.readFileSync(rcFile, { encoding: 'utf8' });
if (!RX_EXISTS.test(rcText)) {
(fs_1.existsSync(rcFile) ? fs_1.appendFileSync : lib_1.touch)(rcFile, getScript(zScript));
printAdded(rcFilename);
}
else {
printExists(rcFilename);
}
}
catch (err) {
lib_1.printError(err);
}
}
}, exports.command = _a.command, exports.describe = _a.describe, exports.builder = _a.builder, exports.handler = _a.handler;
//# sourceMappingURL=on.js.map
;