Skip to content

Commit

Permalink
feat(config): add options to not generate type and to generate type o…
Browse files Browse the repository at this point in the history
…bject
  • Loading branch information
Felipe dos Santos committed Apr 6, 2020
1 parent 6bcce87 commit 8bbec6f
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ Available configurations:
| --version | type | default | output the version number |
| ------------------------- | ----------------------- | ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| typeName | string | myIcons | name of the generated type |
| no-generateType | boolean | false | prevent generating enumeration type |
| generateTypeObject | boolean | false | generate type object |
| prefix | string | myIcon | prefix for the generated svg constants |
| interfaceName | string | MyIcon | name for the generated interface |
| fileName | stirng | my-icons | file name of the generated file |
Expand Down
7 changes: 3 additions & 4 deletions src/lib/converters/multiple-files.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ export const convertToMultipleFiles = async (convertionOptions: MultiFileConvert
info(`write index.ts`);

if (modelFileName) {
const modelFile = `${generateTypeDefinition(typeName, svgDefinitions)}${generateInterfaceDefinition(
interfaceName,
typeName
)}`;
const typeDefinition = generateTypeDefinition(convertionOptions, svgDefinitions);
const interfaceDefinition = generateInterfaceDefinition(convertionOptions);
const modelFile = `${typeDefinition}${interfaceDefinition}`;
await writeFile(`${outputDirectory}/${iconsFolderName}`, modelFileName, modelFile);
info(`model-file successfully generated under ${outputDirectory}/${iconsFolderName}/${modelFileName}.ts`);

Expand Down
8 changes: 4 additions & 4 deletions src/lib/converters/single-file.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export const convertToSingleFile = async (convertionOptions: SingleFileConvertio
try {
const svgDefinitions = await filesProcessor(convertionOptions);
if (svgDefinitions.length) {
const fileContent = `${getSvgConstants(svgDefinitions)}${generateTypeDefinition(
typeName,
svgDefinitions
)}${generateInterfaceDefinition(interfaceName, typeName)}`;
const svgContants = getSvgConstants(svgDefinitions);
const typeDefinition = generateTypeDefinition(convertionOptions, svgDefinitions);
const interfaceDefinition = generateInterfaceDefinition(convertionOptions);
const fileContent = `${svgContants}${typeDefinition}${interfaceDefinition}`;
await writeFile(outputDirectory, fileName, fileContent);
success(`Icons file successfully generated under ${underlineSuccess(outputDirectory)}`);
}
Expand Down
43 changes: 31 additions & 12 deletions src/lib/generators/code-snippet-generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,55 @@ import snakeCase from 'lodash.snakecase';
import camelCase from 'lodash.camelcase';
import kebapCase from 'lodash.kebabcase';
import { SvgDefinition } from '../converters/shared.converter';
import { MultiFileConvertionOptions, SingleFileConvertionOptions } from '../options/convertion-options';

export enum Delimiter {
CAMEL = 'CAMEL',
KEBAB = 'KEBAB',
SNAKE = 'SNAKE'
}

export const generateInterfaceDefinition = (interfaceName: string, typeName: string) => {
return `export interface ${interfaceName}{
name: ${typeName};
export const generateInterfaceDefinition = (
convertionOptions: MultiFileConvertionOptions | SingleFileConvertionOptions
) => {
const iconNameType =
convertionOptions.generateType || convertionOptions.generateTypeObject ? convertionOptions.typeName : 'string';
return `export interface ${convertionOptions.interfaceName}{
name: ${iconNameType};
data: string;}`;
};

export const generateTypeDefinition = (iconTypeName: string, svgDefinitions: SvgDefinition[]): string => {
const type = `
export const generateTypeDefinition = (
convertionOptions: MultiFileConvertionOptions | SingleFileConvertionOptions,
svgDefinitions: SvgDefinition[]
): string => {
let typesDefinition = `
/* ⚠️ Do not edit this file - this file is generated by svg-to-ts*/
export type ${iconTypeName} = ${svgDefinitions
.map(({ typeName }, index) => `'${typeName}'${index === svgDefinitions.length - 1 ? '' : ' | '}`)
.join('')};`;
const typeObject = `
export const ${iconTypeName} = {
`;

if (convertionOptions.generateType || convertionOptions.generateTypeObject) {
typesDefinition += `
export type ${convertionOptions.typeName} = ${svgDefinitions
.map(({ typeName }, index) => `'${typeName}'${index === svgDefinitions.length - 1 ? '' : ' | '}`)
.join('')};`;
}

if (convertionOptions.generateTypeObject) {
typesDefinition += `
export const ${convertionOptions.typeName} = {
${svgDefinitions
.map(
({ typeName }, index) =>
`${typeName}: '${typeName}' as ${iconTypeName}${index === svgDefinitions.length - 1 ? '' : ','}`
`${typeName}: '${typeName}' as ${convertionOptions.typeName}${
index === svgDefinitions.length - 1 ? '' : ','
}`
)
.join('')}
};`;
return `${type}${typeObject}`;
}

return typesDefinition;
};

export const generateSvgConstant = (
Expand Down
6 changes: 6 additions & 0 deletions src/lib/options/args-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const setupCommander = () => {
commander
.version(packgeJSON.version)
.option('-t --typeName <string>', 'name of the generated enumeration type', DEFAULT_OPTIONS.typeName)
.option('--no-generateType', 'prevent generating enumeration type', DEFAULT_OPTIONS.generateType)
.option('--generateTypeObject', 'generate type object', DEFAULT_OPTIONS.generateTypeObject)
.option('-f --fileName <string>', 'name of the generated file', DEFAULT_OPTIONS.fileName)
.option(
'-d --delimiter <Delimiter>',
Expand Down Expand Up @@ -61,6 +63,8 @@ export const collectArgumentOptions = async (): Promise<SingleFileConvertionOpti
outputDirectory,
prefix,
typeName,
generateType,
generateTypeObject,
modelFileName,
iconsFolderName,
optimizeForLazyLoading,
Expand Down Expand Up @@ -88,6 +92,8 @@ export const collectArgumentOptions = async (): Promise<SingleFileConvertionOpti
outputDirectory,
prefix,
typeName,
generateType,
generateTypeObject,
modelFileName,
iconsFolderName,
svgoConfig,
Expand Down
11 changes: 11 additions & 0 deletions src/lib/options/config-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ const mergeWithDefaults = async (
options: MultiFileConvertionOptions | SingleFileConvertionOptions
): Promise<MultiFileConvertionOptions | SingleFileConvertionOptions> => {
const configOptions = { ...options };

if (!configOptions.typeName) {
configOptions.typeName = DEFAULT_OPTIONS.typeName;
info(`No typeName provided, "${DEFAULT_OPTIONS.typeName}" will be used`);
}

if (configOptions.generateType != null) {
configOptions.generateType = DEFAULT_OPTIONS.generateType;
info(`No noGenerateType provided, "${DEFAULT_OPTIONS.generateType}" will be used`);
}

if (configOptions.generateTypeObject != null) {
configOptions.generateTypeObject = DEFAULT_OPTIONS.generateTypeObject;
info(`No generateTypeObject provided, "${DEFAULT_OPTIONS.generateTypeObject}" will be used`);
}

if (!configOptions.interfaceName) {
configOptions.interfaceName = DEFAULT_OPTIONS.interfaceName;
info(`No interfaceName provided, "${DEFAULT_OPTIONS.interfaceName}" will be used`);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/options/convertion-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { info } from '../helpers/log-helper';
export interface ConvertionOptions {
delimiter: Delimiter;
typeName: string;
generateType: boolean;
generateTypeObject: boolean;
prefix: string;
interfaceName: string;
srcFiles: string[];
Expand Down
2 changes: 2 additions & 0 deletions src/lib/options/default-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export const DEFAULT_OPTIONS = {
prefix: 'myIcon',
srcFiles: ['*.svg'],
typeName: 'myIcons',
generateType: true,
generateTypeObject: false,
optimizeForLazyLoading: false,
additionalModelOutputPath: null,
modelFileName: 'my-icons.model',
Expand Down

0 comments on commit 8bbec6f

Please sign in to comment.