Skip to content

Commit

Permalink
feat(defaultexport): use default export if no objectname is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
nivekcode committed Jun 16, 2020
1 parent 615459b commit d5c8635
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,18 @@ Available options:
| 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 |
| objectName | string | default - export | name of the exported const - if nothing is set - default export will be used |

As mentioned above, `svg-to-ts` supports different use-cases. You can either generate you library to a single TypeScript file with multiple constants, to single TypeScript file per Icon
or to allready precompiled JavaScript files.
#### Sample output

```javascript
export default {
expressionLess: '<svg xmlns="http://ww...',
full: '<svg xmlns="http://...',
laughing: '<svg xmlns="http://ww...',
smilingFace: '<svg xmlns="http://www....'
};
```

### 2. Multiple constants - Treeshakable and typesafe with one file (`convertionType==='constants'`)

Expand Down Expand Up @@ -201,7 +210,33 @@ We can now run
`svg-to-ts.ts -s ./inputfiles -o ./dist -t sampleIcon -i SampleIcon -p sampleIcon`
and we end up with the following file in our `dist` folder.

![output](https://raw.githubusercontent.com/kreuzerk/svg-to-ts/master/assets/output.png)
#### Sample ouput

```javascript
export const myIconExpressionLess: MyIcon = {
name: 'expression_less',
data: `<svg xmlns="http://...`
};
export const myIconFull: MyIcon = {
name: 'full',
data: `<svg xmlns="http://www...`
};
export const myIconLaughing: MyIcon = {
name: 'laughing',
data: `<svg xmlns="http://www.w...`
};
export const myIconSmilingFace: MyIcon = {
name: 'smiling_face',
data: `<svg xmlns="http://www.w3...`
};
/* ⚠️ Do not edit this file - this file is generated by svg-to-ts*/

export type myIcons = 'expression_less' | 'full' | 'laughing' | 'smiling_face';
export interface MyIcon {
name: myIcons;
data: string;
}
```

### 3. Fully tree shakable and optimized for lazy loading (`convertionType==='files'`)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"start-object:camel": "ts-node ./src/bin/svg-to-ts.ts --convertionType object -s 'inputfiles-hyphen/*.svg' -d CAMEL",
"start-object:multiple-source": "ts-node ./src/bin/svg-to-ts.ts --convertionType object -s './inputfiles/*.svg' -s 'inputfiles-hyphen/*.svg'",
"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-object:export-const": "ts-node ./src/bin/svg-to-ts.ts --convertionType object -s './inputfiles/*.svg' -o ./dist --objectName myIcons",
"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
7 changes: 3 additions & 4 deletions src/lib/converters/object.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ export const convertToSingleObject = async (convertionOptions: ObjectConvertionO
const svgObject = {};
const svgDefinitions = await filesProcessor(convertionOptions);
svgDefinitions.forEach((svgDefinition: SvgDefinition) => (svgObject[svgDefinition.typeName] = svgDefinition.data));
const fileContent =
objectName === 'default'
? `export default ${JSON.stringify(svgObject)}`
: `export const ${objectName} = ${JSON.stringify(svgObject)}`;
const fileContent = !objectName
? `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) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/options/args-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const setupCommander = () => {
commander
.version(packgeJSON.version)
.option('-c --convertionType <ConvertionType>', 'convertion type (object, constants, files)')
.option('--objectName <string>', 'name of the exported object', DEFAULT_OPTIONS.objectName)
.option('--objectName <string>', 'name of the exported object')
.option('-t --typeName <string>', 'name of the generated enumeration type', DEFAULT_OPTIONS.typeName)
.option('--generateType <boolean>', 'prevent generating enumeration type', DEFAULT_OPTIONS.generateType)
.option('--generateTypeObject <boolean>', 'generate type object', DEFAULT_OPTIONS.generateTypeObject)
Expand Down
9 changes: 0 additions & 9 deletions src/lib/options/config-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ const mergeWithDefaults = async (
info(`No delimiter provided, "${configOptions.delimiter}" will be used`);
}

if (options.convertionType === ConvertionType.OBJECT) {
info(`Convertiontype "Object" will be used`);

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

if (options.convertionType === ConvertionType.CONSTANTS || options.convertionType === ConvertionType.OBJECT) {
if (!(configOptions as ConstantsConvertionOptions).fileName) {
(configOptions as ConstantsConvertionOptions).fileName = DEFAULT_OPTIONS.modelFileName;
Expand Down

0 comments on commit d5c8635

Please sign in to comment.