Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions docs/generated/packages/workspace/generators/npm-package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "npm-package",
"factory": "./src/generators/npm-package/npm-package#npmPackageGenerator",
"factory": "./src/generators/npm-package/npm-package#npmPackageGeneratorInternal",
"schema": {
"$schema": "http://json-schema.org/schema",
"$id": "NxWorkspaceNpmPackage",
Expand All @@ -14,15 +14,25 @@
"description": "Package name.",
"$default": { "$source": "argv", "index": 0 },
"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"],
"presets": []
},
"description": "Create a minimal NPM package.",
"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": [],
"hidden": false,
"path": "/packages/workspace/src/generators/npm-package/schema.json",
Expand Down
2 changes: 1 addition & 1 deletion packages/workspace/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"description": "Fixes projects configuration"
},
"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",
"description": "Create a minimal NPM package.",
"x-type": "library"
Expand Down
57 changes: 45 additions & 12 deletions packages/workspace/src/generators/npm-package/npm-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,45 @@ import {
convertNxGenerator,
formatFiles,
generateFiles,
getWorkspaceLayout,
names,
Tree,
writeJson,
} from '@nx/devkit';
import {
determineProjectNameAndRootOptions,
type ProjectNameAndRootFormat,
} from '@nx/devkit/src/generators/project-name-and-root-utils';
import { join } from 'path';
import { getImportPath } from '../../utilities/get-import-path';

export interface ProjectOptions {
name: string;
directory?: string;
projectNameAndRootFormat?: ProjectNameAndRootFormat;
}
interface NormalizedProjectOptions extends ProjectOptions {
projectRoot: string;
}

function normalizeOptions(options: ProjectOptions): ProjectOptions {
options.name = names(options.name).fileName;
return options;
async function normalizeOptions(
tree: Tree,
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) {
Expand All @@ -34,21 +58,30 @@ function addFiles(projectRoot: string, 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);
const projectRoot = join(libsDir, options.name);
export async function npmPackageGeneratorInternal(
tree: Tree,
_options: ProjectOptions
) {
const options = await normalizeOptions(tree, _options);

addProjectConfiguration(tree, options.name, {
root: projectRoot,
root: options.projectRoot,
});

const fileCount = tree.children(projectRoot).length;
const projectJsonExists = tree.exists(join(projectRoot, 'project.json'));
const fileCount = tree.children(options.projectRoot).length;
const projectJsonExists = tree.exists(
join(options.projectRoot, 'project.json')
);
const isEmpty = fileCount === 0 || (fileCount === 1 && projectJsonExists);

if (isEmpty) {
addFiles(projectRoot, tree, options);
addFiles(options.projectRoot, tree, options);
}

await formatFiles(tree);
Expand Down
12 changes: 11 additions & 1 deletion packages/workspace/src/generators/npm-package/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@
"index": 0
},
"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"]
Expand Down