feat(misc): use helper to determine project name and root in npm-package generator (#18710)

This commit is contained in:
Leosvel Pérez Espinosa 2023-08-22 17:26:54 +01:00 committed by GitHub
parent d1da057811
commit 1b0439b55c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 17 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "npm-package", "name": "npm-package",
"factory": "./src/generators/npm-package/npm-package#npmPackageGenerator", "factory": "./src/generators/npm-package/npm-package#npmPackageGeneratorInternal",
"schema": { "schema": {
"$schema": "http://json-schema.org/schema", "$schema": "http://json-schema.org/schema",
"$id": "NxWorkspaceNpmPackage", "$id": "NxWorkspaceNpmPackage",
@ -14,7 +14,17 @@
"description": "Package name.", "description": "Package name.",
"$default": { "$source": "argv", "index": 0 }, "$default": { "$source": "argv", "index": 0 },
"x-prompt": "What name of your npm package?", "x-prompt": "What name of your npm package?",
"pattern": "^[a-zA-Z].*$" "pattern": "(?:^@[a-zA-Z0-9-*~][a-zA-Z0-9-*._~]*\\/[a-zA-Z0-9-~][a-zA-Z0-9-._~]*|^[a-zA-Z][^:]*)$"
},
"directory": {
"type": "string",
"description": "A directory where the package is placed.",
"alias": "dir"
},
"projectNameAndRootFormat": {
"description": "Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).",
"type": "string",
"enum": ["as-provided", "derived"]
} }
}, },
"required": ["name"], "required": ["name"],
@ -22,7 +32,7 @@
}, },
"description": "Create a minimal NPM package.", "description": "Create a minimal NPM package.",
"x-type": "library", "x-type": "library",
"implementation": "/packages/workspace/src/generators/npm-package/npm-package#npmPackageGenerator.ts", "implementation": "/packages/workspace/src/generators/npm-package/npm-package#npmPackageGeneratorInternal.ts",
"aliases": [], "aliases": [],
"hidden": false, "hidden": false,
"path": "/packages/workspace/src/generators/npm-package/schema.json", "path": "/packages/workspace/src/generators/npm-package/schema.json",

View File

@ -88,7 +88,7 @@
"description": "Fixes projects configuration" "description": "Fixes projects configuration"
}, },
"npm-package": { "npm-package": {
"factory": "./src/generators/npm-package/npm-package#npmPackageGenerator", "factory": "./src/generators/npm-package/npm-package#npmPackageGeneratorInternal",
"schema": "./src/generators/npm-package/schema.json", "schema": "./src/generators/npm-package/schema.json",
"description": "Create a minimal NPM package.", "description": "Create a minimal NPM package.",
"x-type": "library" "x-type": "library"

View File

@ -3,21 +3,45 @@ import {
convertNxGenerator, convertNxGenerator,
formatFiles, formatFiles,
generateFiles, generateFiles,
getWorkspaceLayout,
names,
Tree, Tree,
writeJson, writeJson,
} from '@nx/devkit'; } from '@nx/devkit';
import {
determineProjectNameAndRootOptions,
type ProjectNameAndRootFormat,
} from '@nx/devkit/src/generators/project-name-and-root-utils';
import { join } from 'path'; import { join } from 'path';
import { getImportPath } from '../../utilities/get-import-path'; import { getImportPath } from '../../utilities/get-import-path';
export interface ProjectOptions { export interface ProjectOptions {
name: string; name: string;
directory?: string;
projectNameAndRootFormat?: ProjectNameAndRootFormat;
}
interface NormalizedProjectOptions extends ProjectOptions {
projectRoot: string;
} }
function normalizeOptions(options: ProjectOptions): ProjectOptions { async function normalizeOptions(
options.name = names(options.name).fileName; tree: Tree,
return options; options: ProjectOptions
): Promise<NormalizedProjectOptions> {
const { projectName, projectRoot } = await determineProjectNameAndRootOptions(
tree,
{
name: options.name,
projectType: 'library',
directory: options.directory,
projectNameAndRootFormat: options.projectNameAndRootFormat,
callingGenerator: '@nx/workspace:npm-package',
}
);
return {
...options,
name: projectName,
projectRoot,
};
} }
function addFiles(projectRoot: string, tree: Tree, options: ProjectOptions) { function addFiles(projectRoot: string, tree: Tree, options: ProjectOptions) {
@ -34,21 +58,30 @@ function addFiles(projectRoot: string, tree: Tree, options: ProjectOptions) {
} }
export async function npmPackageGenerator(tree: Tree, options: ProjectOptions) { export async function npmPackageGenerator(tree: Tree, options: ProjectOptions) {
options = normalizeOptions(options); return await npmPackageGeneratorInternal(tree, {
projectNameAndRootFormat: 'derived',
...options,
});
}
const { libsDir } = getWorkspaceLayout(tree); export async function npmPackageGeneratorInternal(
const projectRoot = join(libsDir, options.name); tree: Tree,
_options: ProjectOptions
) {
const options = await normalizeOptions(tree, _options);
addProjectConfiguration(tree, options.name, { addProjectConfiguration(tree, options.name, {
root: projectRoot, root: options.projectRoot,
}); });
const fileCount = tree.children(projectRoot).length; const fileCount = tree.children(options.projectRoot).length;
const projectJsonExists = tree.exists(join(projectRoot, 'project.json')); const projectJsonExists = tree.exists(
join(options.projectRoot, 'project.json')
);
const isEmpty = fileCount === 0 || (fileCount === 1 && projectJsonExists); const isEmpty = fileCount === 0 || (fileCount === 1 && projectJsonExists);
if (isEmpty) { if (isEmpty) {
addFiles(projectRoot, tree, options); addFiles(options.projectRoot, tree, options);
} }
await formatFiles(tree); await formatFiles(tree);

View File

@ -14,7 +14,17 @@
"index": 0 "index": 0
}, },
"x-prompt": "What name of your npm package?", "x-prompt": "What name of your npm package?",
"pattern": "^[a-zA-Z].*$" "pattern": "(?:^@[a-zA-Z0-9-*~][a-zA-Z0-9-*._~]*\\/[a-zA-Z0-9-~][a-zA-Z0-9-._~]*|^[a-zA-Z][^:]*)$"
},
"directory": {
"type": "string",
"description": "A directory where the package is placed.",
"alias": "dir"
},
"projectNameAndRootFormat": {
"description": "Whether to generate the project name and root directory as provided (`as-provided`) or generate them composing their values and taking the configured layout into account (`derived`).",
"type": "string",
"enum": ["as-provided", "derived"]
} }
}, },
"required": ["name"] "required": ["name"]