Skip to content

Commit b7523cd

Browse files
committed
Add pyarn workspace run
1 parent 02f26db commit b7523cd

4 files changed

Lines changed: 41 additions & 11 deletions

File tree

‎src/Project.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,10 @@ export default class Project {
144144

145145
await Promise.all(promises);
146146
}
147+
148+
getWorkspaceByName(workspaces: Array<Workspace>, workspaceName: string) {
149+
return workspaces.find(workspace => {
150+
return workspace.pkg.config.name === workspaceName;
151+
});
152+
}
147153
}

‎src/cli.js‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,18 @@ function runCommandFromCli(args: options.Args, flags: options.Flags) {
237237
} else if (commandMap.WHY[command]) {
238238
return commands.why(commands.toWhyOptions(commandArgs, flags));
239239
} else if (commandMap.WORKSPACE[command]) {
240-
if (commandMap.WORKSPACE_ADD[subCommand]) {
241-
return commands.workspaceAdd(commands.toWorkspaceAddOptions(subCommandArgs, flags));
242-
} else if (commandMap.WORKSPACE_REMOVE[subCommand]) {
243-
return commands.workspaceRemove(commands.toWorkspaceRemoveOptions(subCommandArgs, flags));
244-
} else if (commandMap.WORKSPACE_RUN[subCommand] || typeof subCommand !== 'undefined') {
245-
return commands.workspaceRun(commands.toWorkspaceRunOptions(subCommandArgs, flags));
246-
} else if (commandMap.WORKSPACE_UPGRADE[subCommand]) {
247-
return commands.workspaceUpgrade(commands.toWorkspaceUpgradeOptions(subCommandArgs, flags));
240+
let [workspaceCommand, ...rest] = subCommandArgs;
241+
let workspaceArgs = [subCommand, ...rest];
242+
if (commandMap.WORKSPACE_ADD[workspaceCommand]) {
243+
return commands.workspaceAdd(commands.toWorkspaceAddOptions(workspaceArgs, flags));
244+
} else if (commandMap.WORKSPACE_REMOVE[workspaceCommand]) {
245+
return commands.workspaceRemove(commands.toWorkspaceRemoveOptions(workspaceArgs, flags));
246+
} else if (commandMap.WORKSPACE_RUN[workspaceCommand] || typeof subCommand !== 'undefined') {
247+
return commands.workspaceRun(commands.toWorkspaceRunOptions(workspaceArgs, flags));
248+
} else if (commandMap.WORKSPACE_UPGRADE[workspaceCommand]) {
249+
return commands.workspaceUpgrade(commands.toWorkspaceUpgradeOptions(workspaceArgs, flags));
248250
} else {
249-
return commands.workspaceRun(commands.toWorkspaceRunOptions(commandArgs, flags));
251+
return commands.workspaceRun(commands.toWorkspaceRunOptions([subCommand, ...subCommandArgs], flags));
250252
}
251253
} else if (commandMap.WORKSPACES[command]) {
252254
if (commandMap.WORKSPACES_ADD[subCommand]) {

‎src/commands/run.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ export async function run(opts: RunOptions) {
3232
let validScript = await yarn.run(pkg, opts.script, opts.scriptArgs);
3333

3434
if (!validScript) {
35-
throw new PError(`Package at "${pkgPath}" does not have a script named "${opts.script}"`);
35+
throw new PError(`Package at "${pkg.dir}" does not have a script named "${opts.script}"`);
3636
}
3737
}

‎src/commands/workspace/run.js‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
// @flow
2+
import Project from '../../Project';
23
import * as options from '../../utils/options';
4+
import * as yarn from '../../utils/yarn';
35
import {PError} from '../../utils/errors';
46

57
export type WorkspaceRunOptions = {
68
cwd?: string,
9+
workspaceName: string,
10+
script: string,
11+
scriptArgs: options.Args,
712
};
813

914
export function toWorkspaceRunOptions(args: options.Args, flags: options.Flags): WorkspaceRunOptions {
15+
let [workspaceName, script, ...scriptArgs] = args;
1016
return {
1117
cwd: options.string(flags.cwd, 'cwd'),
18+
workspaceName,
19+
script,
20+
scriptArgs,
1221
};
1322
}
1423

1524
export async function workspaceRun(opts: WorkspaceRunOptions) {
16-
throw new PError('Unimplemented command "workspace run"');
25+
let cwd = opts.cwd || process.cwd();
26+
let project = await Project.init(cwd);
27+
let workspaces = await project.getWorkspaces();
28+
let workspace = await project.getWorkspaceByName(workspaces, opts.workspaceName);
29+
30+
if (!workspace) {
31+
throw new PError(`Could not find a workspace named "${opts.workspaceName}" from "${cwd}"`);
32+
}
33+
34+
let validScript = await yarn.run(workspace.pkg, opts.script, opts.scriptArgs);
35+
36+
if (!validScript) {
37+
throw new PError(`Package at "${workspace.pkg.dir}" does not have a script named "${opts.script}"`);
38+
}
1739
}

0 commit comments

Comments
 (0)