Skip to content

Commit cfe32c2

Browse files
authored
feat(misc): add --useProjectJson flag to CNW (#30475)
## Current Behavior Creating new workspaces since Nx 20.6.0 will generate the Nx configuration in `package.json` files. This is intended, given that it is part of the new setup using TypeScript Project References and Package Manager Workspaces, but there's no way to choose to generate the Nx configuration in `project.json` files. Project generators do allow to choose but there's no way to do it when creating a new workspace. This forces users who want to use `project.json` files to generate an empty workspace and then use a project generator. ## Expected Behavior When creating a new Nx workspace, users can provide an option (`--use-project-json`) to generate the Nx configuration in `project.json` files. ## Related Issue(s) Fixes #30464
1 parent 3d710ce commit cfe32c2

File tree

11 files changed

+36
-9
lines changed

11 files changed

+36
-9
lines changed

docs/generated/cli/create-nx-workspace.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Install `create-nx-workspace` globally to invoke the command directly, or use `n
4646
| `--style` | string | Stylesheet type to be used with certain stacks. |
4747
| `--unitTestRunner` | `jest`, `vitest`, `none` | Test runner to use for unit tests. |
4848
| `--useGitHub` | boolean | Will you be using GitHub as your git hosting provider? (Default: `false`) |
49+
| `--useProjectJson` | boolean | Use a 'project.json' file for the Nx configuration instead of a 'package.json' file. This defaults to 'true' when '--no-workspaces' is used. Otherwise, it defaults to 'false'. |
4950
| `--useReactRouter` | boolean | Generate a Server-Side Rendered (SSR) React app using React Router. |
5051
| `--version` | boolean | Show version number. |
5152
| `--workspaces` | boolean | Use package manager workspaces. (Default: `true`) |

docs/generated/packages/nx/documents/create-nx-workspace.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Install `create-nx-workspace` globally to invoke the command directly, or use `n
4646
| `--style` | string | Stylesheet type to be used with certain stacks. |
4747
| `--unitTestRunner` | `jest`, `vitest`, `none` | Test runner to use for unit tests. |
4848
| `--useGitHub` | boolean | Will you be using GitHub as your git hosting provider? (Default: `false`) |
49+
| `--useProjectJson` | boolean | Use a 'project.json' file for the Nx configuration instead of a 'package.json' file. This defaults to 'true' when '--no-workspaces' is used. Otherwise, it defaults to 'false'. |
4950
| `--useReactRouter` | boolean | Generate a Server-Side Rendered (SSR) React app using React Router. |
5051
| `--version` | boolean | Show version number. |
5152
| `--workspaces` | boolean | Use package manager workspaces. (Default: `true`) |

docs/generated/packages/workspace/generators/new.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@
108108
"description": "Whether to use package manager workspaces.",
109109
"type": "boolean",
110110
"default": true
111+
},
112+
"useProjectJson": {
113+
"type": "boolean",
114+
"description": "Use a `project.json` configuration file instead of inlining the Nx configuration in the `package.json` file."
111115
}
112116
},
113117
"additionalProperties": true,

docs/generated/packages/workspace/generators/preset.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@
125125
"description": "Whether to use package manager workspaces.",
126126
"type": "boolean",
127127
"default": true
128+
},
129+
"useProjectJson": {
130+
"type": "boolean",
131+
"description": "Use a `project.json` configuration file instead of inlining the Nx configuration in the `package.json` file."
128132
}
129133
},
130134
"required": ["preset", "name"],

packages/create-nx-workspace/bin/create-nx-workspace.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ interface BaseArguments extends CreateWorkspaceOptions {
3535
linter?: 'none' | 'eslint';
3636
formatter?: 'none' | 'prettier';
3737
workspaces?: boolean;
38+
useProjectJson?: boolean;
3839
}
3940

4041
interface NoneArguments extends BaseArguments {
@@ -175,6 +176,10 @@ export const commandsObject: yargs.Argv<Arguments> = yargs
175176
type: 'boolean',
176177
default: true,
177178
})
179+
.option('useProjectJson', {
180+
describe: chalk.dim`Use a 'project.json' file for the Nx configuration instead of a 'package.json' file. This defaults to 'true' when '--no-workspaces' is used. Otherwise, it defaults to 'false'.`,
181+
type: 'boolean',
182+
})
178183
.option('formatter', {
179184
describe: chalk.dim`Code formatter to use.`,
180185
type: 'string',
@@ -288,6 +293,7 @@ async function normalizeArgsMiddleware(
288293
});
289294

290295
argv.workspaces ??= true;
296+
argv.useProjectJson ??= !argv.workspaces;
291297

292298
try {
293299
argv.name = await determineFolder(argv);

packages/workspace/src/generators/new/generate-preset.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export function generatePreset(host: Tree, opts: NormalizedSchema) {
103103
opts.nxCloudToken ? `--nxCloudToken=${opts.nxCloudToken}` : null,
104104
opts.formatter ? `--formatter=${opts.formatter}` : null,
105105
opts.workspaces !== false ? `--workspaces` : `--no-workspaces`,
106+
opts.useProjectJson ? `--useProjectJson` : null,
106107
].filter((e) => !!e);
107108
}
108109
}

packages/workspace/src/generators/new/new.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ interface Schema {
4343
formatter?: 'none' | 'prettier';
4444
workspaces?: boolean;
4545
workspaceGlobs?: string | string[];
46+
useProjectJson?: boolean;
4647
}
4748

4849
export interface NormalizedSchema extends Schema {

packages/workspace/src/generators/new/schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@
111111
"description": "Whether to use package manager workspaces.",
112112
"type": "boolean",
113113
"default": true
114+
},
115+
"useProjectJson": {
116+
"type": "boolean",
117+
"description": "Use a `project.json` configuration file instead of inlining the Nx configuration in the `package.json` file."
114118
}
115119
},
116120
"additionalProperties": true

packages/workspace/src/generators/preset/preset.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async function createPreset(tree: Tree, options: Schema) {
8282
nxCloudToken: options.nxCloudToken,
8383
useTsSolution: options.workspaces,
8484
formatter: options.formatter,
85-
useProjectJson: !options.workspaces,
85+
useProjectJson: options.useProjectJson,
8686
});
8787
} else if (options.preset === Preset.ReactStandalone) {
8888
const { applicationGenerator: reactApplicationGenerator } = require('@nx' +
@@ -121,7 +121,7 @@ async function createPreset(tree: Tree, options: Schema) {
121121
nxCloudToken: options.nxCloudToken,
122122
useTsSolution: options.workspaces,
123123
formatter: options.formatter,
124-
useProjectJson: !options.workspaces,
124+
useProjectJson: options.useProjectJson,
125125
});
126126
} else if (options.preset === Preset.VueStandalone) {
127127
const { applicationGenerator: vueApplicationGenerator } = require('@nx' +
@@ -153,7 +153,7 @@ async function createPreset(tree: Tree, options: Schema) {
153153
nxCloudToken: options.nxCloudToken,
154154
useTsSolution: options.workspaces,
155155
formatter: options.formatter,
156-
useProjectJson: !options.workspaces,
156+
useProjectJson: options.useProjectJson,
157157
});
158158
} else if (options.preset === Preset.NuxtStandalone) {
159159
const { applicationGenerator: nuxtApplicationGenerator } = require('@nx' +
@@ -186,7 +186,7 @@ async function createPreset(tree: Tree, options: Schema) {
186186
addPlugin,
187187
useTsSolution: options.workspaces,
188188
formatter: options.formatter,
189-
useProjectJson: !options.workspaces,
189+
useProjectJson: options.useProjectJson,
190190
});
191191
} else if (options.preset === Preset.NextJsStandalone) {
192192
const { applicationGenerator: nextApplicationGenerator } = require('@nx' +
@@ -231,7 +231,7 @@ async function createPreset(tree: Tree, options: Schema) {
231231
addPlugin,
232232
useTsSolution: options.workspaces,
233233
formatter: options.formatter,
234-
useProjectJson: !options.workspaces,
234+
useProjectJson: options.useProjectJson,
235235
});
236236
} else if (options.preset === Preset.Express) {
237237
const {
@@ -246,7 +246,7 @@ async function createPreset(tree: Tree, options: Schema) {
246246
addPlugin,
247247
useTsSolution: options.workspaces,
248248
formatter: options.formatter,
249-
useProjectJson: !options.workspaces,
249+
useProjectJson: options.useProjectJson,
250250
});
251251
} else if (options.preset === Preset.ReactNative) {
252252
const { reactNativeApplicationGenerator } = require('@nx' +
@@ -262,7 +262,7 @@ async function createPreset(tree: Tree, options: Schema) {
262262
bundler: options.bundler ?? 'webpack',
263263
useTsSolution: options.workspaces,
264264
formatter: options.formatter,
265-
useProjectJson: !options.workspaces,
265+
useProjectJson: options.useProjectJson,
266266
});
267267
} else if (options.preset === Preset.Expo) {
268268
const { expoApplicationGenerator } = require('@nx' + '/expo');
@@ -276,7 +276,7 @@ async function createPreset(tree: Tree, options: Schema) {
276276
nxCloudToken: options.nxCloudToken,
277277
useTsSolution: options.workspaces,
278278
formatter: options.formatter,
279-
useProjectJson: !options.workspaces,
279+
useProjectJson: options.useProjectJson,
280280
});
281281
} else if (options.preset === Preset.TS) {
282282
const { initGenerator } = require('@nx' + '/js');
@@ -330,7 +330,7 @@ async function createPreset(tree: Tree, options: Schema) {
330330
addPlugin,
331331
useTsSolution: options.workspaces,
332332
formatter: options.formatter,
333-
useProjectJson: !options.workspaces,
333+
useProjectJson: options.useProjectJson,
334334
});
335335
} else {
336336
throw new Error(`Invalid preset ${options.preset}`);

packages/workspace/src/generators/preset/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ export interface Schema {
2525
serverRouting?: boolean;
2626
prefix?: string;
2727
nxCloudToken?: string;
28+
useProjectJson?: boolean;
2829
}

packages/workspace/src/generators/preset/schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@
128128
"description": "Whether to use package manager workspaces.",
129129
"type": "boolean",
130130
"default": true
131+
},
132+
"useProjectJson": {
133+
"type": "boolean",
134+
"description": "Use a `project.json` configuration file instead of inlining the Nx configuration in the `package.json` file."
131135
}
132136
},
133137
"required": ["preset", "name"]

0 commit comments

Comments
 (0)