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:
parent
84eb280236
commit
30446df23b
@ -18,6 +18,14 @@ describe('nx wrapper / .nx installation', () => {
|
||||
|
||||
beforeAll(() => {
|
||||
runNxWrapper = newWrappedNxWorkspace();
|
||||
updateJson<NxJsonConfiguration>('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<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(
|
||||
|
||||
13
packages/nx/src/command-line/add/add.spec.ts
Normal file
13
packages/nx/src/command-line/add/add.spec.ts
Normal 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');
|
||||
});
|
||||
});
|
||||
@ -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<void> {
|
||||
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<string, string>()
|
||||
);
|
||||
|
||||
@ -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<NxJsonConfiguration>(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),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user