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): dot nx setup shouldn't include target defaults #23180

Merged
merged 1 commit into from
May 3, 2024
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
16 changes: 8 additions & 8 deletions e2e/nx/src/nxw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions packages/nx/src/command-line/add/add.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
51 changes: 19 additions & 32 deletions packages/nx/src/command-line/add/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -113,7 +117,7 @@ async function initializePlugin(

try {
const args = [];
if (coreNxPlugins.includes(pkgName)) {
if (coreNxPluginVersions.has(pkgName)) {
args.push(`--keepExistingVersions`);

if (
Expand Down Expand Up @@ -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'];
Expand All @@ -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>()
);
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
});
}
Expand Down