Skip to content

Commit

Permalink
feat(export): add possibility for default exports
Browse files Browse the repository at this point in the history
  • Loading branch information
nivekcode committed Jun 16, 2020
1 parent b0b307a commit 4246587
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Available options:
| --version | type | default | output the version number |
| --------------- | ----------------------- | ---------------------------------------- | ---------------------------------------------------------------------------- |
| fileName | stirng | my-icons | file name of the generated file |
| delimiter | CAMEL, KEBAP, SNAKE | SNAKE | delimiter which is used to generate the types and name properties |
| delimiter | CAMEL, KEBAP, SNAKE | CAMEL | 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 |
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"start-object:snake": "ts-node ./src/bin/svg-to-ts.ts --convertionType object -s './inputfilesBis/*.svg' -d SNAKE",
"start-object:camel": "ts-node ./src/bin/svg-to-ts.ts --convertionType object -s './inputfilesBis/*.svg' -d CAMEL",
"start-object:multiple-source": "ts-node ./src/bin/svg-to-ts.ts --convertionType object -s './inputfiles/*.svg' -s './inputfilesBis/*.svg'",
"start-object:custom": "ts-node ./src/bin/svg-to-ts.ts --convertionType object -s './inputfiles/*.svg' -o ./dist --objectName awesomeIcons -f icons -d CAMEL",
"start-object:custom": "ts-node ./src/bin/svg-to-ts.ts --convertionType object -s './inputfiles/*.svg' -o ./dist --objectName awesomeIcons -f icons",
"start-object:default-export": "ts-node ./src/bin/svg-to-ts.ts --convertionType object -s './inputfiles/*.svg' -o ./dist --objectName default",
"start-constants": "ts-node ./src/bin/svg-to-ts.ts --convertionType constants -s './inputfiles/*.svg'",
"start-constants:svgconfig": "ts-node ./src/bin/svg-to-ts.ts --convertionType constants -s './inputfiles/*.svg' --svgoConfig ./svgo-test.config.json",
"start-constants:regex": "ts-node ./src/bin/svg-to-ts.ts --convertionType constants -s './inputfilesRegex/**/*.svg'",
Expand Down
6 changes: 5 additions & 1 deletion src/lib/converters/object.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export const convertToSingleObject = async (convertionOptions: ObjectConvertionO
const svgObject = {};
const svgDefinitions = await filesProcessor(convertionOptions);
svgDefinitions.forEach((svgDefinition: SvgDefinition) => (svgObject[svgDefinition.typeName] = svgDefinition.data));
await writeFile(outputDirectory, fileName, `export const ${objectName} = ${JSON.stringify(svgObject)}`);
const fileContent =
objectName === 'default'
? `export default ${JSON.stringify(svgObject)}`
: `export const ${objectName} = ${JSON.stringify(svgObject)}`;
await writeFile(outputDirectory, fileName, fileContent);
success(`Icons file successfully generated under ${underlineSuccess(outputDirectory)}`);
} catch (exception) {
error(`Something went wrong: ${exception}`);
Expand Down
11 changes: 6 additions & 5 deletions src/lib/options/args-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { Delimiter } from '../generators/code-snippet-generators';
import { getSvgoConfig } from '../helpers/svg-optimization';

import {
FileConvertionOptions,
ConstantsConvertionOptions,
ObjectConvertionOptions,
ConvertionType
ConvertionType,
FileConvertionOptions,
ObjectConvertionOptions
} from './convertion-options';
import { DEFAULT_OPTIONS } from './default-options';
import { error } from '../helpers/log-helper';
Expand All @@ -25,8 +25,7 @@ export const setupCommander = () => {
.option('-f --fileName <string>', 'name of the generated file', DEFAULT_OPTIONS.fileName)
.option(
'-d --delimiter <Delimiter>',
`delimiter which is used to generate the types and name properties (${Object.values(Delimiter).join(',')})`,
DEFAULT_OPTIONS.delimiter
`delimiter which is used to generate the types and name properties (${Object.values(Delimiter).join(',')})`
)
.option('-p --prefix <string>', 'prefix for the generated svg constants', DEFAULT_OPTIONS.prefix)
.option('-i --interfaceName <string>', 'name for the generated interface', DEFAULT_OPTIONS.interfaceName)
Expand Down Expand Up @@ -106,6 +105,8 @@ export const collectArgumentOptions = async (): Promise<
generateTypeObject = toBoolean(generateTypeObject, DEFAULT_OPTIONS.generateTypeObject);
compileSources = toBoolean(compileSources, DEFAULT_OPTIONS.compileSources);

delimiter = convertionType === ConvertionType.OBJECT ? Delimiter.CAMEL : Delimiter.SNAKE;

// 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
5 changes: 3 additions & 2 deletions src/lib/options/config-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DEFAULT_OPTIONS } from './default-options';
import * as packgeJSON from '../../../package.json';
import { error, info } from '../helpers/log-helper';
import { getSvgoConfig } from '../helpers/svg-optimization';
import { Delimiter } from '../generators/code-snippet-generators';

export const collectConfigurationOptions = async (): Promise<
ConstantsConvertionOptions | FileConvertionOptions | ObjectConvertionOptions | null
Expand Down Expand Up @@ -50,8 +51,8 @@ const mergeWithDefaults = async (
}

if (!configOptions.delimiter) {
configOptions.delimiter = DEFAULT_OPTIONS.delimiter;
info(`No delimiter provided, "${DEFAULT_OPTIONS.delimiter}" will be used`);
configOptions.delimiter = options.convertionType === ConvertionType.OBJECT ? Delimiter.CAMEL : Delimiter.SNAKE;
info(`No delimiter provided, "${configOptions.delimiter}" will be used`);
}

if (options.convertionType === ConvertionType.OBJECT) {
Expand Down
3 changes: 0 additions & 3 deletions src/lib/options/default-options.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Delimiter } from '../generators/code-snippet-generators';

export const DEFAULT_OPTIONS = {
fileName: 'my-icons',
delimiter: Delimiter.SNAKE,
interfaceName: 'MyIcon',
objectName: 'icons',
outputDirectory: './dist',
Expand Down

0 comments on commit 4246587

Please sign in to comment.