From 9f03426a0fbd8ea39e109c2d73f38d75c9d72620 Mon Sep 17 00:00:00 2001 From: kreuzerk Date: Wed, 11 Mar 2020 09:14:41 +0100 Subject: [PATCH] fix(modelfile): remove output path --- package.json | 2 +- src/bin/svg-to-ts.ts | 5 ----- src/lib/converters/multiple-files.converter.ts | 18 ++++++++++++------ src/lib/generators/code-snippet-generators.ts | 9 +++++++-- src/lib/options/args-collector.ts | 2 -- src/lib/options/config-collector.ts | 5 ----- src/lib/options/convertion-options.ts | 1 - 7 files changed, 20 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 6d7e77a..ccb5089 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,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:customModel": "ts-node ./src/bin/svg-to-ts.ts --optimizeForLazyLoading true -s './inputfilesRegex/**/*.svg' --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 881d12e..0dfed6d 100755 --- a/src/bin/svg-to-ts.ts +++ b/src/bin/svg-to-ts.ts @@ -29,11 +29,6 @@ commander 'optimize the output for lazyloading', DEFAULT_OPTIONS.optimizeForLazyLoading ) - .option( - '--modelOutputPath ', - 'Output path for the types file (only necessary when optimizeForLazyLoading option is enabled)', - DEFAULT_OPTIONS.modelOutputPath - ) .option( '--modelFileName ', 'FileName of the model file (only necessary when optimizeForLazyLoading option is enabled)', diff --git a/src/lib/converters/multiple-files.converter.ts b/src/lib/converters/multiple-files.converter.ts index 655231e..f228091 100644 --- a/src/lib/converters/multiple-files.converter.ts +++ b/src/lib/converters/multiple-files.converter.ts @@ -3,9 +3,9 @@ import * as path from 'path'; import { generateExportStatement, generateInterfaceDefinition, + generateSvgConstantWithImport, generateTypeDefinition, generateTypeName, - generateUntypedSvgConstant, generateVariableName } from '../generators/code-snippet-generators'; import { getFilePathsFromRegex } from '../helpers/regex-helpers'; @@ -24,7 +24,6 @@ export const convertToMultipleFiles = async (convertionOptions: MultiFileConvert delimiter, outputDirectory, srcFiles, - modelOutputPath, modelFileName, iconsFolderName } = convertionOptions; @@ -47,7 +46,13 @@ export const convertToMultipleFiles = async (convertionOptions: MultiFileConvert const optimizedSvg = await svgOptimizer.optimize(rawSvg); const variableName = generateVariableName(prefix, filenameWithoutEnding); const typeName = generateTypeName(filenameWithoutEnding, delimiter); - const svgConstant = generateUntypedSvgConstant(variableName, typeName, optimizedSvg.data); + const svgConstant = generateSvgConstantWithImport( + variableName, + typeName, + interfaceName, + modelFileName, + optimizedSvg.data + ); const generatedFileName = `${prefix}-${filenameWithoutEnding}.icon`; indexFileContent += generateExportStatement(generatedFileName, iconsFolderName); await writeFile(`${outputDirectory}/${iconsFolderName}`, generatedFileName, svgConstant); @@ -57,13 +62,14 @@ export const convertToMultipleFiles = async (convertionOptions: MultiFileConvert } } separatorEnd(); + indexFileContent += generateExportStatement(modelFileName, iconsFolderName); await writeFile(outputDirectory, 'index', indexFileContent); info(`write index.ts`); - if (modelOutputPath && modelFileName) { + if (modelFileName) { const modelFile = (types += generateInterfaceDefinition(interfaceName, typeName)); - await writeFile(modelOutputPath, modelFileName, modelFile); - info(`model-file successfully generated under ${modelOutputPath}/${modelFileName}.model.ts`); + await writeFile(`${outputDirectory}/${iconsFolderName}`, modelFileName, modelFile); + info(`model-file successfully generated under ${outputDirectory}/${iconsFolderName}/${modelFileName}.ts`); } success('========================================================'); diff --git a/src/lib/generators/code-snippet-generators.ts b/src/lib/generators/code-snippet-generators.ts index 775f91a..12fbb7c 100644 --- a/src/lib/generators/code-snippet-generators.ts +++ b/src/lib/generators/code-snippet-generators.ts @@ -34,12 +34,17 @@ export const generateSvgConstant = ( };`; }; -export const generateUntypedSvgConstant = ( +export const generateSvgConstantWithImport = ( variableName: string, filenameWithoutEnding: string, + interfaceName: string, + modelFileName: string, data: string ): string => { - return `export const ${variableName}: {name: any, data: string} = { + return ` + import {${interfaceName}} from './${modelFileName}'; + + export const ${variableName}: ${interfaceName} = { name: '${filenameWithoutEnding}', data: '${data}' };`; diff --git a/src/lib/options/args-collector.ts b/src/lib/options/args-collector.ts index 77101d7..1274f4d 100644 --- a/src/lib/options/args-collector.ts +++ b/src/lib/options/args-collector.ts @@ -11,7 +11,6 @@ export const collectArgumentOptions = (): SingleFileConvertionOptions | MultiFil prefix, typeName, modelFileName, - modelOutputPath, iconsFolderName, optimizeForLazyLoading } = commander; @@ -32,7 +31,6 @@ export const collectArgumentOptions = (): SingleFileConvertionOptions | MultiFil prefix, typeName, modelFileName, - modelOutputPath, iconsFolderName, optimizeForLazyLoading }; diff --git a/src/lib/options/config-collector.ts b/src/lib/options/config-collector.ts index e49a45f..ecd22ca 100644 --- a/src/lib/options/config-collector.ts +++ b/src/lib/options/config-collector.ts @@ -48,11 +48,6 @@ const mergeWithDefaults = ( } if (configOptions.optimizeForLazyLoading) { - if (!(configOptions as MultiFileConvertionOptions).modelOutputPath) { - (configOptions as MultiFileConvertionOptions).modelOutputPath = DEFAULT_OPTIONS.modelOutputPath; - info(`No modelOutputPath provided, "${DEFAULT_OPTIONS.modelOutputPath}" will be used`); - } - if (!(configOptions as MultiFileConvertionOptions).modelFileName) { (configOptions as MultiFileConvertionOptions).modelFileName = DEFAULT_OPTIONS.modelFileName; info(`No modelFileName provided, "${DEFAULT_OPTIONS.modelFileName}" will be used`); diff --git a/src/lib/options/convertion-options.ts b/src/lib/options/convertion-options.ts index 86bf3f3..d576f38 100644 --- a/src/lib/options/convertion-options.ts +++ b/src/lib/options/convertion-options.ts @@ -19,7 +19,6 @@ export interface SingleFileConvertionOptions extends ConvertionOptions { export interface MultiFileConvertionOptions extends ConvertionOptions { modelFileName: string; - modelOutputPath: string; iconsFolderName: string; }