Skip to content

Commit dea86d5

Browse files
author
Kent C. Dodds
committed
feat(help): Add case for no scripts and print scripts
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
1 parent 1363040 commit dea86d5

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/bin-utils.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,24 @@ function getArgs(args, rawArgs, scripts) {
3838

3939
function help({scripts}) {
4040
const availableScripts = getAvailableScripts(scripts)
41-
const scriptLines = availableScripts.map(({name, description}) => {
41+
const scriptLines = availableScripts.map(({name, description, script}) => {
4242
const coloredName = colors.green(name)
43-
if (!description) {
44-
return coloredName
43+
const coloredScript = colors.gray(script)
44+
let line
45+
if (description) {
46+
line = [coloredName, colors.white(description), coloredScript]
4547
} else {
46-
return [coloredName, colors.white(description)].join(' - ').trim()
48+
line = [coloredName, coloredScript]
4749
}
50+
return line.join(' - ').trim()
4851
})
49-
const topMessage = 'Available scripts (camel or kebab case accepted)'
50-
const message = `${topMessage}\n\n${scriptLines.join('\n')}`
51-
return message
52+
if (!scriptLines.length) {
53+
return colors.yellow('There are no scripts available')
54+
} else {
55+
const topMessage = 'Available scripts (camel or kebab case accepted)'
56+
const message = `${topMessage}\n\n${scriptLines.join('\n')}`
57+
return message
58+
}
5259
}
5360

5461
function getAvailableScripts(config, prefix) {
@@ -58,11 +65,11 @@ function getAvailableScripts(config, prefix) {
5865
if (contains(excluded, key)) {
5966
return scripts
6067
}
61-
const script = resolveScriptObjectToScript(val)
62-
if (script) {
63-
const {description} = script
68+
const scriptObj = resolveScriptObjectToScript(val)
69+
if (scriptObj) {
70+
const {description, script} = scriptObj
6471
const prefixed = prefix ? [prefix, key] : [key]
65-
scripts = [...scripts, {name: prefixed.join('.'), description}]
72+
scripts = [...scripts, {name: prefixed.join('.'), description, script}]
6673
}
6774
if (isPlainObject(val)) {
6875
return [...scripts, ...getAvailableScripts(val, key)]

src/bin-utils.test.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,19 @@ test('help: formats a nice message', t => {
5353
const expected = `
5454
Available scripts (camel or kebab case accepted)
5555
56-
${colors.green('foo')} - ${colors.white('the foo script')}
57-
${colors.green('bar')} - ${colors.white('stuff')}
58-
${colors.green('bar.baz')}
59-
${colors.green('bar.barBub')}
60-
${colors.green('foobar')}
56+
${colors.green('foo')} - ${colors.white('the foo script')} - ${colors.gray('echo "foo"')}
57+
${colors.green('bar')} - ${colors.white('stuff')} - ${colors.gray('echo "bar default"')}
58+
${colors.green('bar.baz')} - ${colors.gray('echo "baz"')}
59+
${colors.green('bar.barBub')} - ${colors.gray('echo "barBub"')}
60+
${colors.green('foobar')} - ${colors.gray('echo "foobar"')}
6161
`.trim()
6262

6363
t.is(message, expected)
6464
})
65+
66+
test('help: returns no scripts available', t => {
67+
const config = {scripts: {}}
68+
const message = help(config)
69+
const expected = colors.yellow('There are no scripts available')
70+
t.is(message, expected)
71+
})

0 commit comments

Comments
 (0)