Skip to content

Commit

Permalink
Add exec command (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown authored Oct 8, 2019
1 parent 4dc2780 commit 801a468
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions .changeset/wet-geese-drop/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "releases": [{ "name": "@manypkg/cli", "type": "minor" }], "dependents": [] }
1 change: 1 addition & 0 deletions .changeset/wet-geese-drop/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add exec command that runs a command in every workspace
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"fs-extra": "^8.1.0",
"get-workspaces": "^0.4.0",
"meow": "^5.0.0",
"p-limit": "^2.2.1",
"sembear": "^0.4.0",
"semver": "^6.3.0",
"spawndamnit": "^2.0.0",
Expand All @@ -31,4 +32,4 @@
"script-runner"
]
}
}
}
31 changes: 30 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { checks } from "./checks";
import { ExitError } from "./errors";
import { writeWorkspace } from "./utils";
import spawn from "spawndamnit";
import pLimit from "p-limit";

let runChecks = (
allWorkspaces: Map<string, Workspace>,
Expand Down Expand Up @@ -57,10 +58,38 @@ let runChecks = (
return { requiresInstall, hasErrored };
};

let execLimit = pLimit(4);

async function execCmd(args: string[]) {
let workspacesRoot = await findWorkspacesRoot(process.cwd());
let workspaces = (await getWorkspaces({
cwd: workspacesRoot,
tools: ["yarn", "bolt", "root"]
}))!;
let highestExitCode = 0;
await Promise.all(
workspaces.map(workspace => {
return execLimit(async () => {
let { code } = await spawn(args[0], args.slice(1), {
cwd: workspace.dir,
stdio: "inherit"
});
highestExitCode = Math.max(code, highestExitCode);
});
})
);
throw new ExitError(highestExitCode);
}

(async () => {
let things = process.argv.slice(2);
if (things[0] === "exec") {
return execCmd(things.slice(1));
}
if (things[0] !== "check" && things[0] !== "fix") {
logger.error(`command ${things[0]} not found, only check and fix exist`);
logger.error(
`command ${things[0]} not found, only check, exec and fix exist`
);
throw new ExitError(1);
}
let shouldFix = things[0] === "fix";
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4036,6 +4036,13 @@ p-limit@^2.0.0, p-limit@^2.2.0:
dependencies:
p-try "^2.0.0"

p-limit@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.2.1.tgz#aa07a788cc3151c939b5131f63570f0dd2009537"
integrity sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==
dependencies:
p-try "^2.0.0"

p-locate@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43"
Expand Down

0 comments on commit 801a468

Please sign in to comment.