From 6a24b10c8b8d754e2fb2903ccb1d6e6a06ff63d6 Mon Sep 17 00:00:00 2001 From: Bert De Block Date: Wed, 20 Mar 2024 10:39:30 +0100 Subject: [PATCH] Centralize string transforms --- documents/component.md | 28 ++++++++++------------------ documents/helper.md | 30 ++++++++++-------------------- documents/modifier.md | 30 ++++++++++-------------------- documents/service.md | 8 +++----- package.json | 1 + pnpm-lock.yaml | 7 +++++++ src/generate-document.ts | 12 +++++++++++- 7 files changed, 52 insertions(+), 64 deletions(-) diff --git a/documents/component.md b/documents/component.md index 1468464..b053542 100644 --- a/documents/component.md +++ b/documents/component.md @@ -4,35 +4,30 @@ root: "." output: "**/*" --- -# {{inputs.ts ? "!" : (inputs.classBased ? "!" : "")}}{{inputs.name}}.gjs +# {{inputs.ts ? "!" : (inputs.classBased ? "!" : "")}}{{inputs.name.kebab}}.gjs ```gjs ``` -# {{inputs.ts ? "!" : (inputs.classBased ? "" : "!")}}{{inputs.name}}.gjs +# {{inputs.ts ? "!" : (inputs.classBased ? "" : "!")}}{{inputs.name.kebab}}.gjs ```gjs -{{name := pascal(inputs.name)-}} - import Component from "@glimmer/component"; -export default class {{name}} extends Component { +export default class {{inputs.name.pascal}} extends Component { } ``` -# {{inputs.ts ? (inputs.classBased ? "!" : "") : "!"}}{{inputs.name}}.gts +# {{inputs.ts ? (inputs.classBased ? "!" : "") : "!"}}{{inputs.name.kebab}}.gts ```gts -{{name := pascal(inputs.name)-}} -{{signature := (name + "Signature")-}} - import type { TOC } from '@ember/component/template-only'; -export interface {{signature}} { +export interface {{inputs.signature}} { Args: {}; Blocks: { default: []; @@ -40,21 +35,18 @@ export interface {{signature}} { Element: null; } -const {{name}}: TOC<{{signature}}> = ; +const {{inputs.name.pascal}}: TOC<{{inputs.signature}}> = ; -export default {{name}}; +export default {{inputs.name.pascal}}; ``` -# {{inputs.ts ? (inputs.classBased ? "" : "!") : "!"}}{{inputs.name}}.gts +# {{inputs.ts ? (inputs.classBased ? "" : "!") : "!"}}{{inputs.name.kebab}}.gts ```gts -{{name := pascal(inputs.name)-}} -{{signature := (name + "Signature")-}} - import Component from "@glimmer/component"; -export interface {{signature}} { +export interface {{inputs.signature}} { Args: {}; Blocks: { default: []; @@ -62,7 +54,7 @@ export interface {{signature}} { Element: null; } -export default class {{name}} extends Component<{{signature}}> { +export default class {{inputs.name.pascal}} extends Component<{{inputs.signature}}> { diff --git a/documents/helper.md b/documents/helper.md index 87b3e24..52754cc 100644 --- a/documents/helper.md +++ b/documents/helper.md @@ -4,27 +4,23 @@ root: "." output: "**/*" --- -# {{inputs.ts ? "!" : (inputs.classBased ? "!" : "")}}{{inputs.name}}.js +# {{inputs.ts ? "!" : (inputs.classBased ? "!" : "")}}{{inputs.name.kebab}}.js ```js -{{name := camel(inputs.name)-}} - import { helper } from "@ember/component/helper"; -export default helper(function {{name}}(positional, named) { +export default helper(function {{inputs.name.camel}}(positional, named) { return positional; }); ``` -# {{inputs.ts ? "!" : (inputs.classBased ? "" : "!")}}{{inputs.name}}.js +# {{inputs.ts ? "!" : (inputs.classBased ? "" : "!")}}{{inputs.name.kebab}}.js ```js -{{name := pascal(inputs.name)-}} - import Helper from "@ember/component/helper"; -export default class {{name}} extends Helper { +export default class {{inputs.name.pascal}} extends Helper { compute(positional, named) { return positional; } @@ -32,19 +28,16 @@ export default class {{name}} extends Helper { ``` -# {{inputs.ts ? (inputs.classBased ? "!" : "") : "!"}}{{inputs.name}}.ts +# {{inputs.ts ? (inputs.classBased ? "!" : "") : "!"}}{{inputs.name.kebab}}.ts ```ts -{{name := camel(inputs.name)-}} -{{signature := (pascal(inputs.name) + "Signature")-}} - import { helper } from "@ember/component/helper"; type Named = {}; type Positional = []; type Return = Positional; -export interface {{signature}} { +export interface {{inputs.signature}} { Args: { Named: Named; Positional: Positional; @@ -52,25 +45,22 @@ export interface {{signature}} { Return: Return; } -export default helper<{{signature}}>(function {{name}}(positional, named) { +export default helper<{{inputs.signature}}>(function {{inputs.name.camel}}(positional, named) { return positional; }); ``` -# {{inputs.ts ? (inputs.classBased ? "" : "!") : "!"}}{{inputs.name}}.ts +# {{inputs.ts ? (inputs.classBased ? "" : "!") : "!"}}{{inputs.name.kebab}}.ts ```ts -{{name := pascal(inputs.name)-}} -{{signature := (pascal(inputs.name) + "Signature")-}} - import Helper from "@ember/component/helper"; type Named = {}; type Positional = []; type Return = Positional; -export interface {{signature}} { +export interface {{inputs.signature}} { Args: { Named: Named; Positional: Positional; @@ -78,7 +68,7 @@ export interface {{signature}} { Return: Return; } -export default class {{name}} extends Helper<{{signature}}> { +export default class {{inputs.name.pascal}} extends Helper<{{inputs.signature}}> { compute(positional: Positional, named: Named): Return { return positional; } diff --git a/documents/modifier.md b/documents/modifier.md index eb7aca8..270792e 100644 --- a/documents/modifier.md +++ b/documents/modifier.md @@ -4,39 +4,32 @@ root: "." output: "**/*" --- -# {{inputs.ts ? "!" : (inputs.classBased ? "!" : "")}}{{inputs.name}}.js +# {{inputs.ts ? "!" : (inputs.classBased ? "!" : "")}}{{inputs.name.kebab}}.js ```js -{{name := camel(inputs.name)-}} - import { modifier } from "ember-modifier"; -export default modifier(function {{name}}(element, positional, named) {}); +export default modifier(function {{inputs.name.camel}}(element, positional, named) {}); ``` -# {{inputs.ts ? "!" : (inputs.classBased ? "" : "!")}}{{inputs.name}}.js +# {{inputs.ts ? "!" : (inputs.classBased ? "" : "!")}}{{inputs.name.kebab}}.js ```js -{{name := pascal(inputs.name)-}} - import Modifier from "ember-modifier"; -export default class {{name}} extends Modifier { +export default class {{inputs.name.pascal}} extends Modifier { modify(element, positional, named) {} } ``` -# {{inputs.ts ? (inputs.classBased ? "!" : "") : "!"}}{{inputs.name}}.ts +# {{inputs.ts ? (inputs.classBased ? "!" : "") : "!"}}{{inputs.name.kebab}}.ts ```ts -{{name := camel(inputs.name)-}} -{{signature := (pascal(inputs.name) + "Signature")-}} - import { modifier } from "ember-modifier"; -export interface {{signature}} { +export interface {{inputs.signature}} { Args: { Named: {}; Positional: []; @@ -44,23 +37,20 @@ export interface {{signature}} { Element: null; } -export default modifier<{{signature}}>(function {{name}}(element, positional, named) {}); +export default modifier<{{inputs.signature}}>(function {{inputs.name.camel}}(element, positional, named) {}); ``` -# {{inputs.ts ? (inputs.classBased ? "" : "!") : "!"}}{{inputs.name}}.ts +# {{inputs.ts ? (inputs.classBased ? "" : "!") : "!"}}{{inputs.name.kebab}}.ts ```ts -{{name := pascal(inputs.name)-}} -{{signature := (pascal(inputs.name) + "Signature")-}} - import Modifier from "ember-modifier"; type Named = {}; type Positional = []; type Element = null; -export interface {{signature}} { +export interface {{inputs.signature}} { Args: { Named: Named; Positional: Positional; @@ -68,7 +58,7 @@ export interface {{signature}} { Element: Element; } -export default class {{name}} extends Modifier<{{signature}}> { +export default class {{inputs.name.pascal}} extends Modifier<{{inputs.signature}}> { modify(element: Element, positional: Positional, named: Named) {} } diff --git a/documents/service.md b/documents/service.md index 5867c45..f0d517b 100644 --- a/documents/service.md +++ b/documents/service.md @@ -4,18 +4,16 @@ root: "." output: "**/*" --- -# {{inputs.name}}.{{inputs.ts ? "ts" : "js"}} +# {{inputs.name.kebab}}.{{inputs.ts ? "ts" : "js"}} ```ts -{{name := pascal(inputs.name)-}} - import Service from "@ember/service"; -export default class {{name}} extends Service {} +export default class {{inputs.name.pascal}} extends Service {} {{if inputs.ts}} declare module "@ember/service" { interface Registry { - "{{inputs.name}}": {{name}}; + "{{inputs.name.kebab}}": {{inputs.name.pascal}}; } } {{end}} diff --git a/package.json b/package.json index e808ca5..49f6909 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ }, "dependencies": { "chalk": "^5.3.0", + "change-case": "^5.4.3", "fs-extra": "^11.2.0", "scaffdog": "^3.0.0", "yargs": "^17.7.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4f3a0fb..c3c7151 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ dependencies: chalk: specifier: ^5.3.0 version: 5.3.0 + change-case: + specifier: ^5.4.3 + version: 5.4.3 fs-extra: specifier: ^11.2.0 version: 11.2.0 @@ -1561,6 +1564,10 @@ packages: tslib: 2.6.2 dev: false + /change-case@5.4.3: + resolution: {integrity: sha512-4cdyvorTy/lViZlVzw2O8/hHCLUuHqp4KpSSP3DlauhFCf3LdnfF+p5s0EAhjKsU7bqrMzu7iQArYfoPiHO2nw==} + dev: false + /character-entities@2.0.2: resolution: {integrity: sha512-shx7oQ0Awen/BRIdkjkvz54PnEEI/EjwXDSIZp86/KKdbafHh1Df/RYGBhn4hbe2+uKC9FnT5UCEdyPz3ai9hQ==} dev: false diff --git a/src/generate-document.ts b/src/generate-document.ts index 36e72a7..ff3c464 100644 --- a/src/generate-document.ts +++ b/src/generate-document.ts @@ -1,4 +1,5 @@ import chalk from "chalk"; +import { camelCase, kebabCase, pascalCase } from "change-case"; import { ensureDir } from "fs-extra"; import { writeFile } from "node:fs/promises"; import { dirname, isAbsolute, join, parse, relative } from "node:path"; @@ -31,7 +32,16 @@ export async function generateDocument( const documentPath = getDocumentPath(documentName, cwd, path); const files = await scaffdog.generate(document, documentPath, { - inputs: { ...inputs, name: entityName }, + inputs: { + ...inputs, + name: { + camel: camelCase(entityName), + kebab: kebabCase(entityName), + pascal: pascalCase(entityName), + raw: entityName, + }, + signature: pascalCase(entityName) + "Signature", + }, }); for (const file of files) {