Skip to content

Commit b44cc80

Browse files
committed
feat: 🎸 configuration path
add config option to support custom configuration path ✅ Closes: #67
1 parent ea65433 commit b44cc80

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

‎README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ you want to convert all SVG file in your current folder to TypeScript constants.
8080
### Configuration in package.json or .rc file
8181

8282
When you start using `svg-to-ts` in bigger projects, configuration may get more sophisticated. At this point command line
83-
arguments are hard to read. Therefore `svg-to-ts` allows you to configure it either over package.json or over a `.svg-to-tsrc` file.
83+
arguments are hard to read. Therefore `svg-to-ts` allows you to configure it either over `package.json` or over a `.svg-to-tsrc` file.
84+
85+
Those files can be written in `json`, `yaml`, `yml`, `js` (CommonJS module). By default `svg-to-ts` will search up
86+
the directory tree for a `svg-to-ts` propert in the `package.json`, a `.svg-to-tsrc` file. However, if you are working
87+
in a monorepo or want to have multiple configs, you can use the `--config` property to specify a path your configuration.
88+
For example `svg-to-ts --config ./myconfig.json`.
8489

8590
#### Configure svg-to-ts over package.json
8691

@@ -275,7 +280,7 @@ supports code splitting which is especially usefull in scenarios where you are u
275280
[Here's a step by step guide on how to create an icon library that is optimized for tree shaking](https://medium.com/angular-in-depth/how-to-create-a-fully-tree-shakable-icon-library-in-angular-c5488cf9cd76)
276281

277282
![fully tree shakable](https://raw.githubusercontent.com/kreuzerk/svg-to-ts/master/assets/fully-treeshakable.png)
278-
Often, having the SVGs in a single file is enough. However if you are in a more complex environment with bigger business
283+
Often, having the SVGs in a single file is enough. However, if you are in a more complex environment with bigger business
279284
applications, you may want to make the icons even more tree shakable.
280285

281286
In Angular, for example, having all icons in a single file shakes out the icons that are not used. However, icons always

‎src/bin/svg-to-ts.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ConstantsConversionOptions
88
} from '../lib/options/conversion-options';
99
import { info, printLogo } from '../lib/helpers/log-helper';
10-
import { setupCommander } from '../lib/options/args-collector';
10+
import { setupCommander } from '../lib/options/command-line-collector';
1111
import { convertToSingleObject } from '../lib/converters/object.converter';
1212
import { convertToConstants } from '../lib/converters/constants.converter';
1313
import { convertToFiles } from '../lib/converters/files.converter';

‎src/lib/options/args-collector.ts renamed to ‎src/lib/options/command-line-collector.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const setupCommander = () => {
1717
const collect = (value, previous) => previous.concat([value]);
1818
commander
1919
.version(packgeJSON.version)
20+
.option('--config <string>', 'path to the configuration file')
2021
.option('-c --conversionType <ConversionType>', 'conversion type (object, constants, files)')
2122
.option('--objectName <string>', 'name of the exported object')
2223
.option('-t --typeName <string>', 'name of the generated enumeration type', DEFAULT_OPTIONS.typeName)
@@ -79,7 +80,9 @@ const toBoolean = (str: string, defaultValue: boolean): boolean => {
7980
return result;
8081
};
8182

82-
export const collectArgumentOptions = async (): Promise<
83+
export const getConfigPath = (): string | undefined => commander.config;
84+
85+
export const collectCommandLineOptions = async (): Promise<
8386
ConstantsConversionOptions | FileConversionOptions | ObjectConversionOptions
8487
> => {
8588
if (!commander.conversionType) {

‎src/lib/options/config-collector.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import * as packgeJSON from '../../../package.json';
1212
import { error, info } from '../helpers/log-helper';
1313
import { getSvgoConfig } from '../helpers/svg-optimization';
1414
import { Delimiter } from '../generators/code-snippet-generators';
15+
import { getConfigPath } from './command-line-collector';
1516

1617
export const collectConfigurationOptions = async (): Promise<
1718
ConstantsConversionOptions | FileConversionOptions | ObjectConversionOptions | null
1819
> => {
1920
const explorerSync = cosmiconfigSync(packgeJSON.name);
20-
const cosmiConfigResult = explorerSync.search();
21+
const configPath = getConfigPath();
22+
const cosmiConfigResult = configPath ? explorerSync.load(configPath) : explorerSync.search();
2123
cosmiConfigResult ? info(`Configuration found under: ${cosmiConfigResult.filepath}`) : info('No config found');
2224
return cosmiConfigResult ? await mergeWithDefaults(cosmiConfigResult.config) : null;
2325
};

‎src/lib/options/conversion-options.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { collectConfigurationOptions } from './config-collector';
2-
import { collectArgumentOptions } from './args-collector';
2+
import { collectCommandLineOptions } from './command-line-collector';
33

44
import { info } from '../helpers/log-helper';
55
import { Delimiter } from '../generators/code-snippet-generators';
@@ -60,5 +60,5 @@ export const getOptions = async (): Promise<
6060
info(
6161
'No configuration found in package.json nor rc file - checking for arguments and applying defaults (see --help)'
6262
);
63-
return collectArgumentOptions();
63+
return collectCommandLineOptions();
6464
};

0 commit comments

Comments
 (0)