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 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
838 changes: 493 additions & 345 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,31 @@
"dependencies": {
"@angular-devkit/core": "16.2.1",
"@angular-devkit/schematics": "16.2.1",
"case-anything": "2.1.13",
"comment-json": "4.2.3",
"jsonc-parser": "3.2.0",
"pluralize": "8.0.0"
},
"devDependencies": {
"@commitlint/cli": "17.7.2",
"@commitlint/config-angular": "17.7.0",
"@types/jest": "29.5.5",
"@types/node": "18.18.0",
"@typescript-eslint/eslint-plugin": "6.7.3",
"@typescript-eslint/parser": "6.7.3",
"@commitlint/cli": "17.6.7",
"@commitlint/config-angular": "17.6.7",
"@types/jest": "29.5.3",
"@types/node": "18.17.1",
"@typescript-eslint/eslint-plugin": "6.2.0",
"@typescript-eslint/parser": "6.2.0",
"cpx": "1.5.0",
"eslint": "8.50.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-import": "2.28.1",
"eslint": "8.46.0",
"eslint-config-prettier": "8.9.0",
"eslint-plugin-import": "2.28.0",
"gulp": "4.0.2",
"gulp-clean": "0.4.0",
"husky": "8.0.3",
"jest": "29.7.0",
"jest": "29.6.2",
"nyc": "15.1.0",
"release-it": "16.2.1",
"release-it": "16.1.3",
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"typescript": "5.2.2"
espoal marked this conversation as resolved.
Show resolved Hide resolved
"typescript": "5.1.6"
},
"peerDependencies": {
"typescript": ">=4.8.2"
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
16 changes: 9 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,17 @@ 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.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 +66,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' | 'capital'.
*/
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 {}
56 changes: 55 additions & 1 deletion src/utils/formatting.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,61 @@
import {
camelCase,
kebabCase,
pascalCase,
snakeCase,
capitalCase,
} from 'case-anything';

export type CaseType =
| 'kebab'
| 'snake'
| 'camel'
| 'pascal'
| 'capital'
| 'kebab-or-snake';

/**
*
* @param str
* @returns formated string
* @param caseType
* @returns formatted string
* @description normalizes input to a given case format.
*/
export const normalizeToCase = (
str: string,
caseType: CaseType = 'kebab-or-snake',
) => {
switch (caseType) {
case 'kebab':
return kebabCase(str);
case 'snake':
return snakeCase(str);
case 'camel':
return camelCase(str);
case 'pascal':
return pascalCase(str);
case 'capital':
return capitalCase(str);
// For legacy purposes
case 'kebab-or-snake':
default:
return normalizeToKebabOrSnakeCase(str);
}
};

export const formatString = (str: string) => {
return str.split('').reduce((content, char) => {
if (char === '(' || char === ')' || char === '[' || char === ']') {
return `${content}\\${char}`;
}
return `${content}${char}`;
}, '');
};

/**
*
* @param str
* @returns formatted string
* @description normalizes input to supported path and file name format.
* Changes camelCase strings to kebab-case, replaces spaces with dash and keeps underscores.
*/
Expand Down