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: initial support for case type selection #1501

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
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
2,051 changes: 1,122 additions & 929 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,28 @@
"dependencies": {
"@angular-devkit/core": "16.2.8",
"@angular-devkit/schematics": "16.2.8",
"case-anything": "2.1.13",
"comment-json": "4.2.3",
"jsonc-parser": "3.2.0",
"pluralize": "8.0.0"
},
"devDependencies": {
"@commitlint/cli": "18.4.1",
"@commitlint/config-angular": "18.4.0",
"@types/jest": "29.5.8",
"@types/node": "20.9.0",
"@typescript-eslint/eslint-plugin": "6.11.0",
"@typescript-eslint/parser": "6.11.0",
"@commitlint/cli": "17.8.0",
"@commitlint/config-angular": "17.8.0",
"@types/jest": "29.5.5",
"@types/node": "18.18.5",
"@typescript-eslint/eslint-plugin": "6.8.0",
"@typescript-eslint/parser": "6.8.0",
"cpx": "1.5.0",
"eslint": "8.53.0",
"eslint": "8.51.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-import": "2.29.0",
"eslint-plugin-import": "2.28.1",
"gulp": "4.0.2",
"gulp-clean": "0.4.0",
"husky": "8.0.3",
"jest": "29.7.0",
"nyc": "15.1.0",
"release-it": "17.0.0",
"release-it": "16.2.1",
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"typescript": "5.2.2"
Expand Down
10 changes: 7 additions & 3 deletions src/lib/application/application.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
url,
} from '@angular-devkit/schematics';
import { basename, parse } from 'path';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import { normalizeToCase } from '../../utils/formatting';
import {
DEFAULT_AUTHOR,
DEFAULT_DESCRIPTION,
Expand All @@ -21,7 +21,7 @@ import {
import { ApplicationOptions } from './application.schema';

export function main(options: ApplicationOptions): Rule {
options.name = normalizeToKebabOrSnakeCase(options.name.toString());
options.name = normalizeToCase(options.name.toString(), 'kebab-or-snake');

const path =
!options.directory || options.directory === 'undefined'
Expand All @@ -42,10 +42,14 @@ function transform(options: ApplicationOptions): ApplicationOptions {
target.language = !!target.language ? target.language : DEFAULT_LANGUAGE;
target.name = resolvePackageName(target.name.toString());
target.version = !!target.version ? target.version : DEFAULT_VERSION;
target.specFileSuffix = normalizeToKebabOrSnakeCase(
target.specFileSuffix = normalizeToCase(
options.specFileSuffix || 'spec',
'kebab-or-snake'
);

target.caseNaming = !!target.caseNaming ? target.caseNaming : 'snake';
Copy link

Choose a reason for hiding this comment

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

Hi, maybe i missed something, but this shouldn't be pascal by default ?

Copy link
Author

@espoal espoal Jan 25, 2024

Choose a reason for hiding this comment

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

In the original implementation a case named kebab-or-snake is used, which is the snake you see referred here. I kept the original default to avoid unintended side effects, for example other piece of the nestjs codebase default to and expect snake as input, or they will complain



target.packageManager =
!target.packageManager || target.packageManager === 'undefined'
? 'npm'
Expand Down
4 changes: 4 additions & 0 deletions src/lib/application/application.schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export interface ApplicationOptions {
* Nest included development dependencies (comma separated values).
*/
devDependencies?: string;
/**
* Case format. Options are 'kebab' | 'snake' | 'camel' | 'pascal'.
espoal marked this conversation as resolved.
Show resolved Hide resolved
*/
caseNaming?: string;
/**
* Specifies if a spec file is generated.
*/
Expand Down
5 changes: 4 additions & 1 deletion src/lib/application/files/js/nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"$schema": "https://json.schemastore.org/nest-cli",
"language": "js",
"collection": "@nestjs/schematics",
"sourceRoot": "src"
"sourceRoot": "src", <% if (caseNaming !== 'undefined') { %>
"generateOptions": {
"caseNaming": "<%= caseNaming %>"
} <% } %>
}
5 changes: 4 additions & 1 deletion src/lib/application/files/ts/nest-cli.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"sourceRoot": "src", <% if (caseNaming !== 'undefined') { %>
"generateOptions": {
"caseNaming": "<%= caseNaming %>"
}, <% } %>
"compilerOptions": {
"deleteOutDir": true
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
"type": "string",
"description": "Nest application development dependencies."
},
"caseNaming": {
"type": "string",
"description": "Case format. Options are 'kebab' | 'snake' | 'camel' | 'pascal' | 'kebab-or-snake'"
},
"spec": {
"type": "boolean",
"default": true,
Expand Down
9 changes: 5 additions & 4 deletions src/lib/class/class.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
template,
url,
} from '@angular-devkit/schematics';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import { normalizeToCase } from '../../utils/formatting';
import { Location, NameParser } from '../../utils/name.parser';
import { mergeSourceRoot } from '../../utils/source-root.helpers';
import { DEFAULT_LANGUAGE } from '../defaults';
Expand All @@ -31,9 +31,10 @@ function transform(options: ClassOptions): ClassOptions {
}
const location: Location = new NameParser().parse(target);

target.name = normalizeToKebabOrSnakeCase(location.name);
target.specFileSuffix = normalizeToKebabOrSnakeCase(
target.name = normalizeToCase(location.name, 'kebab-or-snake');
target.specFileSuffix = normalizeToCase(
options.specFileSuffix || 'spec',
'kebab-or-snake'
);
if (target.name.includes('.')) {
target.className = strings.classify(target.name).replace('.', '');
Expand All @@ -44,7 +45,7 @@ function transform(options: ClassOptions): ClassOptions {
target.language =
target.language !== undefined ? target.language : DEFAULT_LANGUAGE;

target.path = normalizeToKebabOrSnakeCase(location.path);
target.path = normalizeToCase(location.path, 'kebab-or-snake');
target.path = target.flat
? target.path
: join(target.path as Path, target.name);
Expand Down
17 changes: 10 additions & 7 deletions src/lib/controller/controller.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
Tree,
url,
} from '@angular-devkit/schematics';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import { normalizeToCase, CaseType } from '../../utils/formatting';
import {
DeclarationOptions,
ModuleDeclarator,
Expand Down Expand Up @@ -44,15 +44,18 @@ function transform(source: ControllerOptions): ControllerOptions {
const target: ControllerOptions = Object.assign({}, source);
target.metadata = ELEMENT_METADATA;
target.type = ELEMENT_TYPE;

const location: Location = new NameParser().parse(target);
target.name = normalizeToKebabOrSnakeCase(location.name);
target.path = normalizeToKebabOrSnakeCase(location.path);
const caseType: CaseType = source.caseNaming || 'kebab-or-snake';
target.caseNaming = caseType;

target.name = normalizeToCase(location.name, caseType);
target.path = normalizeToCase(location.path, caseType);
target.language =
target.language !== undefined ? target.language : DEFAULT_LANGUAGE;

target.specFileSuffix = normalizeToKebabOrSnakeCase(
target.specFileSuffix = normalizeToCase(
source.specFileSuffix || 'spec',
caseType
);

target.path = target.flat
Expand All @@ -64,8 +67,8 @@ function transform(source: ControllerOptions): ControllerOptions {
function generate(options: ControllerOptions) {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec
? noop()
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
const suffix = `.__specFileSuffix__.${languageExtension}`;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/controller/controller.schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Path } from '@angular-devkit/core';
import { CaseType } from '../../utils/formatting';

export interface ControllerOptions {
/**
Expand Down Expand Up @@ -46,4 +47,8 @@ export interface ControllerOptions {
* Flag to indicate if a directory is created.
*/
flat?: boolean;
/**
* Case format. Options are 'kebab' | 'snake' | 'camel' | 'pascal' | 'kebab-or-snake'.
*/
caseNaming?: CaseType;
}
2 changes: 1 addition & 1 deletion src/lib/controller/files/js/__name__.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller } from '@nestjs/common';

@Controller('<%= dasherize(name) %>')
@Controller('<%= name %>')
export class <%= classify(name) %>Controller {}
2 changes: 1 addition & 1 deletion src/lib/controller/files/ts/__name__.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller } from '@nestjs/common';

@Controller('<%= dasherize(name) %>')
@Controller('<%= name %>')
export class <%= classify(name) %>Controller {}
6 changes: 3 additions & 3 deletions src/lib/decorator/decorator.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
template,
url,
} from '@angular-devkit/schematics';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import { normalizeToCase } from '../../utils/formatting';
import { Location, NameParser } from '../../utils/name.parser';
import { mergeSourceRoot } from '../../utils/source-root.helpers';
import { DecoratorOptions } from './decorator.schema';
Expand All @@ -27,8 +27,8 @@ function transform(options: DecoratorOptions): DecoratorOptions {
throw new SchematicsException('Option (name) is required.');
}
const location: Location = new NameParser().parse(target);
target.name = normalizeToKebabOrSnakeCase(location.name);
target.path = normalizeToKebabOrSnakeCase(location.path);
target.name = normalizeToCase(location.name, 'kebab-or-snake');
target.path = normalizeToCase(location.path, 'kebab-or-snake');
target.language = target.language !== undefined ? target.language : 'ts';

target.path = target.flat
Expand Down
13 changes: 7 additions & 6 deletions src/lib/filter/filter.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
template,
url,
} from '@angular-devkit/schematics';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import { normalizeToCase } from '../../utils/formatting';
import { Location, NameParser } from '../../utils/name.parser';
import { mergeSourceRoot } from '../../utils/source-root.helpers';
import { FilterOptions } from './filter.schema';
Expand All @@ -29,11 +29,12 @@ function transform(options: FilterOptions): FilterOptions {
throw new SchematicsException('Option (name) is required.');
}
const location: Location = new NameParser().parse(target);
target.name = normalizeToKebabOrSnakeCase(location.name);
target.path = normalizeToKebabOrSnakeCase(location.path);
target.name = normalizeToCase(location.name, 'kebab-or-snake');
target.path = normalizeToCase(location.path, 'kebab-or-snake');
target.language = target.language !== undefined ? target.language : 'ts';
target.specFileSuffix = normalizeToKebabOrSnakeCase(
target.specFileSuffix = normalizeToCase(
options.specFileSuffix || 'spec',
'kebab-or-snake'
);

target.path = target.flat
Expand All @@ -45,8 +46,8 @@ function transform(options: FilterOptions): FilterOptions {
function generate(options: FilterOptions): Source {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec
? noop()
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
const suffix = `.__specFileSuffix__.${languageExtension}`;
Expand Down
13 changes: 7 additions & 6 deletions src/lib/gateway/gateway.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Tree,
url,
} from '@angular-devkit/schematics';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import { normalizeToCase } from '../../utils/formatting';
import {
DeclarationOptions,
ModuleDeclarator,
Expand Down Expand Up @@ -46,13 +46,14 @@ function transform(options: GatewayOptions): GatewayOptions {
}
target.metadata = 'providers';
target.type = 'gateway';
target.specFileSuffix = normalizeToKebabOrSnakeCase(
target.specFileSuffix = normalizeToCase(
options.specFileSuffix || 'spec',
'kebab-or-snake'
);

const location: Location = new NameParser().parse(target);
target.name = normalizeToKebabOrSnakeCase(location.name);
target.path = normalizeToKebabOrSnakeCase(location.path);
target.name = normalizeToCase(location.name, 'kebab-or-snake');
target.path = normalizeToCase(location.path, 'kebab-or-snake');
target.language = target.language !== undefined ? target.language : 'ts';

target.path = target.flat
Expand All @@ -65,8 +66,8 @@ function generate(options: GatewayOptions): Source {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec ? noop() : filter((path) => !path.endsWith('.spec.ts')),
options.spec
? noop()
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
const suffix = `.__specFileSuffix__.${languageExtension}`;
Expand Down
13 changes: 7 additions & 6 deletions src/lib/guard/guard.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
template,
url,
} from '@angular-devkit/schematics';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import { normalizeToCase } from '../../utils/formatting';
import { Location, NameParser } from '../../utils/name.parser';
import { mergeSourceRoot } from '../../utils/source-root.helpers';
import { GuardOptions } from './guard.schema';
Expand All @@ -29,11 +29,12 @@ function transform(options: GuardOptions): GuardOptions {
throw new SchematicsException('Option (name) is required.');
}
const location: Location = new NameParser().parse(target);
target.name = normalizeToKebabOrSnakeCase(location.name);
target.path = normalizeToKebabOrSnakeCase(location.path);
target.name = normalizeToCase(location.name, 'kebab-or-snake');
target.path = normalizeToCase(location.path, 'kebab-or-snake');
target.language = target.language !== undefined ? target.language : 'ts';
target.specFileSuffix = normalizeToKebabOrSnakeCase(
target.specFileSuffix = normalizeToCase(
options.specFileSuffix || 'spec',
'kebab-or-snake'
);

target.path = target.flat
Expand All @@ -45,8 +46,8 @@ function transform(options: GuardOptions): GuardOptions {
function generate(options: GuardOptions): Source {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec
? noop()
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
const suffix = `.__specFileSuffix__.${languageExtension}`;
Expand Down
13 changes: 7 additions & 6 deletions src/lib/interceptor/interceptor.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
template,
url,
} from '@angular-devkit/schematics';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import { normalizeToCase } from '../../utils/formatting';
import { Location, NameParser } from '../../utils/name.parser';
import { mergeSourceRoot } from '../../utils/source-root.helpers';
import { InterceptorOptions } from './interceptor.schema';
Expand All @@ -29,11 +29,12 @@ function transform(options: InterceptorOptions): InterceptorOptions {
throw new SchematicsException('Option (name) is required.');
}
const location: Location = new NameParser().parse(target);
target.name = normalizeToKebabOrSnakeCase(location.name);
target.path = normalizeToKebabOrSnakeCase(location.path);
target.name = normalizeToCase(location.name, 'kebab-or-snake');
target.path = normalizeToCase(location.path, 'kebab-or-snake');
target.language = target.language !== undefined ? target.language : 'ts';
target.specFileSuffix = normalizeToKebabOrSnakeCase(
target.specFileSuffix = normalizeToCase(
options.specFileSuffix || 'spec',
'kebab-or-snake'
);

target.path = target.flat
Expand All @@ -46,8 +47,8 @@ function generate(options: InterceptorOptions): Source {
return (context: SchematicContext) =>
apply(url(join('./files' as Path, options.language)), [
options.spec ? noop() : filter((path) => !path.endsWith('.spec.ts')),
options.spec
? noop()
options.spec
? noop()
: filter((path) => {
const languageExtension = options.language || 'ts';
const suffix = `.__specFileSuffix__.${languageExtension}`;
Expand Down
Loading