Skip to content

Commit

Permalink
Merge pull request #246 from ghiscoding/bugfix/nx-run-escape-quotes-s…
Browse files Browse the repository at this point in the history
…cript-with-colon

fix(run): add double quotes around script target containing colon
  • Loading branch information
ghiscoding committed Jul 6, 2022
2 parents 34a2574 + 18da175 commit 50d7547
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/run/package.json
Expand Up @@ -43,6 +43,8 @@
"devDependencies": {
"@types/execa": "^2.0.0",
"@types/p-map": "^2.0.0",
"globby": "^11.1.0",
"jest-circus": "^28.1.2",
"perf_hooks": "^0.0.1",
"yargs-parser": "^21.0.1"
}
Expand Down
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 packages/run/src/__tests__/run-command.spec.ts
Expand Up @@ -383,6 +383,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
8 changes: 7 additions & 1 deletion packages/run/src/run-command.ts
Expand Up @@ -205,6 +205,11 @@ export class RunCommand extends Command<RunCommandOption & FilterOptions> {
return chain;
}

/** Nx requires quotes around script names of the form script:name*/
escapeScriptNameQuotes(scriptName: string) {
return scriptName.includes(':') ? `"${scriptName}"` : scriptName;
}

async runScriptsUsingNx() {
if (this.options.ci) {
process.env.CI = 'true';
Expand All @@ -214,7 +219,8 @@ export class RunCommand extends Command<RunCommandOption & FilterOptions> {
const { targetDependencies, options } = await this.prepNxOptions();
if (this.packagesWithScript.length === 1) {
const { runOne } = await import('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.escapeScriptNameQuotes(this.script);
return (runOne as any)(
process.cwd(),
{
Expand Down
6 changes: 5 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 50d7547

Please sign in to comment.