-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip for run * fixes and refactor * fixed a bug and added tests * docs * Update pink-rabbits-punch.md * Update README.md * Update README.md Co-authored-by: Mitchell Hamilton <[email protected]>
- Loading branch information
1 parent
142b45d
commit 63cdae1
Showing
14 changed files
with
276 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
"@manypkg/cli": minor | ||
--- | ||
|
||
Added `manypkg run <partial package name or directory> <script>` which can be used to execute scripts for packages within a monorepo. | ||
|
||
As an example, let's say there are two packages: `@project/package-a` at `packages/pkg-a` and `@project/package-b` at `packages/pkg-a` which both have a `start` script, `manypkg run` can be used like this: | ||
|
||
```bash | ||
yarn manypkg run pkg-a start | ||
yarn manypkg run a start | ||
yarn manypkg run package-a start | ||
yarn manypkg run @project/package-a start | ||
yarn manypkg run packages/pkg-a start | ||
yarn manypkg run package-b start | ||
yarn manypkg run b start | ||
``` | ||
|
||
The following wouldn't work though because the `package` and `pkg` aren't unique among the package names/directories: | ||
|
||
```bash | ||
yarn manypkg run package start | ||
yarn manypkg run pkg start | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "@manypkg/basic-fixture", | ||
"version": "1.0.0", | ||
"workspaces": [ | ||
"packages/*" | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
__fixtures__/basic-with-scripts/packages/package-one/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@manypkg/basic-fixture-pkg-one", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"start": "echo hello start", | ||
"test": "echo testing" | ||
} | ||
} |
Empty file.
8 changes: 8 additions & 0 deletions
8
__fixtures__/basic-with-scripts/packages/package-two/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@manypkg/basic-fixture-pkg-two", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"start": "echo hello start", | ||
"test": "echo testing" | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Run command should execute "package start" and exit with 1: stderr 1`] = ` | ||
"Browserslist: caniuse-lite is outdated. Please run next command \`yarn upgrade\` | ||
☔️ error an identifier must only match a single package but \\"package\\" matches the following packages: | ||
☔️ error @manypkg/basic-fixture-pkg-one | ||
☔️ error @manypkg/basic-fixture-pkg-two | ||
" | ||
`; | ||
|
||
exports[`Run command should execute "package start" and exit with 1: stdout 1`] = `""`; | ||
|
||
exports[`Run command should execute "package-one start" and exit with 0: stderr 1`] = ` | ||
"Browserslist: caniuse-lite is outdated. Please run next command \`yarn upgrade\` | ||
warning package.json: No license field | ||
warning ../../package.json: No license field | ||
" | ||
`; | ||
|
||
exports[`Run command should execute "package-one start" and exit with 0: stdout 1`] = ` | ||
"$ echo hello start | ||
hello start | ||
" | ||
`; | ||
|
||
exports[`Run command should execute "package-one test" and exit with 0: stderr 1`] = ` | ||
"Browserslist: caniuse-lite is outdated. Please run next command \`yarn upgrade\` | ||
warning package.json: No license field | ||
warning ../../package.json: No license field | ||
" | ||
`; | ||
|
||
exports[`Run command should execute "package-one test" and exit with 0: stdout 1`] = ` | ||
"$ echo testing | ||
testing | ||
" | ||
`; | ||
|
||
exports[`Run command should execute "package-three start" and exit with 1: stderr 1`] = ` | ||
"Browserslist: caniuse-lite is outdated. Please run next command \`yarn upgrade\` | ||
☔️ error No matching packages found | ||
" | ||
`; | ||
|
||
exports[`Run command should execute "package-three start" and exit with 1: stdout 1`] = `""`; | ||
|
||
exports[`Run command should execute "package-two something" and exit with 1: stderr 1`] = ` | ||
"Browserslist: caniuse-lite is outdated. Please run next command \`yarn upgrade\` | ||
warning package.json: No license field | ||
warning ../../package.json: No license field | ||
error Command \\"something\\" not found. | ||
" | ||
`; | ||
|
||
exports[`Run command should execute "package-two something" and exit with 1: stdout 1`] = ` | ||
"info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. | ||
" | ||
`; | ||
|
||
exports[`Run command should execute "package-two start" and exit with 0: stderr 1`] = ` | ||
"Browserslist: caniuse-lite is outdated. Please run next command \`yarn upgrade\` | ||
warning package.json: No license field | ||
warning ../../package.json: No license field | ||
" | ||
`; | ||
|
||
exports[`Run command should execute "package-two start" and exit with 0: stdout 1`] = ` | ||
"$ echo hello start | ||
hello start | ||
" | ||
`; | ||
|
||
exports[`Run command should execute "pkg-one start" and exit with 0: stderr 1`] = ` | ||
"Browserslist: caniuse-lite is outdated. Please run next command \`yarn upgrade\` | ||
warning package.json: No license field | ||
warning ../../package.json: No license field | ||
" | ||
`; | ||
|
||
exports[`Run command should execute "pkg-one start" and exit with 0: stdout 1`] = ` | ||
"$ echo hello start | ||
hello start | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { runCmd } from "./run"; | ||
import fixturez from "fixturez"; | ||
import spawn from "spawndamnit"; | ||
import stripAnsi from "strip-ansi"; | ||
|
||
const f = fixturez(__dirname); | ||
|
||
describe("Run command", () => { | ||
it.each([ | ||
["package-one", "start", 0], | ||
["package-one", "test", 0], | ||
["package-two", "start", 0], | ||
["package", "start", 1], | ||
["package-two", "something", 1], | ||
["package-three", "start", 1], | ||
["pkg-one", "start", 0] | ||
// @ts-ignore | ||
])( | ||
'should execute "%s %s" and exit with %i', | ||
async (arg0, arg1, exitCode) => { | ||
const { stdout, stderr, code } = await spawn( | ||
require.resolve("../bin"), | ||
// @ts-ignore | ||
["run", arg0, arg1], | ||
{ | ||
cwd: f.find("basic-with-scripts") | ||
} | ||
); | ||
expect(code).toBe(exitCode); | ||
expect(stripAnsi(stdout.toString())).toMatchSnapshot("stdout"); | ||
expect(stripAnsi(stderr.toString())).toMatchSnapshot("stderr"); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import findWorkspacesRoot from "find-workspaces-root"; | ||
import getWorkspaces from "get-workspaces"; | ||
import path from "path"; | ||
import spawn from "spawndamnit"; | ||
import * as logger from "./logger"; | ||
import { ExitError } from "./errors"; | ||
|
||
export async function runCmd(args: string[], cwd: string) { | ||
let workspacesRoot = await findWorkspacesRoot(cwd); | ||
let workspaces = (await getWorkspaces({ | ||
cwd: workspacesRoot, | ||
tools: ["yarn", "bolt", "pnpm", "root"] | ||
}))!; | ||
|
||
const matchingWorkspaces = workspaces.filter(workspace => { | ||
return ( | ||
workspace.name.includes(args[0]) || | ||
path.relative(workspacesRoot, workspace.dir).includes(args[0]) | ||
); | ||
}); | ||
|
||
if (matchingWorkspaces.length > 1) { | ||
logger.error( | ||
`an identifier must only match a single package but "${ | ||
args[0] | ||
}" matches the following packages: \n${matchingWorkspaces | ||
.map(x => x.name) | ||
.join("\n")}` | ||
); | ||
throw new ExitError(1); | ||
} else if (matchingWorkspaces.length === 0) { | ||
logger.error("No matching packages found"); | ||
throw new ExitError(1); | ||
} else { | ||
const { code } = await spawn("yarn", args.slice(1), { | ||
cwd: matchingWorkspaces[0].dir, | ||
stdio: "inherit" | ||
}); | ||
throw new ExitError(code); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters