@nx/devkit 20.2.1 → 20.3.0-beta.0
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": "20.
|
3
|
+
"version": "20.3.0-beta.0",
|
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": {
|
@@ -2,10 +2,19 @@ import { type Tree } from 'nx/src/devkit-exports';
|
|
2
2
|
export type ArtifactGenerationOptions = {
|
3
3
|
path: string;
|
4
4
|
name?: string;
|
5
|
-
fileExtension?:
|
6
|
-
fileName?: string;
|
5
|
+
fileExtension?: string;
|
7
6
|
suffix?: string;
|
7
|
+
allowedFileExtensions?: string[];
|
8
|
+
/**
|
9
|
+
* @deprecated Provide the full file path including the file extension in the `path` option. This option will be removed in Nx v21.
|
10
|
+
*/
|
11
|
+
js?: boolean;
|
12
|
+
/**
|
13
|
+
* @deprecated Provide the full file path including the file extension in the `path` option. This option will be removed in Nx v21.
|
14
|
+
*/
|
15
|
+
jsOptionName?: string;
|
8
16
|
};
|
17
|
+
export type FileExtensionType = 'js' | 'ts' | 'other';
|
9
18
|
export type NameAndDirectoryOptions = {
|
10
19
|
/**
|
11
20
|
* Normalized artifact name.
|
@@ -19,6 +28,14 @@ export type NameAndDirectoryOptions = {
|
|
19
28
|
* Normalized file name of the artifact without the extension.
|
20
29
|
*/
|
21
30
|
fileName: string;
|
31
|
+
/**
|
32
|
+
* Normalized file extension.
|
33
|
+
*/
|
34
|
+
fileExtension: string;
|
35
|
+
/**
|
36
|
+
* Normalized file extension type.
|
37
|
+
*/
|
38
|
+
fileExtensionType: FileExtensionType;
|
22
39
|
/**
|
23
40
|
* Normalized full file path of the artifact.
|
24
41
|
*/
|
@@ -6,6 +6,13 @@ exports.setCwd = setCwd;
|
|
6
6
|
const devkit_exports_1 = require("nx/src/devkit-exports");
|
7
7
|
const devkit_internals_1 = require("nx/src/devkit-internals");
|
8
8
|
const path_1 = require("path");
|
9
|
+
const DEFAULT_ALLOWED_JS_FILE_EXTENSIONS = ['js', 'cjs', 'mjs', 'jsx'];
|
10
|
+
const DEFAULT_ALLOWED_TS_FILE_EXTENSIONS = ['ts', 'cts', 'mts', 'tsx'];
|
11
|
+
const DEFAULT_ALLOWED_FILE_EXTENSIONS = [
|
12
|
+
...DEFAULT_ALLOWED_JS_FILE_EXTENSIONS,
|
13
|
+
...DEFAULT_ALLOWED_TS_FILE_EXTENSIONS,
|
14
|
+
'vue',
|
15
|
+
];
|
9
16
|
async function determineArtifactNameAndDirectoryOptions(tree, options) {
|
10
17
|
const normalizedOptions = getNameAndDirectoryOptions(tree, options);
|
11
18
|
validateResolvedProject(normalizedOptions.project, normalizedOptions.directory);
|
@@ -15,7 +22,6 @@ function getNameAndDirectoryOptions(tree, options) {
|
|
15
22
|
const path = options.path
|
16
23
|
? (0, devkit_exports_1.normalizePath)(options.path.replace(/^\.?\//, ''))
|
17
24
|
: undefined;
|
18
|
-
const fileExtension = options.fileExtension ?? 'ts';
|
19
25
|
let { name: extractedName, directory } = extractNameAndDirectoryFromPath(path);
|
20
26
|
const relativeCwd = getRelativeCwd();
|
21
27
|
// append the directory to the current working directory if it doesn't start with it
|
@@ -23,15 +29,30 @@ function getNameAndDirectoryOptions(tree, options) {
|
|
23
29
|
directory = (0, devkit_exports_1.joinPathFragments)(relativeCwd, directory);
|
24
30
|
}
|
25
31
|
const project = findProjectFromPath(tree, directory);
|
26
|
-
|
27
|
-
|
28
|
-
const
|
32
|
+
let fileName = extractedName;
|
33
|
+
let fileExtension = options.fileExtension ?? 'ts';
|
34
|
+
const allowedFileExtensions = options.allowedFileExtensions ?? DEFAULT_ALLOWED_FILE_EXTENSIONS;
|
35
|
+
const fileExtensionRegex = new RegExp(`\\.(${allowedFileExtensions.join('|')})$`);
|
36
|
+
const fileExtensionMatch = fileName.match(fileExtensionRegex);
|
37
|
+
if (fileExtensionMatch) {
|
38
|
+
fileExtension = fileExtensionMatch[1];
|
39
|
+
fileName = fileName.replace(fileExtensionRegex, '');
|
40
|
+
extractedName = fileName;
|
41
|
+
}
|
42
|
+
else if (options.suffix) {
|
43
|
+
fileName = `${fileName}.${options.suffix}`;
|
44
|
+
}
|
45
|
+
const filePath = (0, devkit_exports_1.joinPathFragments)(directory, `${fileName}.${fileExtension}`);
|
46
|
+
const fileExtensionType = getFileExtensionType(fileExtension);
|
47
|
+
validateFileExtension(fileExtension, allowedFileExtensions, options.js, options.jsOptionName);
|
29
48
|
return {
|
30
49
|
artifactName: options.name ?? extractedName,
|
31
|
-
directory
|
32
|
-
fileName
|
33
|
-
|
34
|
-
|
50
|
+
directory,
|
51
|
+
fileName,
|
52
|
+
fileExtension,
|
53
|
+
fileExtensionType,
|
54
|
+
filePath,
|
55
|
+
project,
|
35
56
|
};
|
36
57
|
}
|
37
58
|
function validateResolvedProject(project, normalizedDirectory) {
|
@@ -72,3 +93,31 @@ function extractNameAndDirectoryFromPath(path) {
|
|
72
93
|
const directory = parsedPath.join('/');
|
73
94
|
return { name, directory };
|
74
95
|
}
|
96
|
+
function getFileExtensionType(fileExtension) {
|
97
|
+
if (DEFAULT_ALLOWED_JS_FILE_EXTENSIONS.includes(fileExtension)) {
|
98
|
+
return 'js';
|
99
|
+
}
|
100
|
+
if (DEFAULT_ALLOWED_TS_FILE_EXTENSIONS.includes(fileExtension)) {
|
101
|
+
return 'ts';
|
102
|
+
}
|
103
|
+
return 'other';
|
104
|
+
}
|
105
|
+
function validateFileExtension(fileExtension, allowedFileExtensions, js, jsOptionName) {
|
106
|
+
const fileExtensionType = getFileExtensionType(fileExtension);
|
107
|
+
if (!allowedFileExtensions.includes(fileExtension)) {
|
108
|
+
throw new Error(`The provided file path has an extension (.${fileExtension}) that is not supported by this generator.
|
109
|
+
The supported extensions are: ${allowedFileExtensions
|
110
|
+
.map((ext) => `.${ext}`)
|
111
|
+
.join(', ')}.`);
|
112
|
+
}
|
113
|
+
if (js !== undefined) {
|
114
|
+
jsOptionName = jsOptionName ?? 'js';
|
115
|
+
if (js && fileExtensionType === 'ts') {
|
116
|
+
throw new Error(`The provided file path has an extension (.${fileExtension}) that conflicts with the provided "--${jsOptionName}" option.`);
|
117
|
+
}
|
118
|
+
if (!js && fileExtensionType === 'js') {
|
119
|
+
throw new Error(`The provided file path has an extension (.${fileExtension}) that conflicts with the provided "--${jsOptionName}" option.`);
|
120
|
+
}
|
121
|
+
}
|
122
|
+
return fileExtensionType;
|
123
|
+
}
|