Skip to content

Commit

Permalink
fix(misc): dot nx setup shouldn't include target defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed May 3, 2024
1 parent d71a324 commit 075e062
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 51 deletions.
48 changes: 16 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,7 @@ async function installPackage(
);
} else {
nxJson.installation.plugins ??= {};
nxJson.installation.plugins[pkgName] = version;
nxJson.installation.plugins[pkgName] = normalizeVersionForNxJson(version);
writeJsonFile('nx.json', nxJson);

try {
Expand Down Expand Up @@ -113,7 +114,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 +172,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 +185,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',
];
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(version: string) {
if (!valid(version)) {
version = execSync(`npm view nx@${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(version),
},
});
}
Expand Down

0 comments on commit 075e062

Please sign in to comment.