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 11, 2020
1 parent 12d8916 commit 9e120b3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ 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 |
| 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 |
Expand Down
6 changes: 3 additions & 3 deletions src/lib/generators/code-snippet-generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const generateInterfaceDefinition = (
const iconNameType =
convertionOptions.generateType || convertionOptions.generateTypeObject ? convertionOptions.typeName : 'string';
return `export interface ${convertionOptions.interfaceName}{
name: ${iconNameType};
name: ${convertionOptions.generateType ? iconNameType : 'string'};
data: string;}`;
};

Expand All @@ -29,7 +29,7 @@ export const generateTypeDefinition = (
`;

if (convertionOptions.generateType || convertionOptions.generateTypeObject) {
if (convertionOptions.generateType) {
typesDefinition += `
export type ${convertionOptions.typeName} = ${svgDefinitions
.map(({ typeName }, index) => `'${typeName}'${index === svgDefinitions.length - 1 ? '' : ' | '}`)
Expand All @@ -42,7 +42,7 @@ export const generateTypeDefinition = (
${svgDefinitions
.map(
({ typeName }, index) =>
`${typeName}: '${typeName}' as ${convertionOptions.typeName}${
`${typeName}: '${typeName}'${convertionOptions.generateType ? ` as ${convertionOptions.typeName}` : ''}${
index === svgDefinitions.length - 1 ? '' : ','
}`
)
Expand Down
26 changes: 23 additions & 3 deletions src/lib/options/args-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +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('--generateType <boolean>', 'prevent generating enumeration type', DEFAULT_OPTIONS.generateType)
.option('--generateTypeObject <boolean>', '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 @@ -55,8 +55,22 @@ export const setupCommander = () => {
.parse(process.argv);
};

const toBoolean = (str: string, defaultValue: boolean): boolean => {
let result = defaultValue;
switch (str) {
case 'false':
result = false;
break;
case '':
case 'true':
result = true;
break;
}
return result;
};

export const collectArgumentOptions = async (): Promise<SingleFileConvertionOptions | MultiFileConvertionOptions> => {
const {
let {
delimiter,
fileName,
interfaceName,
Expand All @@ -73,6 +87,12 @@ export const collectArgumentOptions = async (): Promise<SingleFileConvertionOpti
} = commander;
let svgoConfig = commander.svgoConfig;

// Parse boolean values
generateType = toBoolean(generateType, DEFAULT_OPTIONS.generateType);
generateTypeObject = toBoolean(generateTypeObject, DEFAULT_OPTIONS.generateTypeObject);
optimizeForLazyLoading = toBoolean(optimizeForLazyLoading, DEFAULT_OPTIONS.optimizeForLazyLoading);
compileSources = toBoolean(compileSources, DEFAULT_OPTIONS.compileSources);

// Because of commander adding default value to params
// See: https://stackoverflow.com/questions/30238654/commander-js-collect-multiple-options-always-include-default
let srcFiles = commander.srcFiles;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/options/config-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ const mergeWithDefaults = async (
info(`No typeName provided, "${DEFAULT_OPTIONS.typeName}" will be used`);
}

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

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

0 comments on commit 9e120b3

Please sign in to comment.