Skip to content

Commit

Permalink
Added ability to run script on all workspaces (#6244)
Browse files Browse the repository at this point in the history
* Added ability to run script on all workspaces

* Makes the execution synchronous
  • Loading branch information
kwelch authored and arcanis committed Aug 13, 2018
1 parent acf82e2 commit 53a6483
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
19 changes: 19 additions & 0 deletions __tests__/commands/workspaces.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// @flow
jest.mock('../../src/util/child');

import {BufferReporter} from '../../src/reporters/index.js';
import {run as workspace} from '../../src/cli/commands/workspaces.js';
import * as reporters from '../../src/reporters/index.js';
import Config from '../../src/config.js';
import path from 'path';
import {NODE_BIN_PATH, YARN_BIN_PATH} from '../../src/constants';

const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'workspace');
const spawn: $FlowFixMe = require('../../src/util/child').spawn;

beforeEach(() => spawn.mockClear());

async function runWorkspaces(
flags: Object,
Expand Down Expand Up @@ -42,3 +47,17 @@ test('workspaces info should list the workspaces', (): Promise<void> => {
});
});
});

test('workspaces run should spawn command for each workspace', (): Promise<void> => {
const originalArgs = ['run', 'script', 'arg1', '--flag1'];
return runWorkspaces({originalArgs}, ['run', 'script', 'arg1', '--flag1'], 'run-basic', config => {
expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'script', 'arg1', '--flag1'], {
stdio: 'inherit',
cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-1'),
});
expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'script', 'arg1', '--flag1'], {
stdio: 'inherit',
cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-2'),
});
});
});
33 changes: 33 additions & 0 deletions src/cli/commands/workspaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {MessageError} from '../../errors.js';
import type {Reporter} from '../../reporters/index.js';
import buildSubCommands from './_build-sub-commands.js';
import {DEPENDENCY_TYPES} from '../../constants.js';
import * as child from '../../util/child.js';
import {NODE_BIN_PATH, YARN_BIN_PATH} from '../../constants';

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

export async function runScript(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
const {workspaceRootFolder} = config;

if (!workspaceRootFolder) {
throw new MessageError(reporter.lang('workspaceRootNotFound', config.cwd));
}

const manifest = await config.findManifest(workspaceRootFolder, false);
invariant(manifest && manifest.workspaces, 'We must find a manifest with a "workspaces" property');

const workspaces = await config.resolveWorkspaces(workspaceRootFolder, manifest);

try {
const [_, ...rest] = flags.originalArgs || [];

for (const workspaceName of Object.keys(workspaces)) {
const {loc} = workspaces[workspaceName];

await child.spawn(NODE_BIN_PATH, [YARN_BIN_PATH, ...rest], {
stdio: 'inherit',
cwd: loc,
});
}
} catch (err) {
throw err;
}
}

const {run, setFlags, examples} = buildSubCommands('workspaces', {
async info(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
await info(config, reporter, flags, args);
},
async run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
await runScript(config, reporter, flags, args);
},
});

export {run, setFlags, examples};

0 comments on commit 53a6483

Please sign in to comment.