diff --git a/package.json b/package.json index b86f498..9bf0fd2 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "start:ofl:regex": "ts-node ./src/bin/svg-to-ts.ts --optimizeForLazyLoading true -s './inputfilesRegex/**/*.svg'", "start:ofl:kebap": "ts-node ./src/bin/svg-to-ts.ts --optimizeForLazyLoading true -s './inputfiles/*.svg' -d KEBAB", "start:ofl:multiple-source": "ts-node ./src/bin/svg-to-ts.ts --optimizeForLazyLoading true -s './inputfiles/*.svg' -s './inputfilesBis/*.svg'", + "start:ofl:customModel": "ts-node ./src/bin/svg-to-ts.ts --optimizeForLazyLoading true -s './inputfilesRegex/**/*.svg' --modelOutputPath ./dist/model --modelFileName test-model -t sampleIcon -i SampleIcon", "start:ofl:custom": "ts-node ./src/bin/svg-to-ts.ts --optimizeForLazyLoading true -s './inputfiles/*.svg' -o ./dist -t sampleIcon -i SampleIcon -p sampleIcon -f icons", "semantic-release": "semantic-release" }, diff --git a/src/bin/svg-to-ts.ts b/src/bin/svg-to-ts.ts index f789fdc..e6ceecc 100755 --- a/src/bin/svg-to-ts.ts +++ b/src/bin/svg-to-ts.ts @@ -15,6 +15,8 @@ export interface ConvertionOptions { interfaceName: string; srcFiles: string[]; outputDirectory: string; + modelOutputPath: string; + modelFileName: string; } const DEFAULTS = { @@ -46,9 +48,20 @@ commander .option('-s --srcFiles ', 'name of the source directory', collect, []) .option('-o --outputDirectory ', 'name of the output directory', DEFAULTS.outputDirectory) .option('--optimizeForLazyLoading ', 'optimize the output for lazyloading', DEFAULTS.optimizeForLazyLoading) + .option('--modelOutputPath ', 'Output path for the types file') + .option('--modelFileName ', 'FileName of the model file') .parse(process.argv); -const { delimiter, fileName, interfaceName, outputDirectory, prefix, typeName } = commander; +const { + delimiter, + fileName, + interfaceName, + outputDirectory, + prefix, + typeName, + modelFileName, + modelOutputPath +} = commander; // Because of commander adding default value to params // See: https://stackoverflow.com/questions/30238654/commander-js-collect-multiple-options-always-include-default @@ -65,7 +78,9 @@ const convertionOptions = { prefix, interfaceName, srcFiles, - outputDirectory + outputDirectory, + modelOutputPath, + modelFileName }; if (optimizeForLazyLoading) { diff --git a/src/lib/converters/multiple-files.converter.ts b/src/lib/converters/multiple-files.converter.ts index 60d1f12..1308c7d 100644 --- a/src/lib/converters/multiple-files.converter.ts +++ b/src/lib/converters/multiple-files.converter.ts @@ -4,6 +4,8 @@ import { ConvertionOptions } from '../../bin/svg-to-ts'; import { generateExportStatement, + generateInterfaceDefinition, + generateTypeDefinition, generateTypeName, generateUntypedSvgConstant, generateVariableName @@ -15,10 +17,21 @@ import { info, success } from '../helpers/log-helper'; import { svgOptimizer } from '../helpers/svg-optimization'; const generateIconsFolderName = 'build'; +const typesDelimitor = ' | '; export const convertToMultipleFiles = async (convertionOptions: ConvertionOptions): Promise => { - const { prefix, delimiter, outputDirectory, srcFiles } = convertionOptions; + const { + typeName, + interfaceName, + prefix, + delimiter, + outputDirectory, + srcFiles, + modelOutputPath, + modelFileName + } = convertionOptions; let indexFileContent = ''; + let types = generateTypeDefinition(typeName); try { const filePaths = await getFilePathsFromRegex(srcFiles); @@ -40,6 +53,8 @@ export const convertToMultipleFiles = async (convertionOptions: ConvertionOption indexFileContent += generateExportStatement(generatedFileName, generateIconsFolderName); await writeFile(`${outputDirectory}/${generateIconsFolderName}`, generatedFileName, svgConstant); info(`write file svg: ${outputDirectory}/${generateIconsFolderName}/${generatedFileName}.ts`); + + types += i === filePaths.length - 1 ? `'${typeName}';` : `'${typeName}'${typesDelimitor}`; } } await writeFile(outputDirectory, 'index', indexFileContent); @@ -53,6 +68,12 @@ export const convertToMultipleFiles = async (convertionOptions: ConvertionOption deleteFiles(generatedTypeScriptFilePaths); info(`delete Typescript files`); + if (modelOutputPath && modelFileName) { + const modelFile = (types += generateInterfaceDefinition(interfaceName, typeName)); + await writeFile(modelOutputPath, `${modelFileName}.model`, modelFile); + info(`model-file successfully generated under ${modelOutputPath}/${modelFileName}.model.ts`); + } + success('========================================================'); success(`your files were successfully created under: ${outputDirectory}`); success( diff --git a/src/lib/generators/code-snippet-generators.ts b/src/lib/generators/code-snippet-generators.ts index 94db2e6..29cc9de 100644 --- a/src/lib/generators/code-snippet-generators.ts +++ b/src/lib/generators/code-snippet-generators.ts @@ -15,7 +15,11 @@ export const generateInterfaceDefinition = (interfaceName: string, typeName: str }; export const generateTypeDefinition = (typeName: string): string => { - return `export type ${typeName} = `; + return ` + /*Do not edit this file - this file is generated by svg-to-ts*/ + + export type ${typeName} = + `; }; export const generateSvgConstant = (