Skip to content

Commit

Permalink
fix(devkit): parseTargetString should support targets with colons in …
Browse files Browse the repository at this point in the history
…the name (#10400)
  • Loading branch information
AgentEnder committed May 20, 2022
1 parent 99b4c60 commit e439718
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
25 changes: 25 additions & 0 deletions packages/devkit/src/executors/parse-target-string.spec.ts
@@ -0,0 +1,25 @@
import { parseTargetString, targetToTargetString } from './parse-target-string';

const cases = [
{ input: 'one:two', expected: { project: 'one', target: 'two' } },
{
input: 'one:two:three',
expected: { project: 'one', target: 'two', configuration: 'three' },
},
{
input: 'one:"two:two":three',
expected: { project: 'one', target: 'two:two', configuration: 'three' },
},
];

describe('parseTargetString', () => {
it.each(cases)('$input -> $expected', ({ input, expected }) => {
expect(parseTargetString(input)).toEqual(expected);
});
});

describe('targetToTargetString', () => {
it.each(cases)('$expected -> $input', ({ input, expected }) => {
expect(targetToTargetString(expected)).toEqual(input);
});
});
5 changes: 3 additions & 2 deletions packages/devkit/src/executors/parse-target-string.ts
@@ -1,4 +1,5 @@
import type { Target } from 'nx/src/command-line/run';
import { splitTarget } from 'nx/src/utils/split-target';

/**
* Parses a target string into {project, target, configuration}
Expand All @@ -12,7 +13,7 @@ import type { Target } from 'nx/src/command-line/run';
* @param targetString - target reference
*/
export function parseTargetString(targetString: string): Target {
const [project, target, configuration] = targetString.split(':');
const [project, target, configuration] = splitTarget(targetString);
if (!project || !target) {
throw new Error(`Invalid Target String: ${targetString}`);
}
Expand Down Expand Up @@ -40,7 +41,7 @@ export function targetToTargetString({
target,
configuration,
}: Target): string {
return `${project}:${target}${
return `${project}:${target.indexOf(':') > -1 ? `"${target}"` : target}${
configuration !== undefined ? ':' + configuration : ''
}`;
}
18 changes: 9 additions & 9 deletions packages/nx/src/utils/split-target.spec.ts
@@ -1,14 +1,14 @@
import { splitTarget } from './split-target';

const cases = [
{ input: 'one', expected: ['one'] },
{ input: 'one:two', expected: ['one', 'two'] },
{ input: 'one:two:three', expected: ['one', 'two', 'three'] },
{ input: 'one:"two:two":three', expected: ['one', 'two:two', 'three'] },
];

describe('splitTarget', () => {
it('should work', () => {
expect(splitTarget('one')).toEqual(['one']);
expect(splitTarget('one:two')).toEqual(['one', 'two']);
expect(splitTarget('one:two:three')).toEqual(['one', 'two', 'three']);
expect(splitTarget('one:"two:two":three')).toEqual([
'one',
'two:two',
'three',
]);
it.each(cases)('$input -> $expected', ({ input, expected }) => {
expect(splitTarget(input)).toEqual(expected);
});
});
6 changes: 4 additions & 2 deletions packages/nx/src/utils/split-target.ts
@@ -1,4 +1,6 @@
export function splitTarget(s: string): any {
export function splitTarget(
s: string
): [project: string, target?: string, configuration?: string] {
const parts = [] as string[];
let currentPart = '';
for (let i = 0; i < s.length; ++i) {
Expand All @@ -15,5 +17,5 @@ export function splitTarget(s: string): any {
}
}
parts.push(currentPart);
return parts;
return parts as [string, string?, string?];
}

0 comments on commit e439718

Please sign in to comment.