Skip to content

Commit

Permalink
refactor: rename the template files (#14)
Browse files Browse the repository at this point in the history
* fix: add begin and end

just like the others

* refactor: rename the template files

* rename `scripts` directory to `templates`
* rename the files to `completion.${ext}`

* test: getCompletionScript on pwsh

* refactor: remove unused import

* refactor: remove TABTAB_FILE_NAME
  • Loading branch information
KSXGitHub authored Jan 31, 2024
1 parent 5e98f4c commit 3bdc487
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 26 deletions.
16 changes: 0 additions & 16 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,19 @@ const SHELL_LOCATIONS = {
pwsh: '~/Documents/PowerShell/Microsoft.PowerShell_profile.ps1'
};

const TEMPLATE_FILE_NAME = {
bash: 'bash.sh',
fish: 'fish.sh',
pwsh: 'pwsh.ps1',
zsh: 'zsh.sh',
};

const COMPLETION_FILE_EXT = {
bash: 'bash',
fish: 'fish',
pwsh: 'ps1',
zsh: 'zsh',
};

const TABTAB_FILE_NAME = {
bash: '__tabtab.bash',
fish: '__tabtab.fish',
pwsh: '__tabtab.ps1',
zsh: '__tabtab.zsh',
};

module.exports = {
BASH_LOCATION,
ZSH_LOCATION,
FISH_LOCATION,
PWSH_LOCATION,
COMPLETION_DIR,
SHELL_LOCATIONS,
TEMPLATE_FILE_NAME,
COMPLETION_FILE_EXT,
TABTAB_FILE_NAME,
};
14 changes: 7 additions & 7 deletions lib/filename.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const { TEMPLATE_FILE_NAME, COMPLETION_FILE_EXT, TABTAB_FILE_NAME } = require('./constants');
const { COMPLETION_FILE_EXT } = require('./constants');

/**
* Get a template file name for the SHELL provided.
* @param {String} shell
* @returns {String}
*/
const templateFileName = (shell = systemShell()) => {
const filename = TEMPLATE_FILE_NAME[shell];
if (!filename) {
const ext = COMPLETION_FILE_EXT[shell];
if (!ext) {
throw new Error(`Unsupported shell: ${shell}`);
}
return filename;
return `completion.${ext}`;
};

/**
Expand All @@ -33,11 +33,11 @@ const completionFileName = (name, shell = systemShell()) => {
* @returns {String}
*/
const tabtabFileName = (shell = systemShell()) => {
const filename = TABTAB_FILE_NAME[shell];
if (!filename) {
const ext = COMPLETION_FILE_EXT[shell];
if (!ext) {
throw new Error(`Unsupported shell: ${shell}`);
}
return filename;
return `__tabtab.${ext}`;
};

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const {
* @param {String} shell - Shell to base the check on, defaults to system shell.
* @returns {String} The template script content, defaults to Bash for shell we don't know yet
*/
const scriptFromShell = (shell = systemShell()) => path.join(__dirname, 'scripts', templateFileName(shell));
const scriptFromShell = (shell = systemShell()) => path.join(__dirname, 'templates', templateFileName(shell));

/**
* Helper to return the expected location for SHELL config file, based on the
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 14 additions & 2 deletions test/getCompletionScript.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const { getCompletionScript } = require('..');

Expand All @@ -11,11 +10,24 @@ describe('getCompletionScript gets the right completion script for', () => {
completer: 'foo-complete',
shell
});
const expected = fs.readFileSync(require.resolve(`../lib/scripts/${shell}.sh`), 'utf8')
const expected = fs.readFileSync(require.resolve(`../lib/templates/completion.${shell}`), 'utf8')
.replace(/\{pkgname\}/g, 'foo')
.replace(/{completer}/g, 'foo-complete')
.replace(/\r?\n/g, '\n');
assert.equal(received, expected);
});
}

it('pwsh', async () => {
const received = await getCompletionScript({
name: 'foo',
completer: 'foo-complete',
shell: 'pwsh'
});
const expected = fs.readFileSync(require.resolve(`../lib/templates/completion.ps1`), 'utf8')
.replace(/\{pkgname\}/g, 'foo')
.replace(/{completer}/g, 'foo-complete')
.replace(/\r?\n/g, '\n');
assert.equal(received, expected);
});
});

0 comments on commit 3bdc487

Please sign in to comment.