Skip to content

Commit

Permalink
fix(run): add double quotes around script target containing colon (#3218
Browse files Browse the repository at this point in the history
)
  • Loading branch information
brees-worth committed Jul 6, 2022
1 parent 041f581 commit ead461e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"scripts": {
"fail": "exit 1",
"my-script": "echo package-1"
"my-script": "echo package-1",
"another-script:but-with-colons": "echo package-1-script-with-colons"
}
}
}
7 changes: 7 additions & 0 deletions commands/run/__tests__/run-command.test.js
Expand Up @@ -331,6 +331,13 @@ describe("RunCommand", () => {
expect(collectedOutput).toContain("Successfully ran target");
});

it("runs a script with a colon in the script name", async () => {
collectedOutput = "";
await lernaRun(testDir)("another-script:but-with-colons");
expect(collectedOutput).toContain("package-1-script-with-colons");
expect(collectedOutput).toContain("Successfully ran target");
});

it("runs a script only in scoped packages", async () => {
collectedOutput = "";
await lernaRun(testDir)("my-script", "--scope", "package-1");
Expand Down
14 changes: 13 additions & 1 deletion commands/run/index.js
Expand Up @@ -170,6 +170,15 @@ class RunCommand extends Command {
return chain;
}

addQuotesAroundScriptNameIfItHasAColon(scriptName) {
// Nx requires quotes around script names of the form script:name
if (scriptName.includes(":")) {
return `"${scriptName}"`;
} else {
return scriptName;
}
}

runScriptsUsingNx() {
if (this.options.ci) {
process.env.CI = "true";
Expand All @@ -179,7 +188,10 @@ class RunCommand extends Command {
const { targetDependencies, options } = this.prepNxOptions();
if (this.packagesWithScript.length === 1) {
const { runOne } = require("nx/src/command-line/run-one");
const fullQualifiedTarget = this.packagesWithScript.map((p) => p.name)[0] + ":" + this.script;
const fullQualifiedTarget =
this.packagesWithScript.map((p) => p.name)[0] +
":" +
this.addQuotesAroundScriptNameIfItHasAColon(this.script);
return runOne(
process.cwd(),
{
Expand Down
102 changes: 102 additions & 0 deletions e2e/tests/lerna-run/lerna-run.spec.ts
Expand Up @@ -310,6 +310,22 @@ describe("useNx", () => {
"print-name": "echo test-package-3",
},
});
await fixture.lerna("create package-4 -y");
await fixture.addScriptsToPackage({
packagePath: "packages/package-4",
scripts: {
"print:name": "echo test-package-4",
"print-name-run-one-only": "echo test-package-4-run-one-only",
"print:name:run-one-only": "echo test-package-4-run-one-only-with-colon",
},
});
await fixture.lerna("create package-5 -y");
await fixture.addScriptsToPackage({
packagePath: "packages/package-5",
scripts: {
"print:name": "echo test-package-5",
},
});
});
afterAll(() => fixture.destroy());

Expand Down Expand Up @@ -355,6 +371,92 @@ test-package-X
> Lerna (powered by Nx) Successfully ran target print-name for 3 projects
lerna notice cli v999.9.9-e2e.0
`);
});

describe("run one", () => {
it("should run script on single child package using nx", async () => {
const output = await fixture.lerna(`run print-name-run-one-only`);

expect(output.combinedOutput).toMatchInlineSnapshot(`
> package-X:print-name-run-one-only
> package-X@0.0.0 print-name-run-one-only
> echo test-package-X-run-one-only
test-package-X-run-one-only
> Lerna (powered by Nx) Successfully ran target print-name-run-one-only for project package-X
lerna notice cli v999.9.9-e2e.0
`);
});

it("should run script with colon on single child package using nx", async () => {
const output = await fixture.lerna(`run print:name:run-one-only`);

expect(output.combinedOutput).toMatchInlineSnapshot(`
> package-X:"print:name:run-one-only"
> package-X@0.0.0 print:name:run-one-only
> echo test-package-X-run-one-only-with-colon
test-package-X-run-one-only-with-colon
> Lerna (powered by Nx) Successfully ran target print:name:run-one-only for project package-X
lerna notice cli v999.9.9-e2e.0
`);
});
});

it("should run script with colon on all child package using nx", async () => {
const output = await fixture.lerna(`run print:name`);

expect(output.combinedOutput).toMatchInlineSnapshot(`
> Lerna (powered by Nx) Running target print:name for 2 project(s):
- package-X
- package-X
> package-X:"print:name"
> package-X@0.0.0 print:name
> echo test-package-X
test-package-X
> package-X:"print:name"
> package-X@0.0.0 print:name
> echo test-package-X
test-package-X
> Lerna (powered by Nx) Successfully ran target print:name for 2 projects
lerna notice cli v999.9.9-e2e.0
`);
Expand Down

0 comments on commit ead461e

Please sign in to comment.