Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(run): add double quotes around script target containing colon #3218

Merged
merged 8 commits into from Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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