Skip to content

Commit 02f7de6

Browse files
maxchehabBYK
authored andcommitted
feat(run): Show a "no bin scripts" or "no scripts available" message (#4891)
**Summary** This adds the additional warning if there are no bin scripts or if there are no scripts available. If no scripts are available, scripts will not be listed. I created this pull request when running `yarn run` without any scripts specified, yarn would try to ask the user to specify which script. To view this feature in the CLI, run `yarn run` inside a project without any bin or scripts available. For example: ```bash $ yarn run yarn run v1.3.2 error No command specified. error There are no binary scripts available. error There are no scripts specified inside package.json. Done in 0.13s. ``` **Test plan** This new functionality is tested with the `returns noScriptsAvailable and noBinAvailable with no bins and scripts` test. This test checks if the implemented error messages are thrown.
1 parent 9ccac48 commit 02f7de6

File tree

7 files changed

+104
-6
lines changed

7 files changed

+104
-6
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`returns noBinAvailable with no bins 1`] = `
4+
Array [
5+
Object {
6+
"data": "No command specified.",
7+
"error": true,
8+
"type": "error",
9+
},
10+
Object {
11+
"data": "There are no binary scripts available.",
12+
"error": true,
13+
"type": "error",
14+
},
15+
Object {
16+
"data": "Project commands",
17+
"error": false,
18+
"type": "info",
19+
},
20+
Object {
21+
"data": Object {
22+
"hints": Object {
23+
"build": "echo 'building'",
24+
"prestart": "echo 'prestart'",
25+
"start": "node index.js",
26+
},
27+
"items": Array [
28+
"build",
29+
"prestart",
30+
"start",
31+
],
32+
"type": "possibleCommands",
33+
},
34+
"error": false,
35+
"type": "list",
36+
},
37+
Object {
38+
"data": "No command specified.",
39+
"error": true,
40+
"type": "error",
41+
},
42+
]
43+
`;
44+
45+
exports[`returns noScriptsAvailable with no scripts 1`] = `
46+
Array [
47+
Object {
48+
"data": "No command specified.",
49+
"error": true,
50+
"type": "error",
51+
},
52+
Object {
53+
"data": "Commands available from binary scripts: cat-names",
54+
"error": false,
55+
"type": "info",
56+
},
57+
Object {
58+
"data": "There are no scripts specified inside package.json.",
59+
"error": true,
60+
"type": "error",
61+
},
62+
]
63+
`;

__tests__/commands/run.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ test('adds quotes if args have spaces and quotes', (): Promise<void> => {
145145
});
146146
});
147147

148+
test('returns noScriptsAvailable with no scripts', (): Promise<void> => {
149+
return runRun([], {}, 'no-scripts', (config, reporter) => {
150+
expect(reporter.getBuffer()).toMatchSnapshot();
151+
});
152+
});
153+
154+
test('returns noBinAvailable with no bins', (): Promise<void> => {
155+
return runRun([], {}, 'no-bin', (config, reporter) => {
156+
expect(reporter.getBuffer()).toMatchSnapshot();
157+
});
158+
});
159+
148160
test('adds workspace root node_modules/.bin to path when in a workspace', (): Promise<void> => {
149161
return runRunInWorkspacePackage('packages/pkg1', ['env'], {}, 'workspace', (config, reporter): ?Promise<void> => {
150162
const logEntry = reporter.getBuffer().find(entry => entry.type === 'log');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"license": "MIT",
3+
"scripts": {
4+
"build": "echo 'building'",
5+
"start": "node index.js",
6+
"prestart": "echo 'prestart'"
7+
}
8+
}

__tests__/fixtures/run/no-scripts/node_modules/.bin/cat-names

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"license": "MIT"
3+
}

src/cli/commands/run.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,22 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg
104104
// list possible scripts if none specified
105105
if (args.length === 0) {
106106
reporter.error(reporter.lang('commandNotSpecified'));
107-
reporter.info(`${reporter.lang('binCommands') + binCommands.join(', ')}`);
108-
reporter.info(`${reporter.lang('possibleCommands')}`);
109-
reporter.list('possibleCommands', pkgCommands, cmdHints);
110-
await reporter
111-
.question(reporter.lang('commandQuestion'))
112-
.then(answer => runCommand(answer.split(' ')), () => reporter.error(reporter.lang('commandNotSpecified')));
107+
108+
if (binCommands.length) {
109+
reporter.info(`${reporter.lang('binCommands') + binCommands.join(', ')}`);
110+
} else {
111+
reporter.error(reporter.lang('noBinAvailable'));
112+
}
113+
114+
if (pkgCommands.length) {
115+
reporter.info(`${reporter.lang('possibleCommands')}`);
116+
reporter.list('possibleCommands', pkgCommands, cmdHints);
117+
await reporter
118+
.question(reporter.lang('commandQuestion'))
119+
.then(answer => runCommand(answer.split(' ')), () => reporter.error(reporter.lang('commandNotSpecified')));
120+
} else {
121+
reporter.error(reporter.lang('noScriptsAvailable'));
122+
}
113123
return Promise.resolve();
114124
} else {
115125
return runCommand(args);

src/reporters/lang/en.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ const messages = {
199199

200200
execMissingCommand: 'Missing command name.',
201201

202+
noScriptsAvailable: 'There are no scripts specified inside package.json.',
203+
noBinAvailable: 'There are no binary scripts available.',
202204
dashDashDeprecation: `From Yarn 1.0 onwards, scripts don't require "--" for options to be forwarded. In a future version, any explicit "--" will be forwarded as-is to the scripts.`,
203205
commandNotSpecified: 'No command specified.',
204206
binCommands: 'Commands available from binary scripts: ',

0 commit comments

Comments
 (0)