Skip to content

Commit ada8be4

Browse files
authored
fix(misc): fix misc issues in project generators for the ts solution setup (#30111)
The following are the main changes in the context of the TS solution setup: - Ensure `name` in `package.json` files is set to the import path for all projects - Set `nx.name` in `package.json` files when the user provides a name different than the package name (import path) - Clean up project generators so they don't set the `nx` property in `package.json` files unless strictly needed - Fix `@nx/vue:application` generator so it creates the Nx config in a `package.json` file for e2e projects - Ensure `@types/node` is installed in `vitest` generator - Fix generated Vite config typing error (surfaced with Vite 6) - Ensure `jsonc-eslint-parser` is installed when the `@nx/dependency-checks` rule is added to the ESLint config - Misc minor alignment changes ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes #
1 parent 121d997 commit ada8be4

File tree

129 files changed

+2031
-878
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+2031
-878
lines changed

e2e/esbuild/src/esbuild.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ describe('EsBuild Plugin', () => {
169169
expect(
170170
readJson(`dist/libs/${parentLib}/package.json`).dependencies
171171
).toEqual({
172-
'jsonc-eslint-parser': expect.any(String),
173172
// Don't care about the versions, just that they exist
174173
rambda: expect.any(String),
175174
lodash: expect.any(String),

e2e/js/src/js-ts-solution.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
getPackageManagerCommand,
44
getSelectedPackageManager,
55
newProject,
6+
readJson,
67
runCLI,
78
runCommand,
89
uniq,
@@ -177,4 +178,28 @@ ${content}`
177178
`Successfully ran target test for project @proj/${viteParentLib}`
178179
);
179180
}, 300_000);
181+
182+
it('should respect and support generating libraries with a name different than the import path', () => {
183+
const lib1 = uniq('lib1');
184+
185+
runCLI(
186+
`generate @nx/js:lib packages/${lib1} --name=${lib1} --bundler=vite --linter=eslint --unitTestRunner=jest`
187+
);
188+
189+
const packageJson = readJson(`packages/${lib1}/package.json`);
190+
expect(packageJson.nx.name).toBe(lib1);
191+
192+
expect(runCLI(`build ${lib1}`)).toContain(
193+
`Successfully ran target build for project ${lib1}`
194+
);
195+
expect(runCLI(`typecheck ${lib1}`)).toContain(
196+
`Successfully ran target typecheck for project ${lib1}`
197+
);
198+
expect(runCLI(`lint ${lib1}`)).toContain(
199+
`Successfully ran target lint for project ${lib1}`
200+
);
201+
expect(runCLI(`test ${lib1}`)).toContain(
202+
`Successfully ran target test for project ${lib1}`
203+
);
204+
}, 300_000);
180205
});

e2e/node/src/node-ts-solution.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
killPorts,
77
newProject,
88
promisifiedTreeKill,
9+
readJson,
910
runCLI,
1011
runCommand,
1112
runCommandUntil,
@@ -169,6 +170,30 @@ describe('Node Applications', () => {
169170
expect(err).toBeFalsy();
170171
}
171172
}, 300_000);
173+
174+
it('should respect and support generating libraries with a name different than the import path', () => {
175+
const nodeLib = uniq('node-lib');
176+
const nestLib = uniq('nest-lib');
177+
178+
runCLI(
179+
`generate @nx/node:lib packages/${nodeLib} --name=${nodeLib} --buildable`
180+
);
181+
runCLI(
182+
`generate @nx/nest:lib packages/${nestLib} --name=${nestLib} --buildable`
183+
);
184+
185+
const packageJson = readJson(`packages/${nodeLib}/package.json`);
186+
expect(packageJson.nx.name).toBe(nodeLib);
187+
const nestPackageJson = readJson(`packages/${nestLib}/package.json`);
188+
expect(nestPackageJson.nx.name).toBe(nestLib);
189+
190+
expect(runCLI(`build ${nodeLib}`)).toContain(
191+
`Successfully ran target build for project ${nodeLib}`
192+
);
193+
expect(runCLI(`build ${nestLib}`)).toContain(
194+
`Successfully ran target build for project ${nestLib}`
195+
);
196+
}, 300_000);
172197
});
173198

174199
function getRandomPort() {

e2e/plugin/src/nx-plugin-ts-solution.test.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import {
33
cleanupProject,
44
createFile,
55
newProject,
6+
readJson,
67
renameFile,
78
runCLI,
9+
runCommand,
810
uniq,
911
updateFile,
1012
updateJson,
@@ -103,12 +105,10 @@ describe('Nx Plugin (TS solution)', () => {
103105

104106
// Register plugin in nx.json (required for inference)
105107
updateJson(`nx.json`, (nxJson) => {
106-
nxJson.plugins = [
107-
{
108-
plugin: `@${workspaceName}/${plugin}`,
109-
options: { inferredTags: ['my-tag'] },
110-
},
111-
];
108+
nxJson.plugins.push({
109+
plugin: `@${workspaceName}/${plugin}`,
110+
options: { inferredTags: ['my-tag'] },
111+
});
112112
return nxJson;
113113
});
114114

@@ -262,4 +262,22 @@ describe('Nx Plugin (TS solution)', () => {
262262
expect(() => checkFilesExist(`libs/${generatedProject}`)).not.toThrow();
263263
expect(() => runCLI(`execute ${generatedProject}`)).not.toThrow();
264264
});
265+
266+
it('should respect and support generating plugins with a name different than the import path', async () => {
267+
const plugin = uniq('plugin');
268+
269+
runCLI(
270+
`generate @nx/plugin:plugin packages/${plugin} --name=${plugin} --linter=eslint --publishable`
271+
);
272+
273+
const packageJson = readJson(`packages/${plugin}/package.json`);
274+
expect(packageJson.nx.name).toBe(plugin);
275+
276+
expect(runCLI(`build ${plugin}`)).toContain(
277+
`Successfully ran target build for project ${plugin}`
278+
);
279+
expect(runCLI(`lint ${plugin}`)).toContain(
280+
`Successfully ran target lint for project ${plugin}`
281+
);
282+
}, 90000);
265283
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
cleanupProject,
3+
newProject,
4+
readJson,
5+
runCLI,
6+
uniq,
7+
} from '@nx/e2e/utils';
8+
9+
describe('React (TS solution)', () => {
10+
let workspaceName: string;
11+
12+
beforeAll(() => {
13+
workspaceName = newProject({ preset: 'ts', packages: ['@nx/react'] });
14+
});
15+
16+
afterAll(() => cleanupProject());
17+
18+
it('should respect and support generating libraries with a name different than the import path', async () => {
19+
const lib = uniq('lib');
20+
21+
runCLI(
22+
`generate @nx/react:library packages/${lib} --name=${lib} --bundler=vite --linter=eslint --unitTestRunner=vitest`
23+
);
24+
25+
const packageJson = readJson(`packages/${lib}/package.json`);
26+
expect(packageJson.nx.name).toBe(lib);
27+
28+
expect(runCLI(`build ${lib}`)).toContain(
29+
`Successfully ran target build for project ${lib}`
30+
);
31+
expect(runCLI(`typecheck ${lib}`)).toContain(
32+
`Successfully ran target typecheck for project ${lib}`
33+
);
34+
expect(runCLI(`lint ${lib}`)).toContain(
35+
`Successfully ran target lint for project ${lib}`
36+
);
37+
expect(runCLI(`test ${lib}`)).toContain(
38+
`Successfully ran target test for project ${lib}`
39+
);
40+
}, 90000);
41+
});

e2e/remix/src/remix-ts-solution.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { cleanupProject, newProject, runCLI, uniq } from '@nx/e2e/utils';
1+
import {
2+
cleanupProject,
3+
newProject,
4+
readJson,
5+
runCLI,
6+
uniq,
7+
} from '@nx/e2e/utils';
28

39
describe('Remix - TS solution setup', () => {
410
beforeAll(() => {
@@ -113,4 +119,28 @@ describe('Remix - TS solution setup', () => {
113119
`Successfully ran target test for project @proj/${buildableLibJest}`
114120
);
115121
}, 120_000);
122+
123+
it('should respect and support generating libraries with a name different than the import path', async () => {
124+
const lib = uniq('lib');
125+
126+
runCLI(
127+
`generate @nx/remix:library packages/${lib} --name=${lib} --linter=eslint --unitTestRunner=vitest --buildable`
128+
);
129+
130+
const packageJson = readJson(`packages/${lib}/package.json`);
131+
expect(packageJson.nx.name).toBe(lib);
132+
133+
expect(runCLI(`build ${lib}`)).toContain(
134+
`Successfully ran target build for project ${lib}`
135+
);
136+
expect(runCLI(`typecheck ${lib}`)).toContain(
137+
`Successfully ran target typecheck for project ${lib}`
138+
);
139+
expect(runCLI(`lint ${lib}`)).toContain(
140+
`Successfully ran target lint for project ${lib}`
141+
);
142+
expect(runCLI(`test ${lib}`)).toContain(
143+
`Successfully ran target test for project ${lib}`
144+
);
145+
}, 120_000);
116146
});

e2e/vue/src/vue-ts-solution.test.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import {
22
cleanupProject,
3-
getSelectedPackageManager,
43
newProject,
4+
readJson,
55
runCLI,
66
uniq,
77
updateFile,
8-
updateJson,
98
} from '@nx/e2e/utils';
109

11-
describe('Vue Plugin', () => {
10+
describe('Vue (TS solution)', () => {
1211
let proj: string;
1312

14-
const pm = getSelectedPackageManager();
15-
1613
beforeAll(() => {
1714
proj = newProject({
1815
packages: ['@nx/vue'],
@@ -57,4 +54,32 @@ describe('Vue Plugin', () => {
5754
expect(() => runCLI(`test @proj/${lib}`)).not.toThrow();
5855
expect(() => runCLI(`build @proj/${lib}`)).not.toThrow();
5956
}, 300_000);
57+
58+
it('should respect and support generating libraries with a name different than the import path', async () => {
59+
const lib = uniq('lib');
60+
61+
runCLI(
62+
`generate @nx/vue:library packages/${lib} --name=${lib} --bundler=vite --unitTestRunner=vitest`
63+
);
64+
// lib generator doesn't generate specs, add one
65+
updateFile(
66+
`packages/${lib}/src/foo.spec.ts`,
67+
`test('it should run', () => {
68+
expect(true).toBeTruthy();
69+
});`
70+
);
71+
72+
const packageJson = readJson(`packages/${lib}/package.json`);
73+
expect(packageJson.nx.name).toBe(lib);
74+
75+
expect(runCLI(`build ${lib}`)).toContain(
76+
`Successfully ran target build for project ${lib}`
77+
);
78+
expect(runCLI(`typecheck ${lib}`)).toContain(
79+
`Successfully ran target typecheck for project ${lib}`
80+
);
81+
expect(runCLI(`test ${lib}`)).toContain(
82+
`Successfully ran target test for project ${lib}`
83+
);
84+
}, 300_000);
6085
});

packages/angular/src/generators/application/lib/normalize-options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { joinPathFragments, type Tree } from '@nx/devkit';
22
import {
33
determineProjectNameAndRootOptions,
4-
ensureProjectName,
4+
ensureRootProjectName,
55
} from '@nx/devkit/src/generators/project-name-and-root-utils';
66
import { Linter } from '@nx/eslint';
77
import { E2eTestRunner, UnitTestRunner } from '../../../utils/test-runners';
@@ -12,7 +12,7 @@ export async function normalizeOptions(
1212
host: Tree,
1313
options: Partial<Schema>
1414
): Promise<NormalizedSchema> {
15-
await ensureProjectName(host, options as Schema, 'application');
15+
await ensureRootProjectName(options as Schema, 'application');
1616
const { projectName: appProjectName, projectRoot: appProjectRoot } =
1717
await determineProjectNameAndRootOptions(host, {
1818
name: options.name,

packages/angular/src/generators/host/host.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@nx/devkit';
88
import {
99
determineProjectNameAndRootOptions,
10-
ensureProjectName,
10+
ensureRootProjectName,
1111
} from '@nx/devkit/src/generators/project-name-and-root-utils';
1212
import { isValidVariable } from '@nx/js';
1313
import { assertNotUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
@@ -53,7 +53,7 @@ export async function host(tree: Tree, schema: Schema) {
5353
});
5454
}
5555

56-
await ensureProjectName(tree, options, 'application');
56+
await ensureRootProjectName(options, 'application');
5757
const { projectName: hostProjectName, projectRoot: appRoot } =
5858
await determineProjectNameAndRootOptions(tree, {
5959
name: options.name,

packages/angular/src/generators/library/lib/normalize-options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { names, Tree } from '@nx/devkit';
22
import {
33
determineProjectNameAndRootOptions,
4-
ensureProjectName,
4+
ensureRootProjectName,
55
} from '@nx/devkit/src/generators/project-name-and-root-utils';
66
import { Linter } from '@nx/eslint';
77
import { UnitTestRunner } from '../../../utils/test-runners';
@@ -29,7 +29,7 @@ export async function normalizeOptions(
2929
...schema,
3030
};
3131

32-
await ensureProjectName(host, options, 'library');
32+
await ensureRootProjectName(options, 'library');
3333
const {
3434
projectName,
3535
names: projectNames,

packages/angular/src/generators/remote/remote.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '@nx/devkit';
99
import {
1010
determineProjectNameAndRootOptions,
11-
ensureProjectName,
11+
ensureRootProjectName,
1212
} from '@nx/devkit/src/generators/project-name-and-root-utils';
1313
import { assertNotUsingTsSolutionSetup } from '@nx/js/src/utils/typescript/ts-solution-setup';
1414
import { swcHelpersVersion } from '@nx/js/src/utils/versions';
@@ -32,7 +32,7 @@ export async function remote(tree: Tree, schema: Schema) {
3232
);
3333
}
3434

35-
await ensureProjectName(tree, options, 'application');
35+
await ensureRootProjectName(options, 'application');
3636
const { projectName: remoteProjectName } =
3737
await determineProjectNameAndRootOptions(tree, {
3838
name: options.name,

packages/cypress/src/generators/configuration/configuration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,10 @@ function createPackageJson(tree: Tree, options: NormalizedSchema) {
474474
name: importPath,
475475
version: '0.0.1',
476476
private: true,
477-
nx: {
478-
name: options.project,
479-
},
480477
};
478+
if (options.project !== importPath) {
479+
packageJson.nx = { name: options.project };
480+
}
481481
writeJson(tree, packageJsonPath, packageJson);
482482
}
483483

0 commit comments

Comments
 (0)