Skip to content

Commit e1fc017

Browse files
committed
Don't try to run scripts that aren't defined.
1 parent 526d9bf commit e1fc017

File tree

7 files changed

+47
-3
lines changed

7 files changed

+47
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Please add one entry in this file for each change in Yarn's behavior. Use the same format for all entries, including the third-person verb. Make sure you don't add more than one line of text to keep it clean. Thanks!
44

55
## Master
6+
- Stop `yarn workspaces run` error if command not defined in workspace
67

78
## 1.13.0
89

__tests__/commands/workspaces.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ test('workspaces info should list the workspaces', (): Promise<void> => {
4848
});
4949
});
5050

51-
test('workspaces run should spawn command for each workspace', (): Promise<void> => {
51+
test('workspaces run should spawn command for each workspace with that script defined', (): Promise<void> => {
5252
const originalArgs = ['run', 'script', 'arg1', '--flag1'];
5353
return runWorkspaces({originalArgs}, ['run', 'script', 'arg1', '--flag1'], 'run-basic', config => {
5454
expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'script', 'arg1', '--flag1'], {
@@ -61,3 +61,13 @@ test('workspaces run should spawn command for each workspace', (): Promise<void>
6161
});
6262
});
6363
});
64+
65+
test('workspaces run should not report error if command is not defined for a workspace', (): Promise<void> => {
66+
const originalArgs = ['run', 'workspace-1-only-script', 'arg1', '--flag1'];
67+
return runWorkspaces({originalArgs}, ['run', 'workspace-1-only-script', 'arg1', '--flag1'], 'run-basic', config => {
68+
expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'workspace-1-only-script', 'arg1', '--flag1'], {
69+
stdio: 'inherit',
70+
cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-1'),
71+
});
72+
});
73+
});

__tests__/fixtures/workspace/run-basic/packages/workspace-child-1/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"scripts": {
55
"prescript": "echo workspace-1 prescript",
66
"script": "echo workspace-1 script",
7-
"postscript": "echo workspace-1 postscript"
7+
"postscript": "echo workspace-1 postscript",
8+
"workspace-1-only-script": "echo workspace-1-only script"
89
}
910
}

packages/pkg-tests/pkg-tests-core/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
"super-resolve": "^1.0.0",
1111
"tar-fs": "^1.16.0",
1212
"tmp": "^0.0.33"
13+
},
14+
"scripts": {
15+
"script": "echo pkg-tests-core script"
1316
}
1417
}

src/cli/commands/workspaces.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,30 @@ export async function runScript(config: Config, reporter: Reporter, flags: Objec
7777
try {
7878
const [_, ...rest] = flags.originalArgs || [];
7979

80+
// first arg not prefixed by `-` is the script that will be run
81+
let scriptName = '';
82+
for (const arg of rest) {
83+
if (arg.startsWith('-')) {
84+
continue;
85+
}
86+
scriptName = arg;
87+
break;
88+
}
89+
8090
for (const workspaceName of Object.keys(workspaces)) {
81-
const {loc} = workspaces[workspaceName];
91+
const {loc, manifest} = workspaces[workspaceName];
92+
93+
// Let the user know what workspace we're running in...
94+
reporter.identifyWorkspace(workspaceName);
95+
96+
// ...and whether the script doesn't exist
97+
if (!manifest.scripts) {
98+
reporter.warn(`No scripts defined.`);
99+
continue;
100+
} else if (!manifest.scripts[scriptName]) {
101+
reporter.warn(`${scriptName} not defined.`);
102+
continue;
103+
}
82104

83105
await child.spawn(NODE_BIN_PATH, [YARN_BIN_PATH, ...rest], {
84106
stdio: 'inherit',

src/reporters/base-reporter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ export default class BaseReporter {
228228
// a table structure
229229
table(head: Array<string>, body: Array<Array<string>>) {}
230230

231+
// identify which workspace we're in
232+
identifyWorkspace(msg: string) {}
233+
231234
// security audit action to resolve advisories
232235
auditAction(recommendation: AuditActionRecommendation) {}
233236

src/reporters/console/console-reporter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ export default class ConsoleReporter extends BaseReporter {
159159
}
160160
}
161161

162+
identifyWorkspace(msg: string) {
163+
this.log(this.format.bold(`\n${msg}`));
164+
}
165+
162166
header(command: string, pkg: Package) {
163167
this.log(this.format.bold(`${pkg.name} ${command} v${pkg.version}`));
164168
}

0 commit comments

Comments
 (0)