diff --git a/src/lib/converters/constants.converter.ts b/src/lib/converters/constants.converter.ts index 30bfc2c..e5c8201 100644 --- a/src/lib/converters/constants.converter.ts +++ b/src/lib/converters/constants.converter.ts @@ -1,7 +1,8 @@ import { generateInterfaceDefinition, generateSvgConstant, - generateTypeDefinition + generateTypeDefinition, + generateTypeHelper } from '../generators/code-snippet-generators'; import { writeFile } from '../helpers/file-helpers'; import { error, success, underlineSuccess } from '../helpers/log-helper'; @@ -17,14 +18,15 @@ const getSvgConstants = svgDefinitions => { }; export const convertToConstants = async (conversionOptions: ConstantsConversionOptions): Promise => { - const { outputDirectory, fileName } = conversionOptions; + const { outputDirectory, fileName, interfaceName } = conversionOptions; try { const svgDefinitions = await filesProcessor(conversionOptions); if (svgDefinitions.length) { const svgContants = getSvgConstants(svgDefinitions); const typeDefinition = generateTypeDefinition(conversionOptions, svgDefinitions); const interfaceDefinition = generateInterfaceDefinition(conversionOptions); - const fileContent = `${svgContants}${typeDefinition}${interfaceDefinition}`; + const typeHelper = generateTypeHelper(interfaceName); + const fileContent = `${svgContants}${typeDefinition}${interfaceDefinition}${typeHelper}`; await writeFile(outputDirectory, fileName, fileContent); success(`Icons file successfully generated under ${underlineSuccess(outputDirectory)}`); } diff --git a/src/lib/converters/files.converter.ts b/src/lib/converters/files.converter.ts index 0c4dcb4..321670b 100644 --- a/src/lib/converters/files.converter.ts +++ b/src/lib/converters/files.converter.ts @@ -3,7 +3,8 @@ import { generateExportStatement, generateInterfaceDefinition, generateSvgConstant, - generateTypeDefinition + generateTypeDefinition, + generateTypeHelperWithImport } from '../generators/code-snippet-generators'; import { generateCompleteIconSetContent } from '../helpers/complete-icon-set.helper'; import { deleteFiles, deleteFolder, writeFile } from '../helpers/file-helpers'; @@ -19,6 +20,7 @@ export const convertToFiles = async (conversionOptions: FileConversionOptions): modelFileName, additionalModelOutputPath, iconsFolderName, + interfaceName, compileSources, exportCompleteIconSet, barrelFileName @@ -49,10 +51,10 @@ export const convertToFiles = async (conversionOptions: FileConversionOptions): generatedFileNames.push(completeIconSetFileName); } - let indexFileContent = generatedFileNames + let indexFileContent = generateTypeHelperWithImport(interfaceName, iconsFolderName, modelFileName); + indexFileContent += generatedFileNames .map((generatedFileName: string) => generateExportStatement(generatedFileName, iconsFolderName)) .join(''); - separatorEnd(); indexFileContent += generateExportStatement(modelFileName, iconsFolderName); diff --git a/src/lib/generators/code-snippet-generators.spec.ts b/src/lib/generators/code-snippet-generators.spec.ts index 33a9bc6..8c0fda0 100644 --- a/src/lib/generators/code-snippet-generators.spec.ts +++ b/src/lib/generators/code-snippet-generators.spec.ts @@ -1,4 +1,5 @@ import { SvgDefinition } from '../converters/shared.converter'; +import { unformatedString } from '../helpers/test-helpers'; import { ConstantsConversionOptions } from '../options/conversion-options'; import { @@ -7,6 +8,8 @@ import { generateNamedImportStatement, generateSvgConstant, generateTypeDefinition, + generateTypeHelper, + generateTypeHelperWithImport, generateTypeName, generateVariableName } from './code-snippet-generators'; @@ -148,4 +151,30 @@ describe('Generators', () => { expect(generatedNamedImpoort).toEqual(expectedNamedImport); }); }); + + describe('Type helper', () => { + it('should generate the correct type helper statement', () => { + const interfaceName = 'MyIcons'; + const expectedStatement = ` + export type IconNameSubset> = T[number]['name']; + `; + + const generatedStatement = generateTypeHelper(interfaceName); + expect(unformatedString(generatedStatement)).toEqual(unformatedString(expectedStatement)); + }); + + it('should generate the correct type helper and import statement', () => { + const interfaceName = 'MyIcon'; + const modelFileName = 'my-icons'; + const iconsFolderName = 'build'; + + const expectedStatement = ` + import {${interfaceName}} from './${iconsFolderName}/${modelFileName}'; + export type IconNameSubset> = T[number]['name']; + `; + + const generatedStatement = generateTypeHelperWithImport(interfaceName, iconsFolderName, modelFileName); + expect(unformatedString(generatedStatement)).toEqual(unformatedString(expectedStatement)); + }); + }); }); diff --git a/src/lib/generators/code-snippet-generators.ts b/src/lib/generators/code-snippet-generators.ts index 01c5597..116b96b 100644 --- a/src/lib/generators/code-snippet-generators.ts +++ b/src/lib/generators/code-snippet-generators.ts @@ -88,6 +88,19 @@ export const generateVariableName = (prefix: string, filenameWithoutEnding): str return prefix ? `${prefix}${capitalize(camelCased)}` : camelCased; }; +export const generateTypeHelper = (interfaceName: string): string => ` + export type IconNameSubset> = T[number]['name']; + `; + +export const generateTypeHelperWithImport = ( + interfaceName: string, + iconsFolderName: string, + modelFileName: string +): string => ` + import {${interfaceName}} from './${iconsFolderName}/${modelFileName}'; + ${generateTypeHelper(interfaceName)} + `; + const capitalize = (value: string): string => { return value.charAt(0).toUpperCase() + value.slice(1); };