From d3b9b21feb1cc6f28b50f2da02305bed361d4061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Wed, 6 Mar 2024 18:28:52 +0100 Subject: [PATCH] fix(misc): do not add includedScripts unless really needed when running nx add (#22180) (cherry picked from commit 1731ad890a0c118b903471ba83bde21d80889d89) --- .../src/utils/update-package-scripts.spec.ts | 35 +++++++++++++++++++ .../src/utils/update-package-scripts.ts | 13 ++++--- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/packages/devkit/src/utils/update-package-scripts.spec.ts b/packages/devkit/src/utils/update-package-scripts.spec.ts index 4fd43d76d3125..317cc5db24e90 100644 --- a/packages/devkit/src/utils/update-package-scripts.spec.ts +++ b/packages/devkit/src/utils/update-package-scripts.spec.ts @@ -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(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', { diff --git a/packages/devkit/src/utils/update-package-scripts.ts b/packages/devkit/src/utils/update-package-scripts.ts index 13324e804671e..42c931f077bb9 100644 --- a/packages/devkit/src/utils/update-package-scripts.ts +++ b/packages/devkit/src/utils/update-package-scripts.ts @@ -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);