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(devkit): parseTargetString should support targets with colons in their name #10400

Merged
merged 1 commit into from
May 20, 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
25 changes: 25 additions & 0 deletions packages/devkit/src/executors/parse-target-string.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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?];
}