fix(core): do not create new targets from target defaults when packag… (#21365)
This commit is contained in:
parent
b4029e09ed
commit
e1bb8bccf1
@ -8,7 +8,7 @@ const {
|
|||||||
createNodes: [, createNodesFn],
|
createNodes: [, createNodesFn],
|
||||||
} = TargetDefaultsPlugin;
|
} = TargetDefaultsPlugin;
|
||||||
|
|
||||||
describe('nx project.json plugin', () => {
|
describe('target-defaults plugin', () => {
|
||||||
let context: CreateNodesContext;
|
let context: CreateNodesContext;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
context = {
|
context = {
|
||||||
@ -23,6 +23,10 @@ describe('nx project.json plugin', () => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
memfs.vol.reset();
|
||||||
|
});
|
||||||
|
|
||||||
it('should add target default info to project json projects', () => {
|
it('should add target default info to project json projects', () => {
|
||||||
memfs.vol.fromJSON(
|
memfs.vol.fromJSON(
|
||||||
{
|
{
|
||||||
@ -77,4 +81,156 @@ describe('nx project.json plugin', () => {
|
|||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should add target if package.json has nx but no includedScripts', () => {
|
||||||
|
memfs.vol.fromJSON(
|
||||||
|
{
|
||||||
|
'package.json': JSON.stringify({
|
||||||
|
name: 'lib-a',
|
||||||
|
scripts: {
|
||||||
|
test: 'nx affected:test',
|
||||||
|
},
|
||||||
|
nx: {},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
'/root'
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
createNodesFn('package.json', undefined, {
|
||||||
|
nxJsonConfiguration: {
|
||||||
|
targetDefaults: {
|
||||||
|
test: {
|
||||||
|
command: 'jest',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
workspaceRoot: '/root',
|
||||||
|
})
|
||||||
|
).toMatchInlineSnapshot(`
|
||||||
|
{
|
||||||
|
"projects": {
|
||||||
|
".": {
|
||||||
|
"targets": {
|
||||||
|
"test": {
|
||||||
|
"command": "jest",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add target if package.json has nx and includes the script in includedScripts', () => {
|
||||||
|
memfs.vol.fromJSON(
|
||||||
|
{
|
||||||
|
'package.json': JSON.stringify({
|
||||||
|
name: 'lib-a',
|
||||||
|
scripts: {
|
||||||
|
test: 'nx affected:test',
|
||||||
|
},
|
||||||
|
nx: {
|
||||||
|
includedScripts: ['test'],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
'/root'
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
createNodesFn('package.json', undefined, {
|
||||||
|
nxJsonConfiguration: {
|
||||||
|
targetDefaults: {
|
||||||
|
test: {
|
||||||
|
command: 'jest',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
workspaceRoot: '/root',
|
||||||
|
})
|
||||||
|
).toMatchInlineSnapshot(`
|
||||||
|
{
|
||||||
|
"projects": {
|
||||||
|
".": {
|
||||||
|
"targets": {
|
||||||
|
"test": {
|
||||||
|
"command": "jest",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not add target if package.json does not have nx', () => {
|
||||||
|
memfs.vol.fromJSON(
|
||||||
|
{
|
||||||
|
'package.json': JSON.stringify({
|
||||||
|
name: 'lib-a',
|
||||||
|
scripts: {
|
||||||
|
test: 'nx affected:test',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
'/root'
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
createNodesFn('package.json', undefined, {
|
||||||
|
nxJsonConfiguration: {
|
||||||
|
targetDefaults: {
|
||||||
|
test: {
|
||||||
|
command: 'jest',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
workspaceRoot: '/root',
|
||||||
|
})
|
||||||
|
).toMatchInlineSnapshot(`{}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not add target if project does not define target', () => {
|
||||||
|
memfs.vol.fromJSON(
|
||||||
|
{
|
||||||
|
'package.json': JSON.stringify({
|
||||||
|
name: 'lib-a',
|
||||||
|
scripts: {
|
||||||
|
test: 'nx affected:test',
|
||||||
|
},
|
||||||
|
nx: {
|
||||||
|
includedScripts: [],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
'/root'
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
createNodesFn('package.json', undefined, {
|
||||||
|
nxJsonConfiguration: {
|
||||||
|
targetDefaults: {
|
||||||
|
test: {
|
||||||
|
command: 'jest',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
workspaceRoot: '/root',
|
||||||
|
})
|
||||||
|
).toMatchInlineSnapshot(`
|
||||||
|
{
|
||||||
|
"projects": {
|
||||||
|
".": {
|
||||||
|
"targets": {
|
||||||
|
"test": {
|
||||||
|
"command": "jest",
|
||||||
|
Symbol(ONLY_MODIFIES_EXISTING_TARGET): true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -58,12 +58,16 @@ export const TargetDefaultsPlugin: NxPluginV2 = {
|
|||||||
const packageJson = readJsonOrNull<PackageJson>(
|
const packageJson = readJsonOrNull<PackageJson>(
|
||||||
join(ctx.workspaceRoot, root, 'package.json')
|
join(ctx.workspaceRoot, root, 'package.json')
|
||||||
);
|
);
|
||||||
const projectDefinedTargets = new Set(
|
const includedScripts = packageJson?.nx?.includedScripts;
|
||||||
Object.keys({
|
const projectDefinedTargets = new Set([
|
||||||
...packageJson?.scripts,
|
...Object.keys(packageJson?.scripts ?? {}).filter((script) => {
|
||||||
...projectJson?.targets,
|
if (includedScripts) {
|
||||||
})
|
return includedScripts.includes(script);
|
||||||
);
|
}
|
||||||
|
return true;
|
||||||
|
}),
|
||||||
|
...Object.keys(projectJson?.targets ?? {}),
|
||||||
|
]);
|
||||||
|
|
||||||
const executorToTargetMap = getExecutorToTargetMap(
|
const executorToTargetMap = getExecutorToTargetMap(
|
||||||
packageJson,
|
packageJson,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user