Skip to content

Commit

Permalink
fix: use correct completion command in generated completion script (#988
Browse files Browse the repository at this point in the history
)
  • Loading branch information
wKovacs64 authored and bcoe committed Oct 18, 2017
1 parent 8515e4f commit 3c8ac1d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions completion.sh.hbs
Expand Up @@ -2,8 +2,8 @@
#
# yargs command completion script
#
# Installation: {{app_path}} completion >> ~/.bashrc
# or {{app_path}} completion >> ~/.bash_profile on OSX.
# Installation: {{app_path}} {{completion_command}} >> ~/.bashrc
# or {{app_path}} {{completion_command}} >> ~/.bash_profile on OSX.
#
_yargs_completions()
{
Expand Down
3 changes: 2 additions & 1 deletion lib/completion.js
Expand Up @@ -78,7 +78,7 @@ module.exports = function completion (yargs, usage, command) {
}

// generate the completion script to add to your .bashrc.
self.generateCompletionScript = function generateCompletionScript ($0) {
self.generateCompletionScript = function generateCompletionScript ($0, cmd) {
let script = fs.readFileSync(
path.resolve(__dirname, '../completion.sh.hbs'),
'utf-8'
Expand All @@ -89,6 +89,7 @@ module.exports = function completion (yargs, usage, command) {
if ($0.match(/\.js$/)) $0 = `./${$0}`

script = script.replace(/{{app_name}}/g, name)
script = script.replace(/{{completion_command}}/g, cmd)
return script.replace(/{{app_path}}/g, $0)
}

Expand Down
7 changes: 7 additions & 0 deletions test/completion.js
Expand Up @@ -172,6 +172,13 @@ describe('Completion', () => {
r.logs[0].should.match(/ndm --get-yargs-completions/)
})

it('replaces completion command variable with custom completion command in script', () => {
const r = checkUsage(() => yargs([]).completion('flintlock')
.showCompletionScript(), ['ndm'])

r.logs[0].should.match(/ndm flintlock >>/)
})

it('if $0 has a .js extension, a ./ prefix is added', () => {
const r = checkUsage(() => yargs([])
.showCompletionScript(), ['test.js'])
Expand Down
2 changes: 1 addition & 1 deletion yargs.js
Expand Up @@ -861,7 +861,7 @@ function Yargs (processArgs, cwd, parentRequire) {
self.showCompletionScript = function ($0) {
argsert('[string]', [$0], arguments.length)
$0 = $0 || self.$0
_logger.log(completion.generateCompletionScript($0))
_logger.log(completion.generateCompletionScript($0, completionCommand))
return self
}

Expand Down

0 comments on commit 3c8ac1d

Please sign in to comment.