create-nx-workspace 23.2.0-beta.8 → 23.2.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,12 @@
1
1
  import yargs from 'yargs';
2
2
  import { CreateWorkspaceOptions } from '../src/create-workspace-options';
3
3
  import { Preset } from '../src/utils/preset/preset';
4
- import { type Linter } from '../src/internal-utils/prompts';
4
+ import { type Formatter, type Linter } from '../src/internal-utils/prompts';
5
5
  type AngularUnitTestRunner = 'none' | 'jest' | 'vitest-angular' | 'vitest-analog';
6
6
  interface BaseArguments extends CreateWorkspaceOptions {
7
7
  preset?: Preset;
8
8
  linter?: Linter;
9
- formatter?: 'none' | 'prettier';
9
+ formatter?: Formatter;
10
10
  workspaces?: boolean;
11
11
  useProjectJson?: boolean;
12
12
  }
@@ -119,6 +119,9 @@ exports.commandsObject = yargs_1.default
119
119
  .option('formatter', {
120
120
  describe: chalk_1.default.dim `Code formatter to use.`,
121
121
  type: 'string',
122
+ // Fail at the CLI on a typo rather than downstream in schema
123
+ // validation, where the message is much further from the cause.
124
+ choices: prompts_1.FORMATTERS,
122
125
  })
123
126
  // `choices` is load-bearing, not documentation: `determineLinterOptions`
124
127
  // returns `--linter` as-is and nothing downstream validates it — the
@@ -842,24 +845,15 @@ async function determinePresetOptions(parsedArgs) {
842
845
  }
843
846
  /**
844
847
  * `web-components` prompts for nothing of its own — `style` and `e2eTestRunner`
845
- * come from the CLI or fall back downstream — but the preset does generate a
846
- * lintable app, so it still needs a linter.
848
+ * come from the CLI or fall back downstream — but the preset does generate an
849
+ * app that is both linted and formatted, so it needs both answers.
847
850
  */
848
851
  async function determineWebOptions(parsedArgs) {
849
- return { linter: await (0, prompts_1.determineLinterOptions)(parsedArgs) };
850
- }
851
- async function determineFormatterOptions(args, opts) {
852
- if (args.formatter)
853
- return args.formatter;
854
- // A skipped run answers yes regardless of `preferPrettier`, which only moves
855
- // the highlighted option when the prompt is shown.
856
- const usePrettier = await (0, prompt_helpers_1.confirmationPrompt)({
857
- message: `Would you like to use Prettier for code formatting?`,
858
- initial: opts?.preferPrettier ?? false,
859
- skip: !args.interactive || (0, is_ci_1.isCI)(),
860
- skippedValue: true,
861
- });
862
- return usePrettier ? 'prettier' : 'none';
852
+ // Property order is the prompt order here - each `await` runs where it sits.
853
+ return {
854
+ formatter: await (0, prompts_1.determineFormatterOptions)(parsedArgs),
855
+ linter: await (0, prompts_1.determineLinterOptions)(parsedArgs),
856
+ };
863
857
  }
864
858
  async function determineNoneOptions(parsedArgs) {
865
859
  if ((!parsedArgs.preset || parsedArgs.preset === preset_1.Preset.TS) &&
@@ -867,7 +861,7 @@ async function determineNoneOptions(parsedArgs) {
867
861
  parsedArgs.workspaces) {
868
862
  return {
869
863
  preset: preset_1.Preset.TS,
870
- formatter: await determineFormatterOptions(parsedArgs),
864
+ formatter: await (0, prompts_1.determineFormatterOptions)(parsedArgs),
871
865
  };
872
866
  }
873
867
  else {
@@ -891,7 +885,9 @@ async function determineNoneOptions(parsedArgs) {
891
885
  }
892
886
  }
893
887
  if (preset === preset_1.Preset.TS) {
894
- return { preset, formatter: 'prettier' };
888
+ // Asked here too: the workspaces variant above already prompts, and this
889
+ // preset reaches `@nx/js:init`, which sets the formatter up either way.
890
+ return { preset, formatter: await (0, prompts_1.determineFormatterOptions)(parsedArgs) };
895
891
  }
896
892
  if (parsedArgs.js !== undefined) {
897
893
  js = parsedArgs.js;
@@ -905,15 +901,30 @@ async function determineNoneOptions(parsedArgs) {
905
901
  skippedValue: true,
906
902
  }));
907
903
  }
908
- // `ts-standalone` is the only preset on this stack that generates a
909
- // lintable project; `apps`, `ts` and `npm` reach no generator that takes a
910
- // linter, so asking would discard the answer.
904
+ // `ts-standalone` is the only preset left to ask here: `ts` returned above
905
+ // with its own prompt, and `apps` and `npm` generate no project, so nothing
906
+ // downstream would apply an answer. An explicit `--formatter` still carries.
907
+ const formatter = preset === preset_1.Preset.TsStandalone
908
+ ? await (0, prompts_1.determineFormatterOptions)(parsedArgs)
909
+ : parsedArgs.formatter;
910
+ // Same rule for the linter: `ts-standalone` is also the only preset here
911
+ // that generates a lintable project, so asking would discard the answer.
911
912
  const linter = preset === preset_1.Preset.TsStandalone
912
913
  ? await (0, prompts_1.determineLinterOptions)(parsedArgs)
913
914
  : undefined;
914
- // Omitted rather than set to `undefined`: the caller `Object.assign`s this
915
- // over `argv`, so an explicit key would clobber a user's `--linter`.
916
- return { preset, js, appName, ...(linter ? { linter } : {}) };
915
+ // Both keys are omitted rather than set to `undefined`: the caller
916
+ // `Object.assign`s this over `argv`, so an explicit key would clobber the
917
+ // user's own flag. Forwarding `--formatter` only when it was given is what
918
+ // keeps a plain `--preset=npm` identical to before - with nothing set,
919
+ // `@nx/workspace:new`'s `"default": "none"` applies and no formatter is
920
+ // installed, which is these presets' long-standing behaviour.
921
+ return {
922
+ preset,
923
+ js,
924
+ appName,
925
+ ...(formatter ? { formatter } : {}),
926
+ ...(linter ? { linter } : {}),
927
+ };
917
928
  }
918
929
  }
919
930
  async function determineReactOptions(parsedArgs) {
@@ -928,7 +939,6 @@ async function determineReactOptions(parsedArgs) {
928
939
  let nextAppDir = false;
929
940
  let nextSrcDir = false;
930
941
  let linter;
931
- let formatter;
932
942
  const workspaces = parsedArgs.workspaces;
933
943
  if (parsedArgs.preset && parsedArgs.preset !== preset_1.Preset.React) {
934
944
  preset = parsedArgs.preset;
@@ -1021,8 +1031,10 @@ async function determineReactOptions(parsedArgs) {
1021
1031
  };
1022
1032
  style = reply.style;
1023
1033
  }
1024
- // Asked outside the gate: the linter is independent of package-manager
1025
- // workspaces, and `--no-workspaces` used to force ESLint without asking.
1034
+ // Asked outside the gate: neither answer depends on package-manager
1035
+ // workspaces, and `--no-workspaces` used to force prettier and ESLint
1036
+ // without asking.
1037
+ const formatter = await (0, prompts_1.determineFormatterOptions)(parsedArgs);
1026
1038
  linter = await (0, prompts_1.determineLinterOptions)(parsedArgs);
1027
1039
  if (preset === preset_1.Preset.ReactStandalone || preset === preset_1.Preset.ReactMonorepo) {
1028
1040
  unitTestRunner = await determineUnitTestRunner(parsedArgs, {
@@ -1039,14 +1051,6 @@ async function determineReactOptions(parsedArgs) {
1039
1051
  });
1040
1052
  e2eTestRunner = await determineE2eTestRunner(parsedArgs);
1041
1053
  }
1042
- if (workspaces) {
1043
- formatter = await determineFormatterOptions(parsedArgs, {
1044
- preferPrettier: true,
1045
- });
1046
- }
1047
- else {
1048
- formatter = 'prettier';
1049
- }
1050
1054
  return {
1051
1055
  preset,
1052
1056
  style,
@@ -1070,7 +1074,6 @@ async function determineVueOptions(parsedArgs) {
1070
1074
  let unitTestRunner = undefined;
1071
1075
  let e2eTestRunner = undefined;
1072
1076
  let linter;
1073
- let formatter;
1074
1077
  const workspaces = parsedArgs.workspaces;
1075
1078
  if (parsedArgs.preset && parsedArgs.preset !== preset_1.Preset.Vue) {
1076
1079
  preset = parsedArgs.preset;
@@ -1134,21 +1137,15 @@ async function determineVueOptions(parsedArgs) {
1134
1137
  };
1135
1138
  style = reply.style;
1136
1139
  }
1137
- // Asked outside the gate: the linter is independent of package-manager
1138
- // workspaces, and `--no-workspaces` used to force ESLint without asking.
1140
+ // Asked outside the gate: neither answer depends on package-manager
1141
+ // workspaces, and `--no-workspaces` used to force prettier and ESLint
1142
+ // without asking.
1143
+ const formatter = await (0, prompts_1.determineFormatterOptions)(parsedArgs);
1139
1144
  linter = await (0, prompts_1.determineLinterOptions)(parsedArgs);
1140
1145
  unitTestRunner = await determineUnitTestRunner(parsedArgs, {
1141
1146
  exclude: 'jest',
1142
1147
  });
1143
1148
  e2eTestRunner = await determineE2eTestRunner(parsedArgs);
1144
- if (workspaces) {
1145
- formatter = await determineFormatterOptions(parsedArgs, {
1146
- preferPrettier: true,
1147
- });
1148
- }
1149
- else {
1150
- formatter = 'prettier';
1151
- }
1152
1149
  return {
1153
1150
  preset,
1154
1151
  style,
@@ -1172,6 +1169,7 @@ async function determineAngularOptions(parsedArgs) {
1172
1169
  const routing = parsedArgs.routing;
1173
1170
  const prefix = parsedArgs.prefix;
1174
1171
  const zoneless = parsedArgs.zoneless;
1172
+ const workspaces = parsedArgs.workspaces;
1175
1173
  if (prefix) {
1176
1174
  // https://github.com/angular/angular-cli/blob/main/packages/schematics/angular/utility/validation.ts#L11-L14
1177
1175
  const htmlSelectorRegex = /^[a-zA-Z][.0-9a-zA-Z]*((:?-[0-9]+)*|(:?-[a-zA-Z][.0-9a-zA-Z]*(:?-[0-9]+)*)*)$/;
@@ -1264,6 +1262,7 @@ async function determineAngularOptions(parsedArgs) {
1264
1262
  };
1265
1263
  ssr = reply.ssr === 'Yes';
1266
1264
  }
1265
+ const formatter = await (0, prompts_1.determineFormatterOptions)(parsedArgs);
1267
1266
  const linter = await (0, prompts_1.determineLinterOptions)(parsedArgs);
1268
1267
  if (parsedArgs.unitTestRunner) {
1269
1268
  unitTestRunner = parsedArgs.unitTestRunner;
@@ -1313,6 +1312,8 @@ async function determineAngularOptions(parsedArgs) {
1313
1312
  prefix,
1314
1313
  zoneless,
1315
1314
  linter,
1315
+ formatter,
1316
+ workspaces,
1316
1317
  };
1317
1318
  }
1318
1319
  async function determineNodeOptions(parsedArgs) {
@@ -1321,7 +1322,6 @@ async function determineNodeOptions(parsedArgs) {
1321
1322
  let framework;
1322
1323
  let docker;
1323
1324
  let linter;
1324
- let formatter;
1325
1325
  let unitTestRunner = undefined;
1326
1326
  const workspaces = parsedArgs.workspaces;
1327
1327
  if (parsedArgs.preset) {
@@ -1371,20 +1371,14 @@ async function determineNodeOptions(parsedArgs) {
1371
1371
  };
1372
1372
  docker = reply.docker === 'Yes';
1373
1373
  }
1374
- // Asked outside the gate: the linter is independent of package-manager
1375
- // workspaces, and `--no-workspaces` used to force ESLint without asking.
1374
+ // Asked outside the gate: neither answer depends on package-manager
1375
+ // workspaces, and `--no-workspaces` used to force prettier and ESLint
1376
+ // without asking.
1377
+ const formatter = await (0, prompts_1.determineFormatterOptions)(parsedArgs);
1376
1378
  linter = await (0, prompts_1.determineLinterOptions)(parsedArgs);
1377
1379
  unitTestRunner = await determineUnitTestRunner(parsedArgs, {
1378
1380
  exclude: 'vitest',
1379
1381
  });
1380
- if (workspaces) {
1381
- formatter = await determineFormatterOptions(parsedArgs, {
1382
- preferPrettier: true,
1383
- });
1384
- }
1385
- else {
1386
- formatter = 'prettier';
1387
- }
1388
1382
  return {
1389
1383
  preset,
1390
1384
  appName,
@@ -33,6 +33,12 @@ async function createWorkspace(preset, options, rawArgs) {
33
33
  if (cliName) {
34
34
  output_1.output.setCliName(cliName ?? 'NX');
35
35
  }
36
+ // Skip formatting during generation - the single pass at the end covers
37
+ // everything once dependencies are on disk. A user who set this deliberately
38
+ // still gets no formatting at all, so remember their value rather than
39
+ // silently taking it over.
40
+ const skipFormatRequested = process.env.NX_SKIP_FORMAT === 'true';
41
+ process.env.NX_SKIP_FORMAT = 'true';
36
42
  let directory;
37
43
  if (options.template) {
38
44
  // Resolve shorthand template names to full GitHub org/repo format
@@ -151,6 +157,45 @@ async function createWorkspace(preset, options, rawArgs) {
151
157
  const ciProvider = nxCloud === 'yes' ? 'github' : nxCloud;
152
158
  await (0, setup_ci_1.setupCI)(directory, ciProvider, packageManager);
153
159
  }
160
+ // Only now are dependencies on disk: the passes inside `@nx/workspace:new`
161
+ // run before the install, when no formatter resolves yet.
162
+ if (skipFormatRequested) {
163
+ process.env.NX_SKIP_FORMAT = 'true';
164
+ }
165
+ else {
166
+ delete process.env.NX_SKIP_FORMAT;
167
+ }
168
+ if (!skipFormatRequested && workspaceHasFormatter(directory)) {
169
+ try {
170
+ const pmc = (0, package_manager_1.getPackageManagerCommand)(packageManager);
171
+ // `--all` because git is not initialised yet, so there is nothing to diff
172
+ // changed files against.
173
+ await (0, child_process_utils_1.execAndWait)(`${pmc.exec} nx format --all`, directory);
174
+ }
175
+ catch (e) {
176
+ // `execAndWait` names a log file only when there was no output at all,
177
+ // and that file is deleted below - so report the exit code instead.
178
+ const reason = e?.logFile && e?.message?.includes(e.logFile)
179
+ ? `The command failed with exit code ${e.exitCode ?? 'unknown'} and produced no output.`
180
+ : e?.message;
181
+ output_1.output.warn({
182
+ title: 'Could not format the new workspace.',
183
+ bodyLines: [
184
+ 'The workspace was created successfully, but its files are not formatted.',
185
+ ...(reason ? [reason] : []),
186
+ 'Run "nx format:write" inside the workspace to format them.',
187
+ ],
188
+ });
189
+ // Otherwise `initializeGitRepo`'s `git add .` puts an error.log in the
190
+ // workspace's first commit; the .gitignore templates do not cover it.
191
+ if (e?.logFile && (0, node_fs_1.existsSync)(e.logFile)) {
192
+ try {
193
+ (0, node_fs_1.unlinkSync)(e.logFile);
194
+ }
195
+ catch { }
196
+ }
197
+ }
198
+ }
154
199
  let pushedToVcs = git_1.VcsPushStatus.SkippedGit;
155
200
  let pushFailReason;
156
201
  if (!skipGit) {
@@ -296,3 +341,28 @@ function getWorkspaceGlobsFromPreset(preset) {
296
341
  return ['packages/*'];
297
342
  }
298
343
  }
344
+ /**
345
+ * Asks the workspace rather than the caller. `formatter` is only populated for
346
+ * the known-preset stacks, so trusting it skips the pass for third-party
347
+ * presets and templates - which is the only formatting those flows get, since
348
+ * everything before the install runs under `NX_SKIP_FORMAT`.
349
+ *
350
+ * Formats when detection cannot run: an unformatted new workspace fails its own
351
+ * `nx format:check`, which is worse than a spawn that finds nothing to do.
352
+ */
353
+ function workspaceHasFormatter(directory) {
354
+ try {
355
+ // nx-ignore-next-line
356
+ const { detectFormatter } = require(require.resolve('nx/src/devkit-internals', {
357
+ paths: [directory],
358
+ // nx-ignore-next-line
359
+ })
360
+ // Typed locally: `detectFormatter` is new in this release, so the
361
+ // published nx these types resolve against does not export it.
362
+ );
363
+ return detectFormatter(directory) !== null;
364
+ }
365
+ catch {
366
+ return true;
367
+ }
368
+ }
@@ -55,3 +55,9 @@ export declare function determineLinterOptions(args: {
55
55
  linter?: Linter;
56
56
  interactive?: boolean;
57
57
  }): Promise<Linter>;
58
+ export type Formatter = 'oxfmt' | 'prettier' | 'none';
59
+ export declare const FORMATTERS: readonly ["oxfmt", "prettier", "none"];
60
+ export declare function determineFormatterOptions(args: {
61
+ formatter?: Formatter;
62
+ interactive?: boolean;
63
+ }): Promise<Formatter>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LINTERS = void 0;
3
+ exports.FORMATTERS = exports.LINTERS = void 0;
4
4
  exports.determineNxCloud = determineNxCloud;
5
5
  exports.determineNxCloudV2 = determineNxCloudV2;
6
6
  exports.determineIfGitHubWillBeUsed = determineIfGitHubWillBeUsed;
@@ -11,6 +11,7 @@ exports.determineDefaultBase = determineDefaultBase;
11
11
  exports.confirmThirdPartyPreset = confirmThirdPartyPreset;
12
12
  exports.determinePackageManager = determinePackageManager;
13
13
  exports.determineLinterOptions = determineLinterOptions;
14
+ exports.determineFormatterOptions = determineFormatterOptions;
14
15
  const tslib_1 = require("tslib");
15
16
  const chalk_1 = tslib_1.__importDefault(require("chalk"));
16
17
  const ab_testing_1 = require("../utils/nx/ab-testing");
@@ -268,3 +269,33 @@ async function determineLinterOptions(args) {
268
269
  initial: 'eslint',
269
270
  });
270
271
  }
272
+ // Order here is incidental; the prompt's own choice order sets the default. `satisfies` stops a typo
273
+ // getting in, and the coverage assertion below stops a member being dropped -
274
+ // on its own the array would happily be a subset, and the prompt would then
275
+ // reject a value the generator schemas still accept.
276
+ exports.FORMATTERS = [
277
+ 'oxfmt',
278
+ 'prettier',
279
+ 'none',
280
+ ];
281
+ const _formattersAreExhaustive = true;
282
+ async function determineFormatterOptions(args) {
283
+ if (args.formatter)
284
+ return args.formatter;
285
+ // Prettier is the non-interactive default; changing it means changing this
286
+ // line, not the option order.
287
+ if (!args.interactive || (0, is_ci_1.isCI)())
288
+ return 'prettier';
289
+ return (0, prompt_helpers_1.selectPrompt)({
290
+ message: `Which code formatter would you like to use?`,
291
+ // `value` is what's returned; `label` is what the list shows. oxfmt is
292
+ // labelled so it isn't presented as an equal of Prettier while it is
293
+ // pre-1.0.
294
+ choices: [
295
+ { value: 'prettier', label: 'prettier' },
296
+ { value: 'oxfmt', label: 'oxfmt (experimental)' },
297
+ { value: 'none', label: 'none' },
298
+ ],
299
+ initial: 'prettier',
300
+ });
301
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-nx-workspace",
3
- "version": "23.2.0-beta.8",
3
+ "version": "23.2.0-beta.9",
4
4
  "private": false,
5
5
  "description": "Smart Monorepos · Fast Builds",
6
6
  "repository": {