diff --git a/README.md b/README.md index a6a92a5e..96735c16 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,12 @@ module.exports = { // select what kind of types you want to generate (default `['enum', 'constant', 'literalId', 'literalKey']`) types: ['constant', 'literalId'], // render the types with `'` instead of `"` (default is `"`) - singleQuotes: true + singleQuotes: true, + // customise names used for the generated types and constants + enumName: 'MyIconType', + constantName: 'MY_CODEPOINTS' + // literalIdName: 'IconId', + // literalKeyName: 'IconKey' } }, // Use a custom Handlebars template diff --git a/package-lock.json b/package-lock.json index 9c963af8..c6ae0ad7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "change-case": "^4.1.2", - "cli-color": "^2.0.3", + "cli-color": "^2.0.2", "commander": "^9.4.1", "glob": "^8.0.3", "handlebars": "^4.7.7", diff --git a/package.json b/package.json index 1b5151c7..43d894d5 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "prepublish": "build", "dependencies": { "change-case": "^4.1.2", - "cli-color": "^2.0.3", + "cli-color": "^2.0.2", "commander": "^9.4.1", "glob": "^8.0.3", "handlebars": "^4.7.7", diff --git a/src/generators/asset-types/__tests__/__snapshots__/ts.ts.snap b/src/generators/asset-types/__tests__/__snapshots__/ts.ts.snap index 74517ab6..08abb4c4 100644 --- a/src/generators/asset-types/__tests__/__snapshots__/ts.ts.snap +++ b/src/generators/asset-types/__tests__/__snapshots__/ts.ts.snap @@ -32,7 +32,7 @@ export const MY_ICONS_SET_CODEPOINTS: { [key in MyIconsSetKey]: string } = { " `; -exports[`\`TS\` asset generator generates no key string literal type if option passed like that 1`] = ` +exports[`\`TS\` asset generator generates no key string literal type if option specifies it 1`] = ` "export enum MyIconsSet { Foo = "foo", Bar = "bar", diff --git a/src/generators/asset-types/__tests__/ts.ts b/src/generators/asset-types/__tests__/ts.ts index 9ed4198f..3f425945 100644 --- a/src/generators/asset-types/__tests__/ts.ts +++ b/src/generators/asset-types/__tests__/ts.ts @@ -33,42 +33,60 @@ const getCleanGen = async (options = {}) => ); describe('`TS` asset generator', () => { - test('renders expected TypeScript module content', async () => { + it('renders expected TypeScript module content', async () => { expect(await tsGen.generate(mockOptions, null)).toMatchSnapshot(); }); - test('correctly renders type declaration', async () => { - expect(await getCleanGen()).toContain( - 'export type MyIconsSetId = | "foo" | "bar";' + it.each([ + { formatOptions: { ts: { literalIdName: undefined } } }, + undefined, + { formatOptions: { ts: { literalIdName: 'Foo' } } } + ])('generates correct type declaration - config: %j', async options => { + expect(await getCleanGen({ ...mockOptions, ...options })).toContain( + `export type ${ + options?.formatOptions?.ts?.literalIdName || 'MyIconsSetId' + } = | "foo" | "bar";` ); }); - test('correctly enum declaration', async () => { - expect(await getCleanGen()).toContain( - 'export enum MyIconsSet { Foo = "foo", Bar = "bar", }' + it.each([ + { formatOptions: { ts: { enumName: undefined } } }, + undefined, + { formatOptions: { ts: { enumName: 'Foo' } } } + ])('generates correct enum declaration - config: %j', async options => { + expect(await getCleanGen({ ...mockOptions, ...options })).toContain( + `export enum ${ + options?.formatOptions?.ts?.enumName || 'MyIconsSet' + } { Foo = "foo", Bar = "bar", }` ); }); - test('correctly codepoints declaration', async () => { - expect(await getCleanGen()).toContain( - 'export const MY_ICONS_SET_CODEPOINTS: { [key in MyIconsSet]: string }' + - ' = { [MyIconsSet.Foo]: "4265", [MyIconsSet.Bar]: "1231", };' - ); - }); + it.each([ + { formatOptions: { ts: { constantName: undefined } } }, + undefined, + { formatOptions: { ts: { constantName: 'FOO' } } } + ])( + 'generates correct codepoints declaration - options: %j', + async options => { + expect(await getCleanGen({ ...mockOptions, ...options })).toContain( + `export const ${ + options?.formatOptions?.ts?.constantName || 'MY_ICONS_SET_CODEPOINTS' + }: { [key in MyIconsSet]: string }` + + ' = { [MyIconsSet.Foo]: "4265", [MyIconsSet.Bar]: "1231", };' + ); + } + ); - test('generates single quotes if format option passed', async () => { + it('generates single quotes if format option passed', async () => { expect( await tsGen.generate( - { - ...mockOptions, - formatOptions: { ts: { singleQuotes: true } } - }, + { ...mockOptions, formatOptions: { ts: { singleQuotes: true } } }, null ) ).toMatchSnapshot(); }); - test('generates no key string literal type if option passed like that', async () => { + it('generates no key string literal type if option specifies it', async () => { const result = await tsGen.generate( { ...mockOptions, @@ -87,7 +105,7 @@ describe('`TS` asset generator', () => { ); }); - test('generates constant with literalId if no enum generated', async () => { + it('generates constant with literalId if no enum generated', async () => { const result = await tsGen.generate( { ...mockOptions, @@ -108,7 +126,7 @@ describe('`TS` asset generator', () => { expect(cleanResult).not.toContain('export enum MyIconsSet'); }); - test('generates constant with literalKey if no enum generated', async () => { + it('generates constant with literalKey if no enum generated', async () => { const result = await tsGen.generate( { ...mockOptions, @@ -129,7 +147,7 @@ describe('`TS` asset generator', () => { expect(cleanResult).not.toContain('export enum MyIconsSet'); }); - test('generates constant only', async () => { + it('generates constant only', async () => { const result = await tsGen.generate( { ...mockOptions, @@ -150,7 +168,7 @@ describe('`TS` asset generator', () => { expect(cleanResult).not.toContain('export enum MyIconsSet'); }); - test('prevents enum keys that start with digits', async () => { + it('prevents enum keys that start with digits', async () => { const result = await tsGen.generate( { ...mockOptions, @@ -172,7 +190,7 @@ describe('`TS` asset generator', () => { ); }); - test('prevents enum keys that start with digits when digits and chars', async () => { + it('prevents enum keys that start with digits when digits and chars', async () => { const result = await tsGen.generate( { ...mockOptions, diff --git a/src/generators/asset-types/ts.ts b/src/generators/asset-types/ts.ts index 48a05afb..159bb0cc 100644 --- a/src/generators/asset-types/ts.ts +++ b/src/generators/asset-types/ts.ts @@ -27,7 +27,7 @@ const generateEnums = ( ].join('\n'); const generateConstant = ({ - codepointsName, + constantName, enumName, literalIdName, literalKeyName, @@ -36,7 +36,7 @@ const generateConstant = ({ quote = '"', kind = {} }: { - codepointsName: string; + constantName: string; enumName: string; literalIdName: string; literalKeyName: string; @@ -56,7 +56,7 @@ const generateConstant = ({ } return [ - `export const ${codepointsName}${varType} = {`, + `export const ${constantName}${varType} = {`, Object.entries(enumKeys) .map(([enumValue, enumKey]) => { const key = kind.enum @@ -95,11 +95,11 @@ const generator: FontGenerator = { .map(kind => ({ [kind]: true })) .reduce((prev, curr) => Object.assign(prev, curr), {}); - const enumName = pascalCase(name); - const codepointsName = `${constantCase(name)}_CODEPOINTS`; - const literalIdName = `${pascalCase(name)}Id`; - const literalKeyName = `${pascalCase(name)}Key`; - const names = { enumName, codepointsName, literalIdName, literalKeyName }; + const enumName = ts?.enumName || pascalCase(name); + const constantName = ts?.constantName || `${constantCase(name)}_CODEPOINTS`; + const literalIdName = ts?.literalIdName || `${pascalCase(name)}Id`; + const literalKeyName = ts?.literalKeyName || `${pascalCase(name)}Key`; + const names = { enumName, constantName, literalIdName, literalKeyName }; const enumKeys = generateEnumKeys(Object.keys(assets)); diff --git a/src/types/format.ts b/src/types/format.ts index 6438e766..b7f15b85 100644 --- a/src/types/format.ts +++ b/src/types/format.ts @@ -17,6 +17,10 @@ interface JsonOptions { interface TsOptions { types?: ('enum' | 'constant' | 'literalId' | 'literalKey')[]; singleQuotes?: boolean; + enumName?: string; + constantName?: string; + literalIdName?: string; + literalKeyName?: string; } export interface FormatOptions {