Skip to content

Commit

Permalink
feat(node): use helper to determine project name and root directory i…
Browse files Browse the repository at this point in the history
…n library generator
  • Loading branch information
leosvelperez committed Aug 14, 2023
1 parent 2e9810c commit 6a7a294
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 42 deletions.
12 changes: 9 additions & 3 deletions docs/generated/packages/node/generators/library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "library",
"factory": "./src/generators/library/library",
"factory": "./src/generators/library/library#libraryGeneratorInternal",
"schema": {
"$schema": "http://json-schema.org/schema",
"cli": "nx",
Expand All @@ -19,13 +19,19 @@
"type": "string",
"description": "Library name",
"$default": { "$source": "argv", "index": 0 },
"x-prompt": "What name would you like to use for the library?"
"x-prompt": "What name would you like to use for the library?",
"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 lib 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"]
},
"simpleModuleName": {
"description": "Keep the module name simple (when using `--directory`).",
"type": "boolean",
Expand Down Expand Up @@ -131,7 +137,7 @@
"aliases": ["lib"],
"x-type": "library",
"description": "Create a node library.",
"implementation": "/packages/node/src/generators/library/library.ts",
"implementation": "/packages/node/src/generators/library/library#libraryGeneratorInternal.ts",
"hidden": false,
"path": "/packages/node/src/generators/library/schema.json",
"type": "generator"
Expand Down
2 changes: 1 addition & 1 deletion packages/node/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"description": "Create a node application."
},
"library": {
"factory": "./src/generators/library/library",
"factory": "./src/generators/library/library#libraryGeneratorInternal",
"schema": "./src/generators/library/schema.json",
"aliases": ["lib"],
"x-type": "library",
Expand Down
69 changes: 33 additions & 36 deletions packages/node/src/generators/library/library.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {
convertNxGenerator,
extractLayoutDirectory,
formatFiles,
generateFiles,
GeneratorCallback,
getWorkspaceLayout,
joinPathFragments,
names,
offsetFromRoot,
Expand All @@ -15,26 +13,31 @@ import {
updateProjectConfiguration,
updateTsConfigsToJs,
} from '@nx/devkit';
import { Schema } from './schema';
import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils';
import { libraryGenerator as jsLibraryGenerator } from '@nx/js';

import { join } from 'path';
import { addSwcDependencies } from '@nx/js/src/utils/swc/add-swc-dependencies';
import { addSwcConfig } from '@nx/js/src/utils/swc/add-swc-config';
import { addSwcDependencies } from '@nx/js/src/utils/swc/add-swc-dependencies';
import { join } from 'path';
import { initGenerator } from '../init/init';
import { getImportPath } from '@nx/js/src/utils/get-import-path';
import { Schema } from './schema';

export interface NormalizedSchema extends Schema {
name: string;
fileName: string;
projectRoot: string;
projectDirectory: string;
parsedTags: string[];
compiler: 'swc' | 'tsc';
}

export async function libraryGenerator(tree: Tree, schema: Schema) {
const options = normalizeOptions(tree, schema);
return await libraryGeneratorInternal(tree, {
projectNameAndRootFormat: 'derived',
...schema,
});
}

export async function libraryGeneratorInternal(tree: Tree, schema: Schema) {
const options = await normalizeOptions(tree, schema);
const tasks: GeneratorCallback[] = [
await initGenerator(tree, {
...options,
Expand Down Expand Up @@ -75,37 +78,39 @@ export async function libraryGenerator(tree: Tree, schema: Schema) {
export default libraryGenerator;
export const librarySchematic = convertNxGenerator(libraryGenerator);

function normalizeOptions(tree: Tree, options: Schema): NormalizedSchema {
const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
options.directory
);
const { npmScope, libsDir: defaultLibsDir } = getWorkspaceLayout(tree);
const libsDir = layoutDirectory ?? defaultLibsDir;
const name = names(options.name).fileName;
const fullProjectDirectory = projectDirectory
? `${names(projectDirectory).fileName}/${name}`
: name;

const projectName = fullProjectDirectory.replace(new RegExp('/', 'g'), '-');
async function normalizeOptions(
tree: Tree,
options: Schema
): Promise<NormalizedSchema> {
const {
projectName,
names: projectNames,
projectRoot,
importPath,
} = await determineProjectNameAndRootOptions(tree, {
name: options.name,
projectType: 'library',
directory: options.directory,
importPath: options.importPath,
projectNameAndRootFormat: options.projectNameAndRootFormat,
});

const fileName = getCaseAwareFileName({
fileName: options.simpleModuleName ? name : projectName,
fileName: options.simpleModuleName
? projectNames.projectSimpleName
: projectNames.projectFileName,
pascalCaseFiles: options.pascalCaseFiles,
});
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);

const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];

const importPath =
options.importPath || getImportPath(tree, fullProjectDirectory);

return {
...options,
fileName,
name: projectName,
projectRoot,
projectDirectory: fullProjectDirectory,
parsedTags,
importPath,
};
Expand Down Expand Up @@ -151,21 +156,13 @@ function updateProject(tree: Tree, options: NormalizedSchema) {
}

const project = readProjectConfiguration(tree, options.name);
const { libsDir } = getWorkspaceLayout(tree);

const rootProject = options.projectRoot === '.' || options.projectRoot === '';

project.targets = project.targets || {};
project.targets.build = {
executor: `@nx/js:${options.compiler}`,
outputs: ['{options.outputPath}'],
options: {
outputPath: joinPathFragments(
'dist',
rootProject
? options.projectDirectory
: `${libsDir}/${options.projectDirectory}`
),
outputPath: joinPathFragments('dist', options.projectRoot),
tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
packageJson: `${options.projectRoot}/package.json`,
main: `${options.projectRoot}/src/index` + (options.js ? '.js' : '.ts'),
Expand Down
4 changes: 3 additions & 1 deletion packages/node/src/generators/library/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Linter } from '@nx/linter';
import type { ProjectNameAndRootFormat } from '@nx/devkit/src/generators/project-name-directory-utils';
import type { Linter } from '@nx/linter';

export interface Schema {
name: string;
directory?: string;
projectNameAndRootFormat?: ProjectNameAndRootFormat;
simpleModuleName?: boolean;
skipTsConfig?: boolean;
skipFormat?: boolean;
Expand Down
8 changes: 7 additions & 1 deletion packages/node/src/generators/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
"$source": "argv",
"index": 0
},
"x-prompt": "What name would you like to use for the library?"
"x-prompt": "What name would you like to use for the library?",
"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 lib 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"]
},
"simpleModuleName": {
"description": "Keep the module name simple (when using `--directory`).",
"type": "boolean",
Expand Down

0 comments on commit 6a7a294

Please sign in to comment.