fix(devkit): parseTargetString should support targets with colons in the name (#10400)
This commit is contained in:
parent
99b4c60f62
commit
e439718059
25
packages/devkit/src/executors/parse-target-string.spec.ts
Normal file
25
packages/devkit/src/executors/parse-target-string.spec.ts
Normal file
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,4 +1,5 @@
|
|||||||
import type { Target } from 'nx/src/command-line/run';
|
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}
|
* Parses a target string into {project, target, configuration}
|
||||||
@ -12,7 +13,7 @@ import type { Target } from 'nx/src/command-line/run';
|
|||||||
* @param targetString - target reference
|
* @param targetString - target reference
|
||||||
*/
|
*/
|
||||||
export function parseTargetString(targetString: string): Target {
|
export function parseTargetString(targetString: string): Target {
|
||||||
const [project, target, configuration] = targetString.split(':');
|
const [project, target, configuration] = splitTarget(targetString);
|
||||||
if (!project || !target) {
|
if (!project || !target) {
|
||||||
throw new Error(`Invalid Target String: ${targetString}`);
|
throw new Error(`Invalid Target String: ${targetString}`);
|
||||||
}
|
}
|
||||||
@ -40,7 +41,7 @@ export function targetToTargetString({
|
|||||||
target,
|
target,
|
||||||
configuration,
|
configuration,
|
||||||
}: Target): string {
|
}: Target): string {
|
||||||
return `${project}:${target}${
|
return `${project}:${target.indexOf(':') > -1 ? `"${target}"` : target}${
|
||||||
configuration !== undefined ? ':' + configuration : ''
|
configuration !== undefined ? ':' + configuration : ''
|
||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
import { splitTarget } from './split-target';
|
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', () => {
|
describe('splitTarget', () => {
|
||||||
it('should work', () => {
|
it.each(cases)('$input -> $expected', ({ input, expected }) => {
|
||||||
expect(splitTarget('one')).toEqual(['one']);
|
expect(splitTarget(input)).toEqual(expected);
|
||||||
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',
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -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[];
|
const parts = [] as string[];
|
||||||
let currentPart = '';
|
let currentPart = '';
|
||||||
for (let i = 0; i < s.length; ++i) {
|
for (let i = 0; i < s.length; ++i) {
|
||||||
@ -15,5 +17,5 @@ export function splitTarget(s: string): any {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
parts.push(currentPart);
|
parts.push(currentPart);
|
||||||
return parts;
|
return parts as [string, string?, string?];
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user