Skip to content

Commit

Permalink
fix(workspace) Workspace pass args 5301 (yarnpkg#5329)
Browse files Browse the repository at this point in the history
* fix(workspaces): Workspace command will now forward all cmd line args to child process.

Previously only plain args were passed, but not flags or any args with a "-". Now all args will be
passed.

yarnpkg#5301

* Add unit test for workspace command to pass arguments
  • Loading branch information
rally25rs authored and bestander committed Feb 7, 2018
1 parent 1b4b318 commit 0fce70a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 15 additions & 1 deletion __tests__/commands/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,25 @@ async function runWorkspace(
}
}

// The unit tests don't use commander.js for argument parsing.
// `rawArgs` is normally passed by commander.js so we just simulate it in the tests.

test('workspace run command', (): Promise<void> => {
return runWorkspace({}, ['workspace-1', 'run', 'script'], 'run-basic', config => {
const rawArgs = ['/path/to/node', '/path/to/yarn', 'workspace', 'workspace-1', 'run', 'script'];
return runWorkspace({rawArgs}, ['workspace-1', 'run', 'script'], 'run-basic', config => {
expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'run', 'script'], {
stdio: 'inherit',
cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-1'),
});
});
});

test('workspace run command forwards raw arguments', (): Promise<void> => {
const rawArgs = ['/path/to/node', '/path/to/yarn', 'workspace', 'workspace-1', 'run', 'script', 'arg1', '--flag1'];
return runWorkspace({rawArgs}, ['workspace-1', 'run', 'script'], 'run-basic', config => {
expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'run', 'script', 'arg1', '--flag1'], {
stdio: 'inherit',
cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-1'),
});
});
});
4 changes: 3 additions & 1 deletion src/cli/commands/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg

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

const [workspaceName, ...rest] = args;
const [workspaceName] = args;
// rawArgs contains: [nodePath, yarnPath, 'workspace', workspaceName, ...]
const [, , , , ...rest] = flags.rawArgs || [];

if (!Object.prototype.hasOwnProperty.call(workspaces, workspaceName)) {
throw new MessageError(reporter.lang('workspaceUnknownWorkspace', workspaceName));
Expand Down

0 comments on commit 0fce70a

Please sign in to comment.