Skip to content

Commit

Permalink
feat(help): Add case for no scripts and print scripts
Browse files Browse the repository at this point in the history
If no `package-scripts.js` (or `-c` file) is found, then we will
indicate that there are no scripts available.

Also, printing the actual script as that could probably be pretty
helpful
  • Loading branch information
Kent C. Dodds committed Apr 27, 2016
1 parent 1363040 commit dea86d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
29 changes: 18 additions & 11 deletions src/bin-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,24 @@ function getArgs(args, rawArgs, scripts) {

function help({scripts}) {
const availableScripts = getAvailableScripts(scripts)
const scriptLines = availableScripts.map(({name, description}) => {
const scriptLines = availableScripts.map(({name, description, script}) => {
const coloredName = colors.green(name)
if (!description) {
return coloredName
const coloredScript = colors.gray(script)
let line
if (description) {
line = [coloredName, colors.white(description), coloredScript]
} else {
return [coloredName, colors.white(description)].join(' - ').trim()
line = [coloredName, coloredScript]
}
return line.join(' - ').trim()
})
const topMessage = 'Available scripts (camel or kebab case accepted)'
const message = `${topMessage}\n\n${scriptLines.join('\n')}`
return message
if (!scriptLines.length) {
return colors.yellow('There are no scripts available')
} else {
const topMessage = 'Available scripts (camel or kebab case accepted)'
const message = `${topMessage}\n\n${scriptLines.join('\n')}`
return message
}
}

function getAvailableScripts(config, prefix) {
Expand All @@ -58,11 +65,11 @@ function getAvailableScripts(config, prefix) {
if (contains(excluded, key)) {
return scripts
}
const script = resolveScriptObjectToScript(val)
if (script) {
const {description} = script
const scriptObj = resolveScriptObjectToScript(val)
if (scriptObj) {
const {description, script} = scriptObj
const prefixed = prefix ? [prefix, key] : [key]
scripts = [...scripts, {name: prefixed.join('.'), description}]
scripts = [...scripts, {name: prefixed.join('.'), description, script}]
}
if (isPlainObject(val)) {
return [...scripts, ...getAvailableScripts(val, key)]
Expand Down
17 changes: 12 additions & 5 deletions src/bin-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ test('help: formats a nice message', t => {
const expected = `
Available scripts (camel or kebab case accepted)
${colors.green('foo')} - ${colors.white('the foo script')}
${colors.green('bar')} - ${colors.white('stuff')}
${colors.green('bar.baz')}
${colors.green('bar.barBub')}
${colors.green('foobar')}
${colors.green('foo')} - ${colors.white('the foo script')} - ${colors.gray('echo "foo"')}
${colors.green('bar')} - ${colors.white('stuff')} - ${colors.gray('echo "bar default"')}
${colors.green('bar.baz')} - ${colors.gray('echo "baz"')}
${colors.green('bar.barBub')} - ${colors.gray('echo "barBub"')}
${colors.green('foobar')} - ${colors.gray('echo "foobar"')}
`.trim()

t.is(message, expected)
})

test('help: returns no scripts available', t => {
const config = {scripts: {}}
const message = help(config)
const expected = colors.yellow('There are no scripts available')
t.is(message, expected)
})

0 comments on commit dea86d5

Please sign in to comment.