Skip to content

Commit

Permalink
feat(nx-nest): throw an error if generators are not executed in an ap…
Browse files Browse the repository at this point in the history
…plication
  • Loading branch information
dario-rodriguez committed Mar 14, 2024
1 parent b4823ac commit dbf3b77
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 11 deletions.
8 changes: 6 additions & 2 deletions packages/nx-nest/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export async function applicationGenerator(

const appTasks = await nestApplicationGenerator(tree, normalizedOptions);
const libTasks = await generateLoggerLibrary(tree, libraryOptions);
updatePackageJson(tree);
if (!options.skipPackageJson) {
updatePackageJson(tree);
}
updateTsconfigJson(tree);
updatePrettier(tree);
updateESLint(tree);
Expand All @@ -71,7 +73,9 @@ export async function applicationGenerator(
updateMain(tree, normalizedOptions.appProjectRoot, npmScope);
addDeclarationToModule(tree, normalizedOptions.appProjectRoot);

await formatFiles(tree);
if (!options.skipFormat) {
await formatFiles(tree);
}
return runTasksInSerial(
...[
appTasks,
Expand Down
5 changes: 4 additions & 1 deletion packages/nx-nest/src/generators/convict/convict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import { libraryGenerator } from '@nx/nest/src/generators/library/library';
import { NormalizedOptions } from '@nx/nest/src/generators/library/schema';
import * as path from 'path';
import { ASTFileBuilder } from '../../utils/ast-file-builder';
import { getNpmScope, updateJestConfig } from '../../utils/tree-utils';
import { ensureProjectIsAnApplication, getNpmScope, updateJestConfig } from '../../utils/tree-utils';
import { packagesVersion } from '../packagesVersion';
import { ConvictGeneratorSchema } from './schema';

export async function convictGenerator(tree: Tree, options: ConvictGeneratorSchema): Promise<GeneratorCallback> {
const appConfig = readProjectConfiguration(tree, options.projectName);

ensureProjectIsAnApplication(appConfig);

const npmScope = getNpmScope(tree);
const projectRoot = appConfig.root;
const libraryOptions = await normalizeLibraryOptions(tree, {
Expand Down
5 changes: 4 additions & 1 deletion packages/nx-nest/src/generators/mailer/mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import * as path from 'path';
import { ASTFileBuilder } from '../../utils/ast-file-builder';
import { ensureConfigFile } from '../../utils/config/config-defaults';
import { mergeDockerCompose } from '../../utils/merge';
import { existsConvictConfig, getNpmScope } from '../../utils/tree-utils';
import { ensureProjectIsAnApplication, existsConvictConfig, getNpmScope } from '../../utils/tree-utils';
import { packagesVersion } from '../packagesVersion';
import { defaultMailerValues, mailerConfigFile, mailerConfigType, mailerValuesFromConfig } from './configvalues';
import { MailerGeneratorSchema } from './schema';

export async function mailerGenerator(tree: Tree, options: MailerGeneratorSchema): Promise<() => void> {
const appConfig = readProjectConfiguration(tree, options.projectName);

ensureProjectIsAnApplication(appConfig);

addDependenciesToPackageJson(
tree,
{
Expand Down
7 changes: 6 additions & 1 deletion packages/nx-nest/src/generators/orm/orm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { formatFiles, installPackagesTask, readProjectConfiguration, Tree } from '@nx/devkit';
import { formatFiles, installPackagesTask, output, readProjectConfiguration, Tree } from '@nx/devkit';
import { mergeDockerCompose } from '../../utils/merge';
import { ensureProjectIsAnApplication } from '../../utils/tree-utils';
import { dbType, InitTypeormGeneratorSchema } from './schema';
import { generateTypeormConfiguration } from './typeorm';

Expand Down Expand Up @@ -75,8 +76,12 @@ export async function initOrmGenerator(tree: Tree, options: InitTypeormGenerator
const appConfig = readProjectConfiguration(tree, options.projectName);
const projectRoot = appConfig.root;

ensureProjectIsAnApplication(appConfig);

if (options.orm === 'typeorm') {
generateTypeormConfiguration(tree, options, projectRoot);
} else {
output.error({ title: `ORM ${options.orm} not implemented yet` });
}
mergeDockerCompose(tree, compose[options.db]);

Expand Down
18 changes: 14 additions & 4 deletions packages/nx-nest/src/generators/orm/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@
"properties": {
"orm": {
"type": "string",
"enum": ["typeorm"],
"default": "typeorm"
"enum": ["typeorm", "prisma", "drizzle"],
"default": "typeorm",
"$default": {
"$source": "argv",
"index": 1
},
"x-prompt": "Which ORM do you want to use?"
},
"db": {
"type": "string",
"enum": ["postgres", "cockroachdb", "mariadb", "mysql", "sqlite", "oracle", "mssql", "mongodb"],
"default": "sqlite"
"default": "sqlite",
"$default": {
"$source": "argv",
"index": 2
},
"x-prompt": "Which database engine do you want to use?"
},
"projectName": {
"type": "string",
Expand All @@ -21,7 +31,7 @@
"$source": "argv",
"index": 0
},
"x-prompt": "In which project would you like to add typeorm configuration?"
"x-prompt": "In which project would you like to initialize the ORM?"
}
},
"required": ["projectName"]
Expand Down
4 changes: 4 additions & 0 deletions packages/nx-nest/src/generators/security/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import {
Tree,
} from '@nx/devkit';
import { ASTFileBuilder } from '../../utils/ast-file-builder';
import { ensureProjectIsAnApplication } from '../../utils/tree-utils';
import { packagesVersion } from '../packagesVersion';
import { SecurityGeneratorSchema } from './schema';

export async function securityGenerator(tree: Tree, options: SecurityGeneratorSchema): Promise<GeneratorCallback> {
const appConfig = readProjectConfiguration(tree, options.projectName);

ensureProjectIsAnApplication(appConfig);

const mainPath = `${appConfig.sourceRoot ?? 'src'}/main.ts`;
const tasks = addDependenciesToPackageJson(
tree,
Expand Down
4 changes: 3 additions & 1 deletion packages/nx-nest/src/generators/swagger/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import * as path from 'path';
import { ASTFileBuilder } from '../../utils/ast-file-builder';
import { ensureConfigFile } from '../../utils/config/config-defaults';
import { existsConvictConfig, getNpmScope } from '../../utils/tree-utils';
import { ensureProjectIsAnApplication, existsConvictConfig, getNpmScope } from '../../utils/tree-utils';
import { packagesVersion } from '../packagesVersion';
import {
defaultSwaggerConfig,
Expand All @@ -21,6 +21,8 @@ import { SwaggerGeneratorSchema } from './schema';
export async function swaggerGenerator(tree: Tree, options: SwaggerGeneratorSchema): Promise<() => void> {
const appConfig = readProjectConfiguration(tree, options.projectName);

ensureProjectIsAnApplication(appConfig);

addDependenciesToPackageJson(
tree,
{ [packagesVersion['nestjsSwagger'].name]: packagesVersion['nestjsSwagger'].version },
Expand Down
8 changes: 7 additions & 1 deletion packages/nx-nest/src/utils/tree-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tree, readJson } from '@nx/devkit';
import { ProjectConfiguration, Tree, readJson } from '@nx/devkit';
import { join } from 'path';

export function existsConvictConfig(tree: Tree, projectRoot: string): boolean {
Expand Down Expand Up @@ -29,3 +29,9 @@ export function updateJestConfig(tree: Tree, projectRoot: string): void {
export function findModuleFile(tree: Tree, modulePath: string): string | undefined {
return tree.children(modulePath).find(m => m.endsWith('.module.ts'));
}

export function ensureProjectIsAnApplication(config: ProjectConfiguration): void {
if (config.projectType === 'library') {
throw new Error('This generator can be only used in an application.');
}
}

0 comments on commit dbf3b77

Please sign in to comment.