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(misc): do not add includedScripts unless really needed when running nx add #22180

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/devkit/src/utils/update-package-scripts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,41 @@ describe('updatePackageScripts', () => {
]);
});

it('should not set "nx.includedScripts" when no script matched an inferred target', async () => {
tree.write('vite.config.ts', '');
writeJson(tree, 'package.json', {
name: 'app1',
scripts: {
serve: 'vite',
coverage: 'vitest run --coverage',
foo: 'echo "foo"',
},
});

await updatePackageScripts(tree, [
'**/{vite,vitest}.config.{js,ts,mjs,mts,cjs,cts}',
() => ({
projects: {
app1: {
targets: {
build: { command: 'vite build' },
serve: { command: 'vite serve' },
test: { command: 'vitest run' },
},
},
},
}),
]);

const packageJson = readJson<PackageJson>(tree, 'package.json');
expect(packageJson.scripts).toStrictEqual({
serve: 'vite',
coverage: 'nx test --coverage',
foo: 'echo "foo"',
});
expect(packageJson.nx).toBeUndefined();
});

it('should exclude replaced package.json scripts from nx if they are initially included', async () => {
tree.write('next.config.js', '');
writeJson(tree, 'package.json', {
Expand Down
13 changes: 9 additions & 4 deletions packages/devkit/src/utils/update-package-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,25 @@ async function processProject(
}
}

packageJson.nx ??= {};
if (process.env.NX_RUNNING_NX_INIT === 'true') {
// running `nx init` so we want to exclude everything by default
packageJson.nx ??= {};
packageJson.nx.includedScripts = [];
} else {
} else if (replacedTargets.size) {
/**
* Running `nx add`. In this case we want to:
* - if `includedScripts` is already set: exclude scripts that match inferred targets that were used to replace a script
* - if `includedScripts` is not set: set `includedScripts` with all scripts except the ones that match an inferred target that was used to replace a script
*/
packageJson.nx.includedScripts ??= Object.keys(packageJson.scripts);
packageJson.nx.includedScripts = packageJson.nx.includedScripts.filter(
const includedScripts =
packageJson.nx?.includedScripts ?? Object.keys(packageJson.scripts);
const filteredScripts = includedScripts.filter(
(s) => !replacedTargets.has(s)
);
if (filteredScripts.length !== includedScripts.length) {
packageJson.nx ??= {};
packageJson.nx.includedScripts = filteredScripts;
}
}

writeJson(tree, packageJsonPath, packageJson);
Expand Down