fix(misc): dot nx setup shouldn't include target defaults (#23180)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
- `nx add @nx/gradle` installs latest version
- `nx init` sets up target defaults
- `nx add package@beta` will add `beta` as the version, rather than
resolving it to the tag.

## Expected Behavior
- `nx add @nx/gradle` installs version matching `nx`
- `nx init` lets plugin handle settings
- `nx add package@beta` will add the latest version matching the beta
tag

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
This commit is contained in:
Craigory Coppola 2024-05-03 19:07:56 -04:00 committed by GitHub
parent 84eb280236
commit 30446df23b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 59 deletions

View File

@ -18,6 +18,14 @@ describe('nx wrapper / .nx installation', () => {
beforeAll(() => { beforeAll(() => {
runNxWrapper = newWrappedNxWorkspace(); runNxWrapper = newWrappedNxWorkspace();
updateJson<NxJsonConfiguration>('nx.json', (json) => {
json.targetDefaults ??= {};
json.targetDefaults.echo = { cache: true };
json.installation.plugins = {
'@nx/js': getPublishedVersion(),
};
return json;
});
}); });
afterAll(() => { afterAll(() => {
@ -39,14 +47,6 @@ describe('nx wrapper / .nx installation', () => {
}) })
); );
updateJson<NxJsonConfiguration>('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('Hello from A');
expect(runNxWrapper('echo a')).toContain( expect(runNxWrapper('echo a')).toContain(

View File

@ -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');
});
});

View File

@ -14,6 +14,7 @@ import { getPluginCapabilities } from '../../utils/plugins';
import { nxVersion } from '../../utils/versions'; import { nxVersion } from '../../utils/versions';
import { workspaceRoot } from '../../utils/workspace-root'; import { workspaceRoot } from '../../utils/workspace-root';
import type { AddOptions } from './command-object'; import type { AddOptions } from './command-object';
import { normalizeVersionForNxJson } from '../init/implementation/dot-nx/add-nx-scripts';
export function addHandler(options: AddOptions): Promise<void> { export function addHandler(options: AddOptions): Promise<void> {
if (options.verbose) { if (options.verbose) {
@ -63,7 +64,10 @@ async function installPackage(
); );
} else { } else {
nxJson.installation.plugins ??= {}; nxJson.installation.plugins ??= {};
nxJson.installation.plugins[pkgName] = version; nxJson.installation.plugins[pkgName] = normalizeVersionForNxJson(
pkgName,
version
);
writeJsonFile('nx.json', nxJson); writeJsonFile('nx.json', nxJson);
try { try {
@ -113,7 +117,7 @@ async function initializePlugin(
try { try {
const args = []; const args = [];
if (coreNxPlugins.includes(pkgName)) { if (coreNxPluginVersions.has(pkgName)) {
args.push(`--keepExistingVersions`); args.push(`--keepExistingVersions`);
if ( if (
@ -171,8 +175,8 @@ function parsePackageSpecifier(
const i = packageSpecifier.lastIndexOf('@'); const i = packageSpecifier.lastIndexOf('@');
if (i <= 0) { if (i <= 0) {
if (coreNxPlugins.includes(packageSpecifier)) { if (coreNxPluginVersions.has(packageSpecifier)) {
return [packageSpecifier, nxVersion]; return [packageSpecifier, coreNxPluginVersions.get(packageSpecifier)];
} }
return [packageSpecifier, 'latest']; return [packageSpecifier, 'latest'];
@ -184,31 +188,14 @@ function parsePackageSpecifier(
return [pkgName, version]; return [pkgName, version];
} }
const coreNxPlugins = [ export const coreNxPluginVersions = (
'@nx/angular', require('../../../package.json') as typeof import('../../../package.json')
'@nx/cypress', )['nx-migrations'].packageGroup.reduce(
'@nx/detox', (map, entry) => {
'@nx/devkit', const packageName = typeof entry === 'string' ? entry : entry.package;
'@nx/esbuild', const version = typeof entry === 'string' ? nxVersion : entry.version;
'@nx/eslint', return map.set(packageName, version);
'@nx/eslint-plugin', },
'@nx/expo', // Package Name -> Desired Version
'@nx/express', new Map<string, string>()
'@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',
];

View File

@ -46,29 +46,18 @@ export function generateDotNxSetup(version?: string) {
flushChanges(host.root, changes); 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) { export function writeMinimalNxJson(host: Tree, version: string) {
if (!host.exists('nx.json')) { if (!host.exists('nx.json')) {
if (!valid(version)) {
version = execSync(`npm view nx@${version} version`).toString();
}
writeJson<NxJsonConfiguration>(host, 'nx.json', { writeJson<NxJsonConfiguration>(host, 'nx.json', {
targetDefaults: {
build: {
cache: true,
dependsOn: ['^build'],
},
lint: {
cache: true,
},
test: {
cache: true,
},
e2e: {
cache: true,
},
},
installation: { installation: {
version: version.trimEnd(), version: normalizeVersionForNxJson('nx', version),
}, },
}); });
} }