@nx/devkit 19.9.0-canary.20240921-a510b36 → 20.0.0-beta.1

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/devkit",
3
- "version": "19.9.0-canary.20240921-a510b36",
3
+ "version": "20.0.0-beta.1",
4
4
  "private": false,
5
5
  "description": "The Nx Devkit is used to customize Nx for different technologies and use cases. It contains many utility functions for reading and writing files, updating configuration, working with Abstract Syntax Trees(ASTs), and more. Learn more about [extending Nx by leveraging the Nx Devkit](https://nx.dev/extending-nx/intro/getting-started) on our docs.",
6
6
  "repository": {
@@ -29,14 +29,13 @@
29
29
  "homepage": "https://nx.dev",
30
30
  "dependencies": {
31
31
  "ejs": "^3.1.7",
32
- "enquirer": "~2.3.6",
33
32
  "ignore": "^5.0.4",
34
33
  "tmp": "~0.2.1",
35
34
  "tslib": "^2.3.0",
36
35
  "semver": "^7.5.3",
37
36
  "yargs-parser": "21.1.1",
38
37
  "minimatch": "9.0.3",
39
- "@nrwl/devkit": "19.9.0-canary.20240921-a510b36"
38
+ "@nrwl/devkit": "20.0.0-beta.1"
40
39
  },
41
40
  "peerDependencies": {
42
41
  "nx": ">= 17 <= 20"
@@ -1,20 +1,12 @@
1
- import { Tree } from 'nx/src/devkit-exports';
2
- export type NameAndDirectoryFormat = 'as-provided' | 'derived';
1
+ import { type Tree } from 'nx/src/devkit-exports';
2
+ export type NameAndDirectoryFormat = 'as-provided';
3
3
  export type ArtifactGenerationOptions = {
4
- artifactType: string;
5
- callingGenerator: string | null;
6
4
  name: string;
7
5
  directory?: string;
8
- disallowPathInNameForDerived?: boolean;
9
6
  fileExtension?: 'js' | 'jsx' | 'ts' | 'tsx' | 'vue';
10
7
  fileName?: string;
11
- flat?: boolean;
12
8
  nameAndDirectoryFormat?: NameAndDirectoryFormat;
13
- pascalCaseDirectory?: boolean;
14
- pascalCaseFile?: boolean;
15
- project?: string;
16
9
  suffix?: string;
17
- derivedDirectory?: string;
18
10
  };
19
11
  export type NameAndDirectoryOptions = {
20
12
  /**
@@ -3,65 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.determineArtifactNameAndDirectoryOptions = determineArtifactNameAndDirectoryOptions;
4
4
  exports.getRelativeCwd = getRelativeCwd;
5
5
  exports.setCwd = setCwd;
6
- const enquirer_1 = require("enquirer");
7
- const path_1 = require("path");
8
- const names_1 = require("../utils/names");
9
6
  const devkit_exports_1 = require("nx/src/devkit-exports");
10
7
  const devkit_internals_1 = require("nx/src/devkit-internals");
8
+ const path_1 = require("path");
11
9
  async function determineArtifactNameAndDirectoryOptions(tree, options) {
12
- if (!options.nameAndDirectoryFormat &&
13
- (process.env.NX_INTERACTIVE !== 'true' || !isTTY())) {
14
- options.nameAndDirectoryFormat = 'derived';
15
- }
16
- const formats = getNameAndDirectoryOptionFormats(tree, options);
17
- const format = options.nameAndDirectoryFormat ?? (await determineFormat(formats, options));
18
- validateResolvedProject(tree, formats[format]?.project, options, formats[format]?.directory);
19
- if (format === 'derived' && options.callingGenerator) {
20
- logDeprecationMessage(options, formats);
21
- }
10
+ const nameAndDirectoryOptions = getNameAndDirectoryOptions(tree, options);
11
+ validateResolvedProject(tree, nameAndDirectoryOptions.project, options, nameAndDirectoryOptions.directory);
22
12
  return {
23
- ...formats[format],
24
- nameAndDirectoryFormat: format,
13
+ ...nameAndDirectoryOptions,
14
+ nameAndDirectoryFormat: 'as-provided',
25
15
  };
26
16
  }
27
- async function determineFormat(formats, options) {
28
- if (!formats.derived) {
29
- return 'as-provided';
30
- }
31
- const asProvidedDescription = `As provided: ${formats['as-provided'].filePath}`;
32
- const asProvidedSelectedValue = formats['as-provided'].filePath;
33
- const derivedDescription = `Derived: ${formats['derived'].filePath}`;
34
- const derivedSelectedValue = formats['derived'].filePath;
35
- if (asProvidedSelectedValue === derivedSelectedValue) {
36
- return 'as-provided';
37
- }
38
- const result = await (0, enquirer_1.prompt)({
39
- type: 'select',
40
- name: 'format',
41
- message: `Where should the ${options.artifactType} be generated?`,
42
- choices: [
43
- {
44
- message: asProvidedDescription,
45
- name: asProvidedSelectedValue,
46
- },
47
- {
48
- message: derivedDescription,
49
- name: derivedSelectedValue,
50
- },
51
- ],
52
- initial: 0,
53
- }).then(({ format }) => format === asProvidedSelectedValue ? 'as-provided' : 'derived');
54
- return result;
55
- }
56
- function logDeprecationMessage(options, formats) {
57
- devkit_exports_1.logger.warn(`
58
- In Nx 20, generating a ${options.artifactType} will no longer support providing a project and deriving the directory.
59
- Please provide the exact directory in the future.
60
- Example: nx g ${options.callingGenerator} ${formats['derived'].artifactName} --directory ${formats['derived'].directory}
61
- NOTE: The example above assumes the command is being run from the workspace root. If the command is being run from a subdirectory, the directory option should be adjusted accordingly.
62
- `);
63
- }
64
- function getNameAndDirectoryOptionFormats(tree, options) {
17
+ function getNameAndDirectoryOptions(tree, options) {
65
18
  const directory = options.directory
66
19
  ? (0, devkit_exports_1.normalizePath)(options.directory.replace(/^\.?\//, ''))
67
20
  : undefined;
@@ -77,43 +30,7 @@ function getNameAndDirectoryOptionFormats(tree, options) {
77
30
  fileExtension,
78
31
  name: extractedName,
79
32
  });
80
- if (!options.project) {
81
- validateResolvedProject(tree, asProvidedOptions.project, options, asProvidedOptions.directory);
82
- }
83
- if (options.nameAndDirectoryFormat === 'as-provided') {
84
- return {
85
- 'as-provided': asProvidedOptions,
86
- derived: undefined,
87
- };
88
- }
89
- if (options.disallowPathInNameForDerived && options.name.includes('/')) {
90
- if (!options.nameAndDirectoryFormat) {
91
- devkit_exports_1.output.warn({
92
- title: `The provided name "${options.name}" contains a path and this is not supported by the "${options.callingGenerator}" when using the "derived" format.`,
93
- bodyLines: [
94
- `The generator will try to generate the ${options.artifactType} using the "as-provided" format at "${asProvidedOptions.filePath}".`,
95
- ],
96
- });
97
- return {
98
- 'as-provided': asProvidedOptions,
99
- derived: undefined,
100
- };
101
- }
102
- throw new Error(`The provided name "${options.name}" contains a path and this is not supported by the "${options.callingGenerator}" when using the "derived" format. ` +
103
- `Please provide a name without a path or use the "as-provided" format.`);
104
- }
105
- const derivedOptions = getDerivedOptions(tree, {
106
- ...options,
107
- directory,
108
- fileExtension,
109
- name: extractedName,
110
- }, asProvidedOptions, !options.disallowPathInNameForDerived && extractedDirectory
111
- ? extractedDirectory
112
- : undefined);
113
- return {
114
- 'as-provided': asProvidedOptions,
115
- derived: derivedOptions,
116
- };
33
+ return asProvidedOptions;
117
34
  }
118
35
  function getAsProvidedOptions(tree, options) {
119
36
  const relativeCwd = getRelativeCwd();
@@ -143,61 +60,6 @@ function getAsProvidedOptions(tree, options) {
143
60
  project: asProvidedProject,
144
61
  };
145
62
  }
146
- function getDerivedOptions(tree, options, asProvidedOptions, extractedDirectory) {
147
- const projects = (0, devkit_exports_1.getProjects)(tree);
148
- if (options.project && !projects.has(options.project)) {
149
- throw new Error(`The provided project "${options.project}" does not exist! Please provide an existing project name.`);
150
- }
151
- const projectName = options.project ?? asProvidedOptions.project;
152
- const project = projects.get(projectName);
153
- const derivedName = options.name;
154
- const baseDirectory = options.directory
155
- ? (0, names_1.names)(options.directory).fileName
156
- : (0, devkit_exports_1.joinPathFragments)(project.sourceRoot ?? (0, devkit_exports_1.joinPathFragments)(project.root, 'src'), project.projectType === 'application' ? 'app' : 'lib', extractedDirectory ?? '');
157
- const derivedDirectory = typeof options.derivedDirectory === 'string'
158
- ? (0, devkit_exports_1.joinPathFragments)(project.sourceRoot ?? project.root, options.derivedDirectory, options.flat
159
- ? ''
160
- : options.pascalCaseDirectory
161
- ? (0, names_1.names)(derivedName).className
162
- : (0, names_1.names)(derivedName).fileName)
163
- : options.flat
164
- ? (0, devkit_exports_1.normalizePath)(baseDirectory)
165
- : (0, devkit_exports_1.joinPathFragments)(baseDirectory, options.pascalCaseDirectory
166
- ? (0, names_1.names)(derivedName).className
167
- : (0, names_1.names)(derivedName).fileName);
168
- if (options.directory &&
169
- !isDirectoryUnderProjectRoot(derivedDirectory, project.root)) {
170
- if (!options.nameAndDirectoryFormat) {
171
- devkit_exports_1.output.warn({
172
- title: `The provided directory "${options.directory}" is not under the provided project root "${project.root}".`,
173
- bodyLines: [
174
- `The generator will try to generate the ${options.artifactType} using the "as-provided" format.`,
175
- `With the "as-provided" format, the "project" option is ignored and the ${options.artifactType} will be generated at "${asProvidedOptions.filePath}" (<cwd>/<provided directory>).`,
176
- ],
177
- });
178
- return undefined;
179
- }
180
- throw new Error(`The provided directory "${options.directory}" is not under the provided project root "${project.root}". ` +
181
- `Please provide a directory that is under the provided project root or use the "as-provided" format and only provide the directory.`);
182
- }
183
- let derivedFileName = options.fileName;
184
- if (!derivedFileName) {
185
- derivedFileName = options.suffix
186
- ? `${derivedName}.${options.suffix}`
187
- : derivedName;
188
- derivedFileName = options.pascalCaseFile
189
- ? (0, names_1.names)(derivedFileName).className
190
- : (0, names_1.names)(derivedFileName).fileName;
191
- }
192
- const derivedFilePath = (0, devkit_exports_1.joinPathFragments)(derivedDirectory, `${derivedFileName}.${options.fileExtension}`);
193
- return {
194
- artifactName: derivedName,
195
- directory: derivedDirectory,
196
- fileName: derivedFileName,
197
- filePath: derivedFilePath,
198
- project: projectName,
199
- };
200
- }
201
63
  function validateResolvedProject(tree, project, options, normalizedDirectory) {
202
64
  if (project) {
203
65
  return;
@@ -218,15 +80,6 @@ function findProjectFromPath(tree, path) {
218
80
  const projectRootMappings = (0, devkit_internals_1.createProjectRootMappingsFromProjectConfigurations)(projectConfigurations);
219
81
  return (0, devkit_internals_1.findProjectForPath)(path, projectRootMappings);
220
82
  }
221
- function isDirectoryUnderProjectRoot(directory, projectRoot) {
222
- const normalizedDirectory = (0, devkit_exports_1.joinPathFragments)(devkit_exports_1.workspaceRoot, directory);
223
- const normalizedProjectRoot = (0, devkit_exports_1.joinPathFragments)(devkit_exports_1.workspaceRoot, projectRoot).replace(/\/$/, '');
224
- return (normalizedDirectory === normalizedProjectRoot ||
225
- normalizedDirectory.startsWith(`${normalizedProjectRoot}/`));
226
- }
227
- function isTTY() {
228
- return !!process.stdout.isTTY && process.env['CI'] !== 'true';
229
- }
230
83
  function getRelativeCwd() {
231
84
  return (0, devkit_exports_1.normalizePath)((0, path_1.relative)(devkit_exports_1.workspaceRoot, getCwd()));
232
85
  }
@@ -1,13 +1,12 @@
1
- import { ProjectType, Tree } from 'nx/src/devkit-exports';
2
- export type ProjectNameAndRootFormat = 'as-provided' | 'derived';
1
+ import { type ProjectType, type Tree } from 'nx/src/devkit-exports';
2
+ export type ProjectNameAndRootFormat = 'as-provided';
3
3
  export type ProjectGenerationOptions = {
4
4
  name: string;
5
5
  projectType: ProjectType;
6
- callingGenerator: string | null;
7
6
  directory?: string;
8
7
  importPath?: string;
9
- projectNameAndRootFormat?: ProjectNameAndRootFormat;
10
8
  rootProject?: boolean;
9
+ projectNameAndRootFormat?: ProjectNameAndRootFormat;
11
10
  };
12
11
  export type ProjectNameAndRootOptions = {
13
12
  /**
@@ -2,31 +2,17 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.determineProjectNameAndRootOptions = determineProjectNameAndRootOptions;
4
4
  exports.setCwd = setCwd;
5
- const enquirer_1 = require("enquirer");
6
- const path_1 = require("path");
7
- const get_workspace_layout_1 = require("../utils/get-workspace-layout");
8
- const names_1 = require("../utils/names");
9
5
  const devkit_exports_1 = require("nx/src/devkit-exports");
6
+ const path_1 = require("path");
10
7
  async function determineProjectNameAndRootOptions(tree, options) {
11
- if (!options.projectNameAndRootFormat &&
12
- (process.env.NX_INTERACTIVE !== 'true' || !isTTY())) {
13
- options.projectNameAndRootFormat = 'derived';
14
- }
15
- validateName(options.name, options.projectNameAndRootFormat);
16
- const formats = getProjectNameAndRootFormats(tree, options);
17
- const format = options.projectNameAndRootFormat ?? (await determineFormat(formats));
18
- if (format === 'derived' && options.callingGenerator) {
19
- logDeprecationMessage(options.callingGenerator, formats);
20
- }
8
+ validateName(options.name);
9
+ const projectNameAndRootOptions = getProjectNameAndRootOptions(tree, options);
21
10
  return {
22
- ...formats[format],
23
- projectNameAndRootFormat: format,
11
+ ...projectNameAndRootOptions,
12
+ projectNameAndRootFormat: 'as-provided',
24
13
  };
25
14
  }
26
- function validateName(name, projectNameAndRootFormat) {
27
- if (projectNameAndRootFormat === 'derived' && name.startsWith('@')) {
28
- throw new Error(`The project name "${name}" cannot start with "@" when the "projectNameAndRootFormat" is "derived".`);
29
- }
15
+ function validateName(name) {
30
16
  /**
31
17
  * Matches two types of project names:
32
18
  *
@@ -43,47 +29,7 @@ function validateName(name, projectNameAndRootFormat) {
43
29
  throw new Error(`The project name should match the pattern "${pattern}". The provided value "${name}" does not match.`);
44
30
  }
45
31
  }
46
- function logDeprecationMessage(callingGenerator, formats) {
47
- devkit_exports_1.logger.warn((0, devkit_exports_1.stripIndents) `
48
- In Nx 20, generating projects will no longer derive the name and root.
49
- Please provide the exact project name and root in the future.
50
- Example: nx g ${callingGenerator} ${formats['derived'].projectName} --directory ${formats['derived'].projectRoot}
51
- `);
52
- }
53
- async function determineFormat(formats) {
54
- if (!formats.derived) {
55
- return 'as-provided';
56
- }
57
- const asProvidedDescription = `As provided:
58
- Name: ${formats['as-provided'].projectName}
59
- Root: ${formats['as-provided'].projectRoot}`;
60
- const asProvidedSelectedValue = `${formats['as-provided'].projectName} @ ${formats['as-provided'].projectRoot}`;
61
- const derivedDescription = `Derived:
62
- Name: ${formats['derived'].projectName}
63
- Root: ${formats['derived'].projectRoot}`;
64
- const derivedSelectedValue = `${formats['derived'].projectName} @ ${formats['derived'].projectRoot}`;
65
- if (asProvidedSelectedValue === derivedSelectedValue) {
66
- return 'as-provided';
67
- }
68
- const result = await (0, enquirer_1.prompt)({
69
- type: 'select',
70
- name: 'format',
71
- message: 'What should be the project name and where should it be generated?',
72
- choices: [
73
- {
74
- message: asProvidedDescription,
75
- name: asProvidedSelectedValue,
76
- },
77
- {
78
- message: derivedDescription,
79
- name: derivedSelectedValue,
80
- },
81
- ],
82
- initial: 0,
83
- }).then(({ format }) => format === asProvidedSelectedValue ? 'as-provided' : 'derived');
84
- return result;
85
- }
86
- function getProjectNameAndRootFormats(tree, options) {
32
+ function getProjectNameAndRootOptions(tree, options) {
87
33
  const directory = options.directory
88
34
  ? (0, devkit_exports_1.normalizePath)(options.directory.replace(/^\.?\//, ''))
89
35
  : undefined;
@@ -97,38 +43,7 @@ function getProjectNameAndRootFormats(tree, options) {
97
43
  directory: directory ?? asProvidedParsedDirectory,
98
44
  name: asProvidedParsedName,
99
45
  });
100
- if (options.projectNameAndRootFormat === 'as-provided') {
101
- return {
102
- 'as-provided': asProvidedOptions,
103
- derived: undefined,
104
- };
105
- }
106
- if (asProvidedOptions.projectName.startsWith('@')) {
107
- if (!options.projectNameAndRootFormat) {
108
- devkit_exports_1.output.warn({
109
- title: `The provided name "${options.name}" contains a scoped project name and this is not supported by the "${options.callingGenerator}" when using the "derived" format.`,
110
- bodyLines: [
111
- `The generator will try to generate the project "${asProvidedOptions.projectName}" using the "as-provided" format at "${asProvidedOptions.projectRoot}".`,
112
- ],
113
- });
114
- return {
115
- 'as-provided': asProvidedOptions,
116
- derived: undefined,
117
- };
118
- }
119
- throw new Error(`The provided name "${options.name}" contains a scoped project name and this is not supported by the "${options.callingGenerator}" when using the "derived" format. ` +
120
- `Please provide a name without "@" or use the "as-provided" format.`);
121
- }
122
- const { name: derivedParsedName, directory: derivedParsedDirectory } = parseNameForDerived(options.name);
123
- const derivedOptions = getDerivedOptions(tree, {
124
- ...options,
125
- directory: directory ?? derivedParsedDirectory,
126
- name: derivedParsedName,
127
- });
128
- return {
129
- 'as-provided': asProvidedOptions,
130
- derived: derivedOptions,
131
- };
46
+ return asProvidedOptions;
132
47
  }
133
48
  function getAsProvidedOptions(tree, options) {
134
49
  let projectSimpleName;
@@ -191,54 +106,6 @@ function getAsProvidedOptions(tree, options) {
191
106
  projectRoot,
192
107
  };
193
108
  }
194
- function getDerivedOptions(tree, options) {
195
- const name = (0, names_1.names)(options.name).fileName;
196
- let { projectDirectory, layoutDirectory } = getDirectories(tree, options.directory, options.projectType);
197
- const projectDirectoryWithoutLayout = projectDirectory
198
- ? `${(0, names_1.names)(projectDirectory).fileName}/${name}`
199
- : options.rootProject
200
- ? '.'
201
- : name;
202
- // the project name uses the directory without the layout directory
203
- const projectName = projectDirectoryWithoutLayout === '.'
204
- ? name
205
- : projectDirectoryWithoutLayout.replace(/\//g, '-');
206
- const projectSimpleName = name;
207
- let projectRoot = projectDirectoryWithoutLayout;
208
- if (projectDirectoryWithoutLayout !== '.') {
209
- // prepend the layout directory
210
- projectRoot = (0, devkit_exports_1.joinPathFragments)(layoutDirectory, projectRoot);
211
- }
212
- let importPath;
213
- if (options.projectType === 'library') {
214
- importPath = options.importPath;
215
- if (!importPath) {
216
- const npmScope = getNpmScope(tree);
217
- importPath =
218
- projectRoot === '.'
219
- ? (0, devkit_exports_1.readJson)(tree, 'package.json').name ??
220
- getImportPath(npmScope, projectName)
221
- : getImportPath(npmScope, projectDirectoryWithoutLayout);
222
- }
223
- }
224
- return {
225
- projectName,
226
- names: {
227
- projectSimpleName,
228
- projectFileName: projectName,
229
- },
230
- importPath,
231
- projectRoot,
232
- };
233
- }
234
- function getDirectories(tree, directory, projectType) {
235
- let { projectDirectory, layoutDirectory } = (0, get_workspace_layout_1.extractLayoutDirectory)(directory);
236
- if (!layoutDirectory) {
237
- const { appsDir, libsDir } = (0, get_workspace_layout_1.getWorkspaceLayout)(tree);
238
- layoutDirectory = projectType === 'application' ? appsDir : libsDir;
239
- }
240
- return { projectDirectory, layoutDirectory };
241
- }
242
109
  function getImportPath(npmScope, name) {
243
110
  return npmScope ? `${npmScope === '@' ? '' : '@'}${npmScope}/${name}` : name;
244
111
  }
@@ -248,9 +115,6 @@ function getNpmScope(tree) {
248
115
  : { name: null };
249
116
  return name?.startsWith('@') ? name.split('/')[0].substring(1) : undefined;
250
117
  }
251
- function isTTY() {
252
- return !!process.stdout.isTTY && process.env['CI'] !== 'true';
253
- }
254
118
  /**
255
119
  * When running a script with the package manager (e.g. `npm run`), the package manager will
256
120
  * traverse the directory tree upwards until it finds a `package.json` and will set `process.cwd()`
@@ -288,9 +152,3 @@ function parseNameForAsProvided(rawName) {
288
152
  }
289
153
  return { name: rawName, directory: undefined };
290
154
  }
291
- function parseNameForDerived(rawName) {
292
- const parsedName = (0, devkit_exports_1.normalizePath)(rawName).split('/');
293
- const name = parsedName.pop();
294
- const directory = parsedName.length ? parsedName.join('/') : undefined;
295
- return { name, directory };
296
- }