feat(express): use helper to determine project name and root in application generator (#18679)

This commit is contained in:
Leosvel Pérez Espinosa 2023-08-22 15:10:54 +01:00 committed by GitHub
parent 42d93b0d95
commit bb9f880a8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 28 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "application", "name": "application",
"factory": "./src/generators/application/application#applicationGenerator", "factory": "./src/generators/application/application#applicationGeneratorInternal",
"schema": { "schema": {
"$schema": "http://json-schema.org/schema", "$schema": "http://json-schema.org/schema",
"cli": "nx", "cli": "nx",
@ -14,12 +14,17 @@
"type": "string", "type": "string",
"$default": { "$source": "argv", "index": 0 }, "$default": { "$source": "argv", "index": 0 },
"x-prompt": "What name would you like to use for the node application?", "x-prompt": "What name would you like to use for the node application?",
"pattern": "^[a-zA-Z].*$" "pattern": "^[a-zA-Z][^:]*$"
}, },
"directory": { "directory": {
"description": "The directory of the new application.", "description": "The directory of the new application.",
"type": "string" "type": "string"
}, },
"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"]
},
"skipFormat": { "skipFormat": {
"description": "Skip formatting files.", "description": "Skip formatting files.",
"type": "boolean", "type": "boolean",
@ -90,7 +95,7 @@
"aliases": ["app"], "aliases": ["app"],
"x-type": "application", "x-type": "application",
"description": "Create an Express application.", "description": "Create an Express application.",
"implementation": "/packages/express/src/generators/application/application#applicationGenerator.ts", "implementation": "/packages/express/src/generators/application/application#applicationGeneratorInternal.ts",
"hidden": false, "hidden": false,
"path": "/packages/express/src/generators/application/schema.json", "path": "/packages/express/src/generators/application/schema.json",
"type": "generator" "type": "generator"

View File

@ -12,7 +12,7 @@
}, },
"application": { "application": {
"factory": "./src/generators/application/application#applicationGenerator", "factory": "./src/generators/application/application#applicationGeneratorInternal",
"schema": "./src/generators/application/schema.json", "schema": "./src/generators/application/schema.json",
"aliases": ["app"], "aliases": ["app"],
"x-type": "application", "x-type": "application",

View File

@ -1,20 +1,13 @@
import {
convertNxGenerator,
extractLayoutDirectory,
formatFiles,
getWorkspaceLayout,
joinPathFragments,
names,
toJS,
updateJson,
} from '@nx/devkit';
import type { Tree } from '@nx/devkit'; import type { Tree } from '@nx/devkit';
import { convertNxGenerator, formatFiles, toJS, updateJson } from '@nx/devkit';
import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils';
import { applicationGenerator as nodeApplicationGenerator } from '@nx/node'; import { applicationGenerator as nodeApplicationGenerator } from '@nx/node';
import { join } from 'path'; import { join } from 'path';
import { initGenerator } from '../init/init'; import { initGenerator } from '../init/init';
import type { Schema } from './schema'; import type { Schema } from './schema';
interface NormalizedSchema extends Schema { interface NormalizedSchema extends Schema {
appProjectName: string;
appProjectRoot: string; appProjectRoot: string;
} }
@ -45,7 +38,7 @@ const app = express();
app.use('/assets', express.static(path.join(__dirname, 'assets'))); app.use('/assets', express.static(path.join(__dirname, 'assets')));
app.get('/api', (req, res) => { app.get('/api', (req, res) => {
res.send({ message: 'Welcome to ${options.name}!' }); res.send({ message: 'Welcome to ${options.appProjectName}!' });
}); });
const port = process.env.PORT || 3333; const port = process.env.PORT || 3333;
@ -62,7 +55,14 @@ server.on('error', console.error);
} }
export async function applicationGenerator(tree: Tree, schema: Schema) { export async function applicationGenerator(tree: Tree, schema: Schema) {
const options = normalizeOptions(tree, schema); return await applicationGeneratorInternal(tree, {
projectNameAndRootFormat: 'derived',
...schema,
});
}
export async function applicationGeneratorInternal(tree: Tree, schema: Schema) {
const options = await normalizeOptions(tree, schema);
const initTask = await initGenerator(tree, { ...options, skipFormat: true }); const initTask = await initGenerator(tree, { ...options, skipFormat: true });
const applicationTask = await nodeApplicationGenerator(tree, { const applicationTask = await nodeApplicationGenerator(tree, {
...schema, ...schema,
@ -85,19 +85,26 @@ export async function applicationGenerator(tree: Tree, schema: Schema) {
export default applicationGenerator; export default applicationGenerator;
export const applicationSchematic = convertNxGenerator(applicationGenerator); export const applicationSchematic = convertNxGenerator(applicationGenerator);
function normalizeOptions(host: Tree, options: Schema): NormalizedSchema { async function normalizeOptions(
const { layoutDirectory, projectDirectory } = extractLayoutDirectory( host: Tree,
options.directory options: Schema
); ): Promise<NormalizedSchema> {
const appDirectory = projectDirectory const {
? `${names(projectDirectory).fileName}/${names(options.name).fileName}` projectName: appProjectName,
: names(options.name).fileName; projectRoot: appProjectRoot,
projectNameAndRootFormat,
const appsDir = layoutDirectory ?? getWorkspaceLayout(host).appsDir; } = await determineProjectNameAndRootOptions(host, {
const appProjectRoot = joinPathFragments(appsDir, appDirectory); name: options.name,
projectType: 'application',
directory: options.directory,
projectNameAndRootFormat: options.projectNameAndRootFormat,
callingGenerator: '@nx/express:application',
});
options.projectNameAndRootFormat = projectNameAndRootFormat;
return { return {
...options, ...options,
appProjectName,
appProjectRoot, appProjectRoot,
}; };
} }

View File

@ -1,11 +1,13 @@
import { UnitTestRunner } from '../../utils/test-runners'; import type { ProjectNameAndRootFormat } from '@nx/devkit/src/generators/project-name-and-root-utils';
import type { Linter } from '@nx/linter'; import type { Linter } from '@nx/linter';
import type { UnitTestRunner } from '../../utils/test-runners';
export interface Schema { export interface Schema {
name: string; name: string;
skipFormat: boolean; skipFormat: boolean;
skipPackageJson: boolean; skipPackageJson: boolean;
directory?: string; directory?: string;
projectNameAndRootFormat?: ProjectNameAndRootFormat;
unitTestRunner: UnitTestRunner; unitTestRunner: UnitTestRunner;
tags?: string; tags?: string;
linter: Linter; linter: Linter;

View File

@ -14,12 +14,17 @@
"index": 0 "index": 0
}, },
"x-prompt": "What name would you like to use for the node application?", "x-prompt": "What name would you like to use for the node application?",
"pattern": "^[a-zA-Z].*$" "pattern": "^[a-zA-Z][^:]*$"
}, },
"directory": { "directory": {
"description": "The directory of the new application.", "description": "The directory of the new application.",
"type": "string" "type": "string"
}, },
"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"]
},
"skipFormat": { "skipFormat": {
"description": "Skip formatting files.", "description": "Skip formatting files.",
"type": "boolean", "type": "boolean",