Skip to content

Commit bd1b0b7

Browse files
authored
feat(misc): introduce a way to set the project name/root format for all generators (#18971)
1 parent 0cc6ba9 commit bd1b0b7

File tree

14 files changed

+107
-35
lines changed

14 files changed

+107
-35
lines changed

docs/generated/devkit/NxJsonConfiguration.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ Where new apps + libs should be placed
191191

192192
#### Type declaration
193193

194-
| Name | Type |
195-
| :-------- | :------- |
196-
| `appsDir` | `string` |
197-
| `libsDir` | `string` |
194+
| Name | Type |
195+
| :-------------------------- | :----------------------------- |
196+
| `appsDir?` | `string` |
197+
| `libsDir?` | `string` |
198+
| `projectNameAndRootFormat?` | `"as-provided"` \| `"derived"` |

docs/generated/devkit/Workspace.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,11 @@ Where new apps + libs should be placed
267267

268268
#### Type declaration
269269

270-
| Name | Type |
271-
| :-------- | :------- |
272-
| `appsDir` | `string` |
273-
| `libsDir` | `string` |
270+
| Name | Type |
271+
| :-------------------------- | :----------------------------- |
272+
| `appsDir?` | `string` |
273+
| `libsDir?` | `string` |
274+
| `projectNameAndRootFormat?` | `"as-provided"` \| `"derived"` |
274275

275276
#### Inherited from
276277

e2e/linter/src/linter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ describe('Linter', () => {
558558
bundler: 'vite',
559559
e2eTestRunner: 'none',
560560
});
561-
runCLI(`generate @nx/js:lib ${mylib}`);
561+
runCLI(`generate @nx/js:lib ${mylib} --directory libs/${mylib}`);
562562

563563
// migrate to flat structure
564564
runCLI(`generate @nx/linter:convert-to-flat-config`);

e2e/utils/create-project-utils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
RunCmdOpts,
2929
runCommand,
3030
} from './command-utils';
31-
import { output } from '@nx/devkit';
31+
import { NxJsonConfiguration, output } from '@nx/devkit';
3232
import { readFileSync } from 'fs';
3333
import { join } from 'path';
3434

@@ -41,6 +41,7 @@ let projName: string;
4141
export function newProject({
4242
name = uniq('proj'),
4343
packageManager = getSelectedPackageManager(),
44+
unsetProjectNameAndRootFormat = true,
4445
} = {}): string {
4546
try {
4647
const projScope = 'proj';
@@ -51,6 +52,13 @@ export function newProject({
5152
packageManager,
5253
});
5354

55+
if (unsetProjectNameAndRootFormat) {
56+
updateJson<NxJsonConfiguration>('nx.json', (nxJson) => {
57+
delete nxJson.workspaceLayout;
58+
return nxJson;
59+
});
60+
}
61+
5462
// Temporary hack to prevent installing with `--frozen-lockfile`
5563
if (isCI && packageManager === 'pnpm') {
5664
updateFile(

packages/devkit/src/generators/project-name-and-root-utils.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,21 @@ describe('determineProjectNameAndRootOptions', () => {
348348

349349
expect(promptSpy).toHaveBeenCalledTimes(2);
350350

351-
expect(readNxJson(tree).generators['@nx/some-plugin:app']).toEqual({
351+
expect(readNxJson(tree).workspaceLayout).toEqual({
352352
projectNameAndRootFormat: 'as-provided',
353353
});
354354

355+
promptSpy.mockReset();
356+
357+
await determineProjectNameAndRootOptions(tree, {
358+
name: 'libName',
359+
projectType: 'library',
360+
directory: 'shared',
361+
callingGenerator: '@nx/some-plugin:app',
362+
});
363+
364+
expect(promptSpy).not.toHaveBeenCalled();
365+
355366
// restore original interactive mode
356367
restoreOriginalInteractiveMode();
357368
});

packages/devkit/src/generators/project-name-and-root-utils.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ export async function determineProjectNameAndRootOptions(
7575
const formats = getProjectNameAndRootFormats(tree, options);
7676
const format =
7777
options.projectNameAndRootFormat ??
78-
(await determineFormat(tree, formats, options.callingGenerator));
78+
(getDefaultProjectNameAndRootFormat(tree) === 'as-provided'
79+
? 'as-provided'
80+
: await determineFormat(tree, formats, options.callingGenerator));
7981

8082
return {
8183
...formats[format],
@@ -167,11 +169,7 @@ async function determineFormat(
167169
initial: true,
168170
});
169171
if (saveDefault) {
170-
const nxJson = readNxJson(tree);
171-
nxJson.generators ??= {};
172-
nxJson.generators[callingGenerator] ??= {};
173-
nxJson.generators[callingGenerator].projectNameAndRootFormat = result;
174-
updateNxJson(tree, nxJson);
172+
setProjectNameAndRootFormatDefault(tree);
175173
} else {
176174
logger.warn(deprecationWarning);
177175
}
@@ -183,6 +181,18 @@ async function determineFormat(
183181
return result;
184182
}
185183

184+
function setProjectNameAndRootFormatDefault(tree: Tree) {
185+
const nxJson = readNxJson(tree);
186+
nxJson.workspaceLayout ??= {};
187+
nxJson.workspaceLayout.projectNameAndRootFormat = 'as-provided';
188+
updateNxJson(tree, nxJson);
189+
}
190+
191+
function getDefaultProjectNameAndRootFormat(tree: Tree) {
192+
const nxJson = readNxJson(tree);
193+
return nxJson.workspaceLayout?.projectNameAndRootFormat ?? 'derived';
194+
}
195+
186196
function getProjectNameAndRootFormats(
187197
tree: Tree,
188198
options: ProjectGenerationOptions

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function toNodeApplicationGeneratorOptions(
4141
name: options.name,
4242
directory: options.directory,
4343
frontendProject: options.frontendProject,
44+
projectNameAndRootFormat: options.projectNameAndRootFormat,
4445
linter: options.linter,
4546
skipFormat: true,
4647
skipPackageJson: options.skipPackageJson,

packages/nx/schemas/nx-schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
"appsDir": {
5757
"type": "string",
5858
"description": "Default folder name for apps."
59+
},
60+
"projectNameAndRootFormat": {
61+
"type": "string",
62+
"description": "Default method of handling arguments for generating projects",
63+
"enum": ["as-provided", "derived"]
5964
}
6065
},
6166
"additionalProperties": false

packages/nx/src/config/nx-json.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
8686
* Where new apps + libs should be placed
8787
*/
8888
workspaceLayout?: {
89-
libsDir: string;
90-
appsDir: string;
89+
libsDir?: string;
90+
appsDir?: string;
91+
projectNameAndRootFormat?: 'as-provided' | 'derived';
9192
};
9293
/**
9394
* Available Task Runners

packages/workspace/src/generators/new/__snapshots__/new.spec.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,8 @@ exports[`new should generate an empty nx.json 1`] = `
5353
"runner": "nx/tasks-runners/default",
5454
},
5555
},
56+
"workspaceLayout": {
57+
"projectNameAndRootFormat": "as-provided",
58+
},
5659
}
5760
`;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy