-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
162 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* eslint no-invalid-this:"off" */ | ||
import omelette from 'omelette' | ||
import {includes, isPlainObject, kebabCase} from 'lodash' | ||
|
||
const complete = omelette('nps <script>') | ||
|
||
export {autocomplete as default, install} | ||
|
||
function autocomplete(config = {}) { | ||
complete.on('script', onScript) | ||
complete.init() | ||
|
||
function onScript() { | ||
this.reply(getScripts(config.scripts)) | ||
} | ||
} | ||
|
||
function install(destination = '~/.bash_profile') { | ||
complete.setupShellInitFile(destination) | ||
return destination | ||
} | ||
|
||
function getScripts(scriptsObject, prefix = '', allScripts = []) { | ||
const excludedKeys = ['default', 'script', 'description'] | ||
return Object.keys(scriptsObject).reduce((acc, key) => { | ||
if (includes(excludedKeys, key)) { | ||
return acc | ||
} | ||
const value = scriptsObject[key] | ||
const kebabKey = kebabCase(key) | ||
const deepKey = prefix ? `${prefix}.${kebabKey}` : kebabKey | ||
acc.push(deepKey) | ||
if (isPlainObject(value)) { | ||
getScripts(value, deepKey, acc) | ||
} | ||
return acc | ||
}, allScripts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import test from 'ava' | ||
import {spy} from 'sinon' | ||
import proxyquire from 'proxyquire' | ||
|
||
test('inits omelette', t => { | ||
const {autocomplete, initSpy} = getAutocomplete() | ||
autocomplete() | ||
t.true(initSpy.calledOnce) | ||
}) | ||
|
||
test('calls this.reply with the available scripts', t => { | ||
const stubConfig = { | ||
scripts: { | ||
build: {default: {script: 'build'}, watch: 'build.watch', main: {umd: 'build.main.umd', default: 'build.main'}}, | ||
lint: {default: {script: 'lint', description: 'lint things'}, watch: 'lint.watch'}, | ||
test: 'test', | ||
camelCase: 'camelCase', | ||
cover: {description: 'this is a description', script: 'this is the script'}, | ||
}, | ||
} | ||
const expectedReplyArgs = [ | ||
'build', 'build.watch', 'build.main', 'build.main.umd', | ||
'lint', 'lint.watch', | ||
'test', 'camel-case', 'cover', | ||
] | ||
const {autocomplete, getReplyArgs} = getAutocomplete() | ||
autocomplete(stubConfig) | ||
const actualReplyArgs = getReplyArgs() | ||
t.deepEqual(actualReplyArgs, expectedReplyArgs) | ||
}) | ||
|
||
test('install calls setupShellInitFile with the given destination', t => { | ||
const {install, setupShellInitFileSpy} = getInstall() | ||
const destination = '~/.my_bash_profile' | ||
install(destination) | ||
const [actualDestination] = setupShellInitFileSpy.firstCall.args | ||
t.true(setupShellInitFileSpy.calledOnce) | ||
t.is(actualDestination, destination) | ||
}) | ||
|
||
test('install defaults to ~/.bash_profile', t => { | ||
const {install, setupShellInitFileSpy} = getInstall() | ||
const expectedDestination = '~/.bash_profile' | ||
install() | ||
const [actualDestination] = setupShellInitFileSpy.firstCall.args | ||
t.true(setupShellInitFileSpy.calledOnce) | ||
t.is(actualDestination, expectedDestination) | ||
}) | ||
|
||
function getAutocomplete() { | ||
const onSpy = spy() | ||
const initSpy = spy() | ||
const omelette = spy(() => { | ||
return {on: onSpy, init: initSpy} | ||
}) | ||
const autocomplete = proxyquire('./index', {omelette}).default | ||
return {autocomplete, getReplyArgs, initSpy} | ||
|
||
function getReplyArgs() { | ||
const [, replier] = onSpy.firstCall.args | ||
const reply = spy() | ||
const context = {reply} | ||
replier.call(context) | ||
return reply.firstCall.args[0] | ||
} | ||
} | ||
|
||
function getInstall() { | ||
const setupShellInitFileSpy = spy() | ||
const omelette = spy(() => ({setupShellInitFile: setupShellInitFileSpy})) | ||
const {install} = proxyquire('./index', {omelette}) | ||
return {install, setupShellInitFileSpy} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters