@nx/devkit 16.7.0-beta.3 → 16.7.0-beta.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/devkit",
3
- "version": "16.7.0-beta.3",
3
+ "version": "16.7.0-beta.5",
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.",
6
6
  "repository": {
@@ -19,7 +19,7 @@
19
19
  "Cypress",
20
20
  "CLI"
21
21
  ],
22
- "main": "./index",
22
+ "main": "./index.js",
23
23
  "typings": "./index.d.ts",
24
24
  "author": "Victor Savkin",
25
25
  "license": "MIT",
@@ -28,8 +28,9 @@
28
28
  },
29
29
  "homepage": "https://nx.dev",
30
30
  "dependencies": {
31
- "@nrwl/devkit": "16.7.0-beta.3",
31
+ "@nrwl/devkit": "16.7.0-beta.5",
32
32
  "ejs": "^3.1.7",
33
+ "enquirer": "~2.3.6",
33
34
  "ignore": "^5.0.4",
34
35
  "semver": "7.5.3",
35
36
  "tmp": "~0.2.1",
@@ -44,6 +45,6 @@
44
45
  "nx-migrations": {
45
46
  "migrations": "./migrations.json"
46
47
  },
47
- "types": "./index.d.ts",
48
- "gitHead": "118faf4e432667fd041954d96aa6c9c99fea5ebd"
48
+ "type": "commonjs",
49
+ "gitHead": "fa513731d4e6a960f112a5af6b0d725b2e624b36"
49
50
  }
@@ -0,0 +1,40 @@
1
+ import type { ProjectType } from 'nx/src/config/workspace-json-project-json';
2
+ import type { Tree } from 'nx/src/generators/tree';
3
+ export type ProjectNameAndRootFormat = 'as-provided' | 'derived';
4
+ export type ProjectGenerationOptions = {
5
+ name: string;
6
+ projectType: ProjectType;
7
+ directory?: string;
8
+ importPath?: string;
9
+ projectNameAndRootFormat?: ProjectNameAndRootFormat;
10
+ rootProject?: boolean;
11
+ };
12
+ export type ProjectNameAndRootOptions = {
13
+ /**
14
+ * Normalized full project name, including scope if name was provided with
15
+ * scope (e.g., `@scope/name`, only available when `projectNameAndRootFormat`
16
+ * is `as-provided`).
17
+ */
18
+ projectName: string;
19
+ /**
20
+ * Normalized project root, including the layout directory if configured.
21
+ */
22
+ projectRoot: string;
23
+ names: {
24
+ /**
25
+ * Normalized project name without scope. It's meant to be used when
26
+ * generating file names that contain the project name.
27
+ */
28
+ projectFileName: string;
29
+ /**
30
+ * Normalized project name without scope or directory. It's meant to be used
31
+ * when generating shorter file names that contain the project name.
32
+ */
33
+ projectSimpleName: string;
34
+ };
35
+ /**
36
+ * Normalized import path for the project.
37
+ */
38
+ importPath?: string;
39
+ };
40
+ export declare function determineProjectNameAndRootOptions(tree: Tree, options: ProjectGenerationOptions): Promise<ProjectNameAndRootOptions>;
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.determineProjectNameAndRootOptions = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const enquirer_1 = require("enquirer");
6
+ const nx_1 = require("../../nx");
7
+ const get_workspace_layout_1 = require("../utils/get-workspace-layout");
8
+ const names_1 = require("../utils/names");
9
+ const { joinPathFragments, readJson, readNxJson } = (0, nx_1.requireNx)();
10
+ function determineProjectNameAndRootOptions(tree, options) {
11
+ var _a;
12
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
13
+ validateName(options.name, options.projectNameAndRootFormat);
14
+ const formats = getProjectNameAndRootFormats(tree, options);
15
+ const format = (_a = options.projectNameAndRootFormat) !== null && _a !== void 0 ? _a : (yield determineFormat(formats));
16
+ return formats[format];
17
+ });
18
+ }
19
+ exports.determineProjectNameAndRootOptions = determineProjectNameAndRootOptions;
20
+ function validateName(name, projectNameAndRootFormat) {
21
+ if (projectNameAndRootFormat === 'derived' && name.startsWith('@')) {
22
+ throw new Error(`The project name "${name}" cannot start with "@" when the "projectNameAndRootFormat" is "derived".`);
23
+ }
24
+ /**
25
+ * Matches two types of project names:
26
+ *
27
+ * 1. Valid npm package names (e.g., '@scope/name' or 'name').
28
+ * 2. Names starting with a letter and can contain any character except whitespace and ':'.
29
+ *
30
+ * The second case is to support the legacy behavior (^[a-zA-Z].*$) with the difference
31
+ * that it doesn't allow the ":" character. It was wrong to allow it because it would
32
+ * conflict with the notation for tasks.
33
+ */
34
+ const pattern = '(?:^@[a-zA-Z0-9-*~][a-zA-Z0-9-*._~]*\\/[a-zA-Z0-9-~][a-zA-Z0-9-._~]*|^[a-zA-Z][^:]*)$';
35
+ const validationRegex = new RegExp(pattern);
36
+ if (!validationRegex.test(name)) {
37
+ throw new Error(`The project name should match the pattern "${pattern}". The provided value "${name}" does not match.`);
38
+ }
39
+ }
40
+ function determineFormat(formats) {
41
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
42
+ if (!formats.derived) {
43
+ return 'as-provided';
44
+ }
45
+ if (process.env.NX_INTERACTIVE !== 'true' || !isTTY()) {
46
+ return 'derived';
47
+ }
48
+ const asProvidedDescription = `As provided:
49
+ Name: ${formats['as-provided'].projectName}
50
+ Root: ${formats['as-provided'].projectRoot}`;
51
+ const asProvidedSelectedValue = `${formats['as-provided'].projectName} @ ${formats['as-provided'].projectRoot}`;
52
+ const derivedDescription = `Derived:
53
+ Name: ${formats['derived'].projectName}
54
+ Root: ${formats['derived'].projectRoot}`;
55
+ const derivedSelectedValue = `${formats['derived'].projectName} @ ${formats['derived'].projectRoot} (This was derived from the folder structure. Please provide the exact name and directory in the future)`;
56
+ return yield (0, enquirer_1.prompt)({
57
+ type: 'select',
58
+ name: 'format',
59
+ message: 'What should be the project name and where should it be generated?',
60
+ choices: [
61
+ {
62
+ message: asProvidedDescription,
63
+ name: asProvidedSelectedValue,
64
+ },
65
+ {
66
+ message: derivedDescription,
67
+ name: derivedSelectedValue,
68
+ },
69
+ ],
70
+ initial: 'as-provided',
71
+ }).then(({ format }) => format === asProvidedSelectedValue ? 'as-provided' : 'derived');
72
+ });
73
+ }
74
+ function getProjectNameAndRootFormats(tree, options) {
75
+ var _a, _b, _c, _d;
76
+ const name = (0, names_1.names)(options.name).fileName;
77
+ const directory = (_a = options.directory) === null || _a === void 0 ? void 0 : _a.replace(/^\.?\//, '');
78
+ const asProvidedProjectName = name;
79
+ const asProvidedProjectDirectory = directory
80
+ ? (0, names_1.names)(directory).fileName
81
+ : options.rootProject
82
+ ? '.'
83
+ : asProvidedProjectName;
84
+ if (name.startsWith('@')) {
85
+ const nameWithoutScope = asProvidedProjectName.split('/')[1];
86
+ return {
87
+ 'as-provided': {
88
+ projectName: asProvidedProjectName,
89
+ names: {
90
+ projectSimpleName: nameWithoutScope,
91
+ projectFileName: nameWithoutScope,
92
+ },
93
+ importPath: (_b = options.importPath) !== null && _b !== void 0 ? _b : asProvidedProjectName,
94
+ projectRoot: asProvidedProjectDirectory,
95
+ },
96
+ };
97
+ }
98
+ let asProvidedImportPath;
99
+ let npmScope;
100
+ if (options.projectType === 'library') {
101
+ asProvidedImportPath = options.importPath;
102
+ if (!asProvidedImportPath) {
103
+ npmScope = getNpmScope(tree);
104
+ asProvidedImportPath =
105
+ asProvidedProjectDirectory === '.'
106
+ ? (_c = readJson(tree, 'package.json').name) !== null && _c !== void 0 ? _c : getImportPath(npmScope, asProvidedProjectName)
107
+ : getImportPath(npmScope, asProvidedProjectName);
108
+ }
109
+ }
110
+ let { projectDirectory, layoutDirectory } = getDirectories(tree, directory, options.projectType);
111
+ const derivedProjectDirectoryWithoutLayout = projectDirectory
112
+ ? `${(0, names_1.names)(projectDirectory).fileName}/${name}`
113
+ : options.rootProject
114
+ ? '.'
115
+ : name;
116
+ // the project name uses the directory without the layout directory
117
+ const derivedProjectName = derivedProjectDirectoryWithoutLayout === '.'
118
+ ? name
119
+ : derivedProjectDirectoryWithoutLayout.replace(/\//g, '-');
120
+ const derivedSimpleProjectName = name;
121
+ let derivedProjectDirectory = derivedProjectDirectoryWithoutLayout;
122
+ if (derivedProjectDirectoryWithoutLayout !== '.') {
123
+ // prepend the layout directory
124
+ derivedProjectDirectory = joinPathFragments(layoutDirectory, derivedProjectDirectory);
125
+ }
126
+ let derivedImportPath;
127
+ if (options.projectType === 'library') {
128
+ derivedImportPath = options.importPath;
129
+ if (!derivedImportPath) {
130
+ derivedImportPath =
131
+ derivedProjectDirectory === '.'
132
+ ? (_d = readJson(tree, 'package.json').name) !== null && _d !== void 0 ? _d : getImportPath(npmScope, derivedProjectName)
133
+ : getImportPath(npmScope, derivedProjectDirectoryWithoutLayout);
134
+ }
135
+ }
136
+ return {
137
+ 'as-provided': {
138
+ projectName: asProvidedProjectName,
139
+ names: {
140
+ projectSimpleName: asProvidedProjectName,
141
+ projectFileName: asProvidedProjectName,
142
+ },
143
+ importPath: asProvidedImportPath,
144
+ projectRoot: asProvidedProjectDirectory,
145
+ },
146
+ derived: {
147
+ projectName: derivedProjectName,
148
+ names: {
149
+ projectSimpleName: derivedSimpleProjectName,
150
+ projectFileName: derivedProjectName,
151
+ },
152
+ importPath: derivedImportPath,
153
+ projectRoot: derivedProjectDirectory,
154
+ },
155
+ };
156
+ }
157
+ function getDirectories(tree, directory, projectType) {
158
+ let { projectDirectory, layoutDirectory } = (0, get_workspace_layout_1.extractLayoutDirectory)(directory);
159
+ if (!layoutDirectory) {
160
+ const { appsDir, libsDir } = (0, get_workspace_layout_1.getWorkspaceLayout)(tree);
161
+ layoutDirectory = projectType === 'application' ? appsDir : libsDir;
162
+ }
163
+ return { projectDirectory, layoutDirectory };
164
+ }
165
+ function getImportPath(npmScope, name) {
166
+ return npmScope ? `${npmScope === '@' ? '' : '@'}${npmScope}/${name}` : name;
167
+ }
168
+ function getNpmScope(tree) {
169
+ const nxJson = readNxJson(tree);
170
+ // TODO(v17): Remove reading this from nx.json
171
+ if (nxJson === null || nxJson === void 0 ? void 0 : nxJson.npmScope) {
172
+ return nxJson.npmScope;
173
+ }
174
+ const { name } = tree.exists('package.json')
175
+ ? readJson(tree, 'package.json')
176
+ : { name: null };
177
+ return (name === null || name === void 0 ? void 0 : name.startsWith('@')) ? name.split('/')[0].substring(1) : undefined;
178
+ }
179
+ function isTTY() {
180
+ return !!process.stdout.isTTY && process.env['CI'] !== 'true';
181
+ }