Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
21 changes: 19 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestjs/schematics",
"version": "10.0.1",
"version": "10.1.0",
"description": "Nest - modern, fast, powerful node.js web framework (@schematics)",
"main": "dist/index.js",
"files": [
Expand Down Expand Up @@ -43,6 +43,7 @@
"dependencies": {
"@angular-devkit/core": "16.1.6",
"@angular-devkit/schematics": "16.1.6",
"case-anything": "2.1.13",
"comment-json": "4.2.3",
"jsonc-parser": "3.2.0",
"pluralize": "8.0.0"
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
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.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' | '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