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 17 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
17 changes: 17 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"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"
Expand Down
7 changes: 6 additions & 1 deletion src/lib/application/application.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ describe('Application Factory', () => {
describe('when only the name is supplied', () => {
it('should manage basic (ie., cross-platform) name', async () => {
const options: ApplicationOptions = {
name: 'project',
name: 'project'
};
console.log({ options})
const tree: UnitTestTree = await runner
.runSchematicAsync('application', options)
.toPromise();
console.log({ tree})

const files: string[] = tree.files;
console.log({ files })
espoal marked this conversation as resolved.
Show resolved Hide resolved

expect(files).toEqual([
'/project/.eslintrc.js',
'/project/.gitignore',
Expand Down
7 changes: 5 additions & 2 deletions src/lib/application/application.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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 @@ -19,7 +19,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 @@ -41,6 +41,9 @@ function transform(options: ApplicationOptions): ApplicationOptions {
target.name = resolvePackageName(target.name.toString());
target.version = !!target.version ? target.version : DEFAULT_VERSION;

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,4 +40,8 @@ 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;
}
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 @@ -55,6 +55,10 @@
"devDependencies": {
"type": "string",
"description": "Nest application development dependencies."
},
"caseNaming": {
"type": "string",
"description": "Case format. Options are 'kebab' | 'snake' | 'camel' | 'pascal' | 'capital'"
espoal marked this conversation as resolved.
Show resolved Hide resolved
}
},
"required": ["name"]
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'.
*/
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