Skip to content

Commit 53a6483

Browse files
kwelcharcanis
authored andcommitted
Added ability to run script on all workspaces (#6244)
* Added ability to run script on all workspaces * Makes the execution synchronous
1 parent acf82e2 commit 53a6483

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

__tests__/commands/workspaces.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
// @flow
2+
jest.mock('../../src/util/child');
23

34
import {BufferReporter} from '../../src/reporters/index.js';
45
import {run as workspace} from '../../src/cli/commands/workspaces.js';
56
import * as reporters from '../../src/reporters/index.js';
67
import Config from '../../src/config.js';
78
import path from 'path';
9+
import {NODE_BIN_PATH, YARN_BIN_PATH} from '../../src/constants';
810

911
const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'workspace');
12+
const spawn: $FlowFixMe = require('../../src/util/child').spawn;
13+
14+
beforeEach(() => spawn.mockClear());
1015

1116
async function runWorkspaces(
1217
flags: Object,
@@ -42,3 +47,17 @@ test('workspaces info should list the workspaces', (): Promise<void> => {
4247
});
4348
});
4449
});
50+
51+
test('workspaces run should spawn command for each workspace', (): Promise<void> => {
52+
const originalArgs = ['run', 'script', 'arg1', '--flag1'];
53+
return runWorkspaces({originalArgs}, ['run', 'script', 'arg1', '--flag1'], 'run-basic', config => {
54+
expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'script', 'arg1', '--flag1'], {
55+
stdio: 'inherit',
56+
cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-1'),
57+
});
58+
expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'script', 'arg1', '--flag1'], {
59+
stdio: 'inherit',
60+
cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-2'),
61+
});
62+
});
63+
});

src/cli/commands/workspaces.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {MessageError} from '../../errors.js';
55
import type {Reporter} from '../../reporters/index.js';
66
import buildSubCommands from './_build-sub-commands.js';
77
import {DEPENDENCY_TYPES} from '../../constants.js';
8+
import * as child from '../../util/child.js';
9+
import {NODE_BIN_PATH, YARN_BIN_PATH} from '../../constants';
810

911
const invariant = require('invariant');
1012
const path = require('path');
@@ -60,10 +62,41 @@ export async function info(config: Config, reporter: Reporter, flags: Object, ar
6062
reporter.log(JSON.stringify(publicData, null, 2), {force: true});
6163
}
6264

65+
export async function runScript(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
66+
const {workspaceRootFolder} = config;
67+
68+
if (!workspaceRootFolder) {
69+
throw new MessageError(reporter.lang('workspaceRootNotFound', config.cwd));
70+
}
71+
72+
const manifest = await config.findManifest(workspaceRootFolder, false);
73+
invariant(manifest && manifest.workspaces, 'We must find a manifest with a "workspaces" property');
74+
75+
const workspaces = await config.resolveWorkspaces(workspaceRootFolder, manifest);
76+
77+
try {
78+
const [_, ...rest] = flags.originalArgs || [];
79+
80+
for (const workspaceName of Object.keys(workspaces)) {
81+
const {loc} = workspaces[workspaceName];
82+
83+
await child.spawn(NODE_BIN_PATH, [YARN_BIN_PATH, ...rest], {
84+
stdio: 'inherit',
85+
cwd: loc,
86+
});
87+
}
88+
} catch (err) {
89+
throw err;
90+
}
91+
}
92+
6393
const {run, setFlags, examples} = buildSubCommands('workspaces', {
6494
async info(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
6595
await info(config, reporter, flags, args);
6696
},
97+
async run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
98+
await runScript(config, reporter, flags, args);
99+
},
67100
});
68101

69102
export {run, setFlags, examples};

0 commit comments

Comments
 (0)