Skip to content

Commit

Permalink
feat: print completion script
Browse files Browse the repository at this point in the history
  • Loading branch information
KSXGitHub committed Jan 30, 2024
1 parent 8f3ebcd commit a8f2c87
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
16 changes: 16 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ const { tabtabDebug, systemShell } = require('./utils');
// TABTAB_DEBUG file provided.
const debug = tabtabDebug('tabtab');

/**
* Print completion code to stdout.
* @param {Object} options - Options object.
* @param {String} options.name - The package configured for completion
* @param {String} options.completer - The program the will act as the completer for the `name` program
* @param {String} options.shell
*/
const print = async ({ name, completer, shell }) => {
if (!name) throw new TypeError('options.name is required');
if (!completer) throw new TypeError('options.completer is required');
if (!shell) throw new TypeError('options.shell is required');
const completionScriptContent = await installer.getCompletionScript({ name, completer, shell })
console.log(completionScriptContent)
}

/**
* Install and enable completion on user system. It'll ask for:
*
Expand Down Expand Up @@ -219,6 +234,7 @@ const logFiles = () => {

module.exports = {
shell: systemShell,
print,
install,
uninstall,
parseEnv,
Expand Down
34 changes: 22 additions & 12 deletions lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,25 @@ const writeToTabtabScript = async ({ name, shell }) => {
);
};

/**
* Construct a completion script
* @param {Object} options - Options object
* @param {String} options.name - The package configured for completion
* @param {String} options.completer - The program the will act as the completer for the `name` program
* @param {String} [options.shell]
* @returns {Promise.<String>}
*/
const getCompletionScript = async ({ name, completer, shell }) => {
const templatePath = scriptFromShell(shell);
const templateContent = await readFile(templatePath, 'utf8');
const scriptContent = templateContent
.replace(/\{pkgname\}/g, name)
.replace(/{completer}/g, completer)
// on Bash on windows, we need to make sure to remove any \r
.replace(/\r?\n/g, '\n');
return scriptContent
};

/**
* This writes a new completion script in the internal `~/.config/tabtab`
* directory. Depending on the SHELL used, a different script is created for
Expand All @@ -238,19 +257,9 @@ const writeToCompletionScript = async ({ name, completer, shell }) => {
path.join(COMPLETION_DIR, shell, `${name}.${shell}`)
);

const script = scriptFromShell(shell);
debug('Writing completion script to', filename);
debug('with', script);

try {
let filecontent = await readFile(script, 'utf8');

filecontent = filecontent
.replace(/\{pkgname\}/g, name)
.replace(/{completer}/g, completer)
// on Bash on windows, we need to make sure to remove any \r
.replace(/\r?\n/g, '\n');

const filecontent = getCompletionScript({ name, completer, shell })
debug('Writing completion script to', filename);
await mkdir(path.dirname(filename), { recursive: true });
await writeFile(filename, filecontent);
console.log('=> Wrote completion script to %s file', filename);
Expand Down Expand Up @@ -417,6 +426,7 @@ module.exports = {
install,
uninstall,
checkFilenameForLine,
getCompletionScript,
writeToShellConfig,
writeToTabtabScript,
writeToCompletionScript,
Expand Down

0 comments on commit a8f2c87

Please sign in to comment.