diff --git a/e2e/nx/src/nxw.test.ts b/e2e/nx/src/nxw.test.ts index 6da7fddb85613..448d198fcdb25 100644 --- a/e2e/nx/src/nxw.test.ts +++ b/e2e/nx/src/nxw.test.ts @@ -18,6 +18,14 @@ describe('nx wrapper / .nx installation', () => { beforeAll(() => { runNxWrapper = newWrappedNxWorkspace(); + updateJson('nx.json', (json) => { + json.targetDefaults ??= {}; + json.targetDefaults.echo = { cache: true }; + json.installation.plugins = { + '@nx/js': getPublishedVersion(), + }; + return json; + }); }); afterAll(() => { @@ -39,14 +47,6 @@ describe('nx wrapper / .nx installation', () => { }) ); - updateJson('nx.json', (json) => { - json.targetDefaults.echo = { cache: true }; - json.installation.plugins = { - '@nx/js': getPublishedVersion(), - }; - return json; - }); - expect(runNxWrapper('echo a')).toContain('Hello from A'); expect(runNxWrapper('echo a')).toContain( diff --git a/packages/nx/src/command-line/add/add.spec.ts b/packages/nx/src/command-line/add/add.spec.ts new file mode 100644 index 0000000000000..290ac43899d71 --- /dev/null +++ b/packages/nx/src/command-line/add/add.spec.ts @@ -0,0 +1,13 @@ +import { nxVersion } from '../../utils/versions'; +import { coreNxPluginVersions } from './add'; + +describe('nx core packages', () => { + it('should map nx packages to nx version', () => { + expect(coreNxPluginVersions.get('@nx/workspace')).toEqual(nxVersion); + }); + + it('should map nx-cloud to latest', () => { + expect(coreNxPluginVersions.get('@nrwl/nx-cloud')).toEqual('latest'); + expect(coreNxPluginVersions.get('nx-cloud')).toEqual('latest'); + }); +}); diff --git a/packages/nx/src/command-line/add/add.ts b/packages/nx/src/command-line/add/add.ts index 00604401054aa..b227b11d3b602 100644 --- a/packages/nx/src/command-line/add/add.ts +++ b/packages/nx/src/command-line/add/add.ts @@ -14,6 +14,7 @@ import { getPluginCapabilities } from '../../utils/plugins'; import { nxVersion } from '../../utils/versions'; import { workspaceRoot } from '../../utils/workspace-root'; import type { AddOptions } from './command-object'; +import { normalizeVersionForNxJson } from '../init/implementation/dot-nx/add-nx-scripts'; export function addHandler(options: AddOptions): Promise { if (options.verbose) { @@ -63,7 +64,10 @@ async function installPackage( ); } else { nxJson.installation.plugins ??= {}; - nxJson.installation.plugins[pkgName] = version; + nxJson.installation.plugins[pkgName] = normalizeVersionForNxJson( + pkgName, + version + ); writeJsonFile('nx.json', nxJson); try { @@ -113,7 +117,7 @@ async function initializePlugin( try { const args = []; - if (coreNxPlugins.includes(pkgName)) { + if (coreNxPluginVersions.has(pkgName)) { args.push(`--keepExistingVersions`); if ( @@ -171,8 +175,8 @@ function parsePackageSpecifier( const i = packageSpecifier.lastIndexOf('@'); if (i <= 0) { - if (coreNxPlugins.includes(packageSpecifier)) { - return [packageSpecifier, nxVersion]; + if (coreNxPluginVersions.has(packageSpecifier)) { + return [packageSpecifier, coreNxPluginVersions.get(packageSpecifier)]; } return [packageSpecifier, 'latest']; @@ -184,31 +188,14 @@ function parsePackageSpecifier( return [pkgName, version]; } -const coreNxPlugins = [ - '@nx/angular', - '@nx/cypress', - '@nx/detox', - '@nx/devkit', - '@nx/esbuild', - '@nx/eslint', - '@nx/eslint-plugin', - '@nx/expo', - '@nx/express', - '@nx/jest', - '@nx/nest', - '@nx/next', - '@nx/node', - '@nx/nuxt', - '@nx/playwright', - '@nx/plugin', - '@nx/react', - '@nx/react-native', - '@nx/remix', - '@nx/rollup', - '@nx/storybook', - '@nx/vite', - '@nx/vue', - '@nx/web', - '@nx/webpack', - '@nx/workspace', -]; +export const coreNxPluginVersions = ( + require('../../../package.json') as typeof import('../../../package.json') +)['nx-migrations'].packageGroup.reduce( + (map, entry) => { + const packageName = typeof entry === 'string' ? entry : entry.package; + const version = typeof entry === 'string' ? nxVersion : entry.version; + return map.set(packageName, version); + }, + // Package Name -> Desired Version + new Map() +); diff --git a/packages/nx/src/command-line/init/implementation/dot-nx/add-nx-scripts.ts b/packages/nx/src/command-line/init/implementation/dot-nx/add-nx-scripts.ts index 13a6e08d33623..624002b0fc79e 100644 --- a/packages/nx/src/command-line/init/implementation/dot-nx/add-nx-scripts.ts +++ b/packages/nx/src/command-line/init/implementation/dot-nx/add-nx-scripts.ts @@ -46,29 +46,18 @@ export function generateDotNxSetup(version?: string) { flushChanges(host.root, changes); } +export function normalizeVersionForNxJson(pkg: string, version: string) { + if (!valid(version)) { + version = execSync(`npm view ${pkg}@${version} version`).toString(); + } + return version.trimEnd(); +} + export function writeMinimalNxJson(host: Tree, version: string) { if (!host.exists('nx.json')) { - if (!valid(version)) { - version = execSync(`npm view nx@${version} version`).toString(); - } writeJson(host, 'nx.json', { - targetDefaults: { - build: { - cache: true, - dependsOn: ['^build'], - }, - lint: { - cache: true, - }, - test: { - cache: true, - }, - e2e: { - cache: true, - }, - }, installation: { - version: version.trimEnd(), + version: normalizeVersionForNxJson('nx', version), }, }); }