Skip to content

Commit

Permalink
feat(constants): export all generated constants by default and add co…
Browse files Browse the repository at this point in the history
…nfiguration option

#132
  • Loading branch information
nivekcode committed Feb 4, 2022
1 parent 17ee447 commit cd43dcb
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 19 deletions.
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,21 +281,23 @@ Only the icons included in the consuming SPA also end up in the final bundle of

#### Available options:

| --version | type | default | description |
| ------------------ | -------------------------- | ---------------------------------------- | ------------------------------------------------------------------------------------- |
| typeName | string | myIcons | name of the generated type |
| generateType | boolean | false | prevent generating enumeration type |
| generateTypeObject | boolean | false | generate type object |
| generateEnum | boolean | false | generate enum object |
| prefix | string | myIcon | prefix for the generated svg constants |
| interfaceName | string | MyIcon | name for the generated interface |
| fileName | string | my-icons | file name of the generated file |
| enumName | string | MyIcons | name for the generated enum |
| delimiter | CAMEL, KEBAB, SNAKE, UPPER | SNAKE | delimiter which is used to generate the types and name properties |
| svgoConfig | string or config object | check help command - to large to display | a path to your svgoConfiguration JSON file or an inline configuration object |
| srcFiles | string | "/\*.svg" | input files matching the given filename pattern |
| outputDirectory | string | "./dist" | name of the output directory |
| verbose | boolean | false | defines if the log should contain additional information. Can be useful for debugging |
| --version | type | default | description |
| --------------------- | -------------------------- | ---------------------------------------- | ------------------------------------------------------------------------------------- |
| typeName | string | myIcons | name of the generated type |
| generateType | boolean | false | prevent generating enumeration type |
| generateTypeObject | boolean | false | generate type object |
| generateEnum | boolean | false | generate enum object |
| prefix | string | myIcon | prefix for the generated svg constants |
| interfaceName | string | MyIcon | name for the generated interface |
| fileName | string | my-icons | file name of the generated file |
| enumName | string | MyIcons | name for the generated enum |
| delimiter | CAMEL, KEBAB, SNAKE, UPPER | SNAKE | delimiter which is used to generate the types and name properties |
| svgoConfig | string or config object | check help command - to large to display | a path to your svgoConfiguration JSON file or an inline configuration object |
| srcFiles | string | "/\*.svg" | input files matching the given filename pattern |
| outputDirectory | string | "./dist" | name of the output directory |
| exportCompleteIconSet | boolean | true | exports a complete icon set |
| complteIconSetName | string | completeIconSet | Default name of the exported variable |
| verbose | boolean | false | defines if the log should contain additional information. Can be useful for debugging |

#### Example usage

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"start-constants:upper": "ts-node ./src/bin/svg-to-ts-constants.ts -s './inputfiles/*.svg' -d UPPER",
"start-constants:multiple-source": "ts-node ./src/bin/svg-to-ts-constants.ts -s './inputfiles/*.svg' -s 'inputfiles-hyphen/*.svg'",
"start-constants:custom": "ts-node ./src/bin/svg-to-ts-constants.ts -s './inputfiles/*.svg' -o ./dist -t sampleIcon -i SampleIcon -p sampleIcon -f icons",
"start-constants:completeIconSet": "ts-node ./src/bin/svg-to-ts-constants.ts -s './inputfiles/*.svg' --exportCompleteIconSet true",
"start-constants:completeIconSetName": "ts-node ./src/bin/svg-to-ts-constants.ts -s './inputfiles/*.svg' --exportCompleteIconSet true --completeIconSetName myCompleteSet",
"start-files": "ts-node ./src/bin/svg-to-ts-files.ts -s './inputfiles/*.svg'",
"start-files:help": "ts-node ./src/bin/svg-to-ts-files.ts --help",
"start-files:invalid-path": "ts-node ./src/bin/svg-to-ts-files.ts -s './doesntExist/*.svg'",
Expand Down
16 changes: 14 additions & 2 deletions src/lib/converters/constants.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
generateTypeDefinition,
generateTypeHelper
} from '../generators/code-snippet-generators';
import { generateExportSection } from '../helpers/complete-icon-set.helper';
import { writeFile } from '../helpers/file-helpers';
import { Logger } from '../helpers/logger';
import { callAndMonitor, callAndMonitorAsync } from '../helpers/monitor';
Expand All @@ -19,11 +20,14 @@ const getSvgConstants = (svgDefinitions): string => {
};

export const convertToConstants = async (conversionOptions: ConstantsConversionOptions): Promise<void> => {
const { outputDirectory, fileName, interfaceName } = conversionOptions;
const { outputDirectory, fileName, interfaceName, exportCompleteIconSet, completeIconSetName } = conversionOptions;
let exportAllStatement = '';

const svgDefinitions = await callAndMonitorAsync<SvgDefinition[]>(
filesProcessor.bind({}, conversionOptions),
'Processing SVG files'
);

if (svgDefinitions.length) {
const svgContants = callAndMonitor<string>(getSvgConstants.bind({}, svgDefinitions), 'Generate SVG constants');
const typeDefinition = callAndMonitor<string>(
Expand All @@ -34,8 +38,16 @@ export const convertToConstants = async (conversionOptions: ConstantsConversionO
generateInterfaceDefinition.bind({}, conversionOptions),
'Generate Interface Definition'
);

if (exportCompleteIconSet) {
exportAllStatement = callAndMonitor<string>(
generateExportSection.bind({}, svgDefinitions, completeIconSetName),
'Exporting all constants'
);
}

const typeHelper = callAndMonitor<string>(generateTypeHelper.bind({}, interfaceName), 'Generate Type Helper');
const fileContent = `${svgContants}${typeDefinition}${interfaceDefinition}${typeHelper}`;
const fileContent = `${svgContants}${typeDefinition}${interfaceDefinition}${typeHelper}${exportAllStatement}`;
await callAndMonitorAsync<void>(
writeFile.bind({}, outputDirectory, fileName, fileContent),
`Writing files to ${outputDirectory}`
Expand Down
2 changes: 1 addition & 1 deletion src/lib/helpers/complete-icon-set.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const generateImportSection = (svgDefinitions: SvgDefinition[]): string =>
return acc;
}, '');

const generateExportSection = (svgDefinitions: SvgDefinition[], completeIconSetName: string): string =>
export const generateExportSection = (svgDefinitions: SvgDefinition[], completeIconSetName: string): string =>
svgDefinitions.reduce((acc: string, svgDefinition: SvgDefinition, index: number) => {
if (index === svgDefinitions.length - 1) {
acc += `${svgDefinition.variableName}];`;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/options/commander/constant-options.commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export const setupConstantOptionsCommander = () => {
'Specifies if the complete icon set should be exported or not',
DEFAULT_CONST_CONVERSION_OPTIONS.exportCompleteIconSet
)
.option(
'--completeIconSetName <string>',
'The name of the complete icon set (only effective if exportCompleteIconSet is set to true)',
DEFAULT_CONST_CONVERSION_OPTIONS.completeIconSetName
)
.option(
'--verbose <boolean>',
'Specifies if a verbose log message should be printed or not',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface ConstantsConversionOptions {
generateTypeObject?: boolean;
generateEnum?: boolean;
exportCompleteIconSet?: boolean;
completeIconSetName: string;
prefix?: string;
interfaceName?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const DEFAULT_CONST_CONVERSION_OPTIONS: ConstantsConversionOptions = {
generateTypeObject: false,
generateEnum: false,
interfaceName: 'MyIcon',
exportCompleteIconSet: false,
exportCompleteIconSet: true,
completeIconSetName: 'completeIconSet',
verbose: false,
delimiter: Delimiter.SNAKE
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ export const collectCommandLineConstantOptions = async (): Promise<ConstantsConv
generateType,
generateTypeObject,
generateEnum,
exportCompleteIconSet,
completeIconSetName,
verbose
} = commander;
let svgoConfig = commander.svgoConfig;
generateType = toBoolean(generateType, DEFAULT_CONST_CONVERSION_OPTIONS.generateType);
generateTypeObject = toBoolean(generateTypeObject, DEFAULT_CONST_CONVERSION_OPTIONS.generateTypeObject);
generateEnum = toBoolean(generateEnum, DEFAULT_CONST_CONVERSION_OPTIONS.generateTypeObject);
verbose = toBoolean(verbose, DEFAULT_CONST_CONVERSION_OPTIONS.verbose);
exportCompleteIconSet = toBoolean(exportCompleteIconSet, DEFAULT_CONST_CONVERSION_OPTIONS.exportCompleteIconSet);

// Because of commander adding default value to params
// See: https://stackoverflow.com/questions/30238654/commander-js-collect-multiple-options-always-include-default
Expand All @@ -46,6 +49,8 @@ export const collectCommandLineConstantOptions = async (): Promise<ConstantsConv
generateType,
generateTypeObject,
generateEnum,
exportCompleteIconSet,
completeIconSetName,
svgoConfig,
verbose
};
Expand Down

0 comments on commit cd43dcb

Please sign in to comment.