Skip to content

Commit

Permalink
feat(express): use helper to determine project name and root in appli…
Browse files Browse the repository at this point in the history
…cation generator (#18679)
  • Loading branch information
leosvelperez authored Aug 22, 2023
1 parent 42d93b0 commit bb9f880
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
11 changes: 8 additions & 3 deletions docs/generated/packages/express/generators/application.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "application",
"factory": "./src/generators/application/application#applicationGenerator",
"factory": "./src/generators/application/application#applicationGeneratorInternal",
"schema": {
"$schema": "http://json-schema.org/schema",
"cli": "nx",
Expand All @@ -14,12 +14,17 @@
"type": "string",
"$default": { "$source": "argv", "index": 0 },
"x-prompt": "What name would you like to use for the node application?",
"pattern": "^[a-zA-Z].*$"
"pattern": "^[a-zA-Z][^:]*$"
},
"directory": {
"description": "The directory of the new application.",
"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": {
"description": "Skip formatting files.",
"type": "boolean",
Expand Down Expand Up @@ -90,7 +95,7 @@
"aliases": ["app"],
"x-type": "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,
"path": "/packages/express/src/generators/application/schema.json",
"type": "generator"
Expand Down
2 changes: 1 addition & 1 deletion packages/express/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},

"application": {
"factory": "./src/generators/application/application#applicationGenerator",
"factory": "./src/generators/application/application#applicationGeneratorInternal",
"schema": "./src/generators/application/schema.json",
"aliases": ["app"],
"x-type": "application",
Expand Down
51 changes: 29 additions & 22 deletions packages/express/src/generators/application/application.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import {
convertNxGenerator,
extractLayoutDirectory,
formatFiles,
getWorkspaceLayout,
joinPathFragments,
names,
toJS,
updateJson,
} 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 { join } from 'path';
import { initGenerator } from '../init/init';
import type { Schema } from './schema';

interface NormalizedSchema extends Schema {
appProjectName: string;
appProjectRoot: string;
}

Expand Down Expand Up @@ -45,7 +38,7 @@ const app = express();
app.use('/assets', express.static(path.join(__dirname, 'assets')));
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;
Expand All @@ -62,7 +55,14 @@ server.on('error', console.error);
}

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 applicationTask = await nodeApplicationGenerator(tree, {
...schema,
Expand All @@ -85,19 +85,26 @@ export async function applicationGenerator(tree: Tree, schema: Schema) {
export default applicationGenerator;
export const applicationSchematic = convertNxGenerator(applicationGenerator);

function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
const { layoutDirectory, projectDirectory } = extractLayoutDirectory(
options.directory
);
const appDirectory = projectDirectory
? `${names(projectDirectory).fileName}/${names(options.name).fileName}`
: names(options.name).fileName;

const appsDir = layoutDirectory ?? getWorkspaceLayout(host).appsDir;
const appProjectRoot = joinPathFragments(appsDir, appDirectory);
async function normalizeOptions(
host: Tree,
options: Schema
): Promise<NormalizedSchema> {
const {
projectName: appProjectName,
projectRoot: appProjectRoot,
projectNameAndRootFormat,
} = await determineProjectNameAndRootOptions(host, {
name: options.name,
projectType: 'application',
directory: options.directory,
projectNameAndRootFormat: options.projectNameAndRootFormat,
callingGenerator: '@nx/express:application',
});
options.projectNameAndRootFormat = projectNameAndRootFormat;

return {
...options,
appProjectName,
appProjectRoot,
};
}
4 changes: 3 additions & 1 deletion packages/express/src/generators/application/schema.d.ts
Original file line number Diff line number Diff line change
@@ -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 { UnitTestRunner } from '../../utils/test-runners';

export interface Schema {
name: string;
skipFormat: boolean;
skipPackageJson: boolean;
directory?: string;
projectNameAndRootFormat?: ProjectNameAndRootFormat;
unitTestRunner: UnitTestRunner;
tags?: string;
linter: Linter;
Expand Down
7 changes: 6 additions & 1 deletion packages/express/src/generators/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
"index": 0
},
"x-prompt": "What name would you like to use for the node application?",
"pattern": "^[a-zA-Z].*$"
"pattern": "^[a-zA-Z][^:]*$"
},
"directory": {
"description": "The directory of the new application.",
"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": {
"description": "Skip formatting files.",
"type": "boolean",
Expand Down

1 comment on commit bb9f880

@vercel
Copy link

@vercel vercel bot commented on bb9f880 Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.