Skip to content

Commit

Permalink
feat(model): generate model to a different path
Browse files Browse the repository at this point in the history
  • Loading branch information
nivekcode committed Mar 5, 2020
1 parent 1e0fc26 commit c10d322
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
19 changes: 17 additions & 2 deletions src/bin/svg-to-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface ConvertionOptions {
interfaceName: string;
srcFiles: string[];
outputDirectory: string;
modelOutputPath: string;
modelFileName: string;
}

const DEFAULTS = {
Expand Down Expand Up @@ -46,9 +48,20 @@ commander
.option('-s --srcFiles <value>', 'name of the source directory', collect, [])
.option('-o --outputDirectory <string>', 'name of the output directory', DEFAULTS.outputDirectory)
.option('--optimizeForLazyLoading <boolean>', 'optimize the output for lazyloading', DEFAULTS.optimizeForLazyLoading)
.option('--modelOutputPath <string>', 'Output path for the types file')
.option('--modelFileName <string>', '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
Expand All @@ -65,7 +78,9 @@ const convertionOptions = {
prefix,
interfaceName,
srcFiles,
outputDirectory
outputDirectory,
modelOutputPath,
modelFileName
};

if (optimizeForLazyLoading) {
Expand Down
23 changes: 22 additions & 1 deletion src/lib/converters/multiple-files.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { ConvertionOptions } from '../../bin/svg-to-ts';

import {
generateExportStatement,
generateInterfaceDefinition,
generateTypeDefinition,
generateTypeName,
generateUntypedSvgConstant,
generateVariableName
Expand All @@ -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<void> => {
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);
Expand All @@ -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);
Expand All @@ -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(
Expand Down
6 changes: 5 additions & 1 deletion src/lib/generators/code-snippet-generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

0 comments on commit c10d322

Please sign in to comment.