Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

feat: name casing #2

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft

feat: name casing #2

wants to merge 6 commits into from

Conversation

espoal
Copy link

@espoal espoal commented Jul 14, 2023

Summary

This is the second of a multi PR effort aiming at supporting custom name casing conventions, as detailed here.

  1. The first PR attempts to update @nestjs/schematics to accept a caseType option when generating a controller.
  2. In this PR we add a --case option to the CLI so that it can be correctly passed to the schematic generator.
  3. A third PR will add support for filenames casing convention, on top of variable names, and extend this approach to classes other than controllers

For now I decided to support only controllers, to reduce the changes size and make reviewing easier. If the approach will be approved I will make sure to extend it to other objects and to add more tests.

How to run

Run:

npm install
npm run build

Then clone this branch, and do:

npm install
npm run build

Copy the dist folder in the previous repo nest-cli/node_modules/@nestjs/schematics/dist/

Then run

node bin/nest.js g co keb-pap --case camel

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

Currently @nestjs/cli uses kebap-case, as mentioned in this issue.

Issue Number: 462

What is the new behavior?

The goal is to add a --case option to the CLI so that generated files will follow the desired name casing convention.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

@espoal espoal self-assigned this Jul 14, 2023
@@ -1,3 +1,48 @@
import { CaseToCase } from 'case-to-case'
Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure this is the best package to do this


export class SchematicOption {
constructor(private name: string, private value: boolean | string) {}
constructor(private name: string, private value: boolean | string, private caseType: CaseType) {}
Copy link
Author

Choose a reason for hiding this comment

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

I decided to pass CaseType to the constructor of SchematicOption, so that I can pass the flag to the schematics package and I can format each field accordingly.
This approach adds more noise than I would like, so I'm open to a better solution if anyone can find it

@espoal espoal requested review from bennycode and igrek8 July 14, 2023 12:50
@espoal espoal mentioned this pull request Jul 14, 2023
12 tasks
const excludedInputNames = ['schematic', 'spec', 'flat', 'specFileSuffix'];
const options: SchematicOption[] = [];
inputs.forEach((input) => {
if (!excludedInputNames.includes(input.name) && input.value !== undefined) {
options.push(new SchematicOption(input.name, input.value));
options.push(new SchematicOption(input.name, input.value, caseType));
Copy link

Choose a reason for hiding this comment

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

I would introduce a key-value options here since it has 3 arguments.

Copy link
Author

@espoal espoal Jul 27, 2023

Choose a reason for hiding this comment

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

You mean adding something like SchmaticOption('naming', 'camelCase')?
That's the first thing I tried but I couldn't make it work because when we launch the command nest generate controller emails --naming=camelCase we have two schematic options:

  • SchmaticOption('name', 'emails')
  • SchmaticOption('suffix', 'controller')

We need to apply the camelCase transform to both, hence why I used a 3 way option.
If I misunderstood you please let me know, or maybe you can see something I'm not seeing.
One option could be to open a draft PR and ask the nestjs authors for advice.

Copy link
Author

@espoal espoal Jul 28, 2023

Choose a reason for hiding this comment

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

Proposal: nameCase becomes an extensible object (additionalParameters = { caseNaming: hello })

@@ -48,6 +48,10 @@ export class GenerateCommand extends AbstractCommand {
'-c, --collection [collectionName]',
'Schematics collection to use.',
)
.option(
'--case [case]',
Copy link

Choose a reason for hiding this comment

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

--naming

Copy link
Author

Choose a reason for hiding this comment

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

--caseNaming
--fileCaseNaming
--directoryCaseNaming
--variableCaseNaming

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants