Skip to content

Commit

Permalink
feat: 🎸 handle multiple configurations
Browse files Browse the repository at this point in the history
handle array configurations defined in package.json or external config
object

✅ Closes: 56
  • Loading branch information
nivekcode committed Oct 6, 2020
1 parent 4ea8af2 commit bc8befc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
22 changes: 17 additions & 5 deletions src/bin/svg-to-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import { convertToSingleObject } from '../lib/converters/object.converter';
import { convertToConstants } from '../lib/converters/constants.converter';
import { convertToFiles } from '../lib/converters/files.converter';

(async () => {
setupCommander();
printLogo();
const conversionOptions = await getOptions();

const convert = async (
conversionOptions: FileConversionOptions | ConstantsConversionOptions | ObjectConversionOptions
) => {
if (conversionOptions.conversionType === ConversionType.FILES) {
info('We are using the conversiontype "files"');
await convertToFiles(conversionOptions as FileConversionOptions);
Expand All @@ -31,4 +29,18 @@ import { convertToFiles } from '../lib/converters/files.converter';
info('We are using the conversion type "object"');
await convertToSingleObject(conversionOptions as ObjectConversionOptions);
}
};

(async () => {
setupCommander();
printLogo();
const conversionOptions = await getOptions();

if (Array.isArray(conversionOptions)) {
for (const c of conversionOptions) {
await convert(c);
}
} else {
await convert(conversionOptions);
}
})();
20 changes: 18 additions & 2 deletions src/lib/options/config-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,29 @@ import { Delimiter } from '../generators/code-snippet-generators';
import { getConfigPath } from './command-line-collector';

export const collectConfigurationOptions = async (): Promise<
ConstantsConversionOptions | FileConversionOptions | ObjectConversionOptions | null
| ConstantsConversionOptions
| FileConversionOptions
| ObjectConversionOptions
| Array<ConstantsConversionOptions | FileConversionOptions | ObjectConversionOptions>
| null
> => {
const explorerSync = cosmiconfigSync(packgeJSON.name);
const configPath = getConfigPath();
const cosmiConfigResult = configPath ? explorerSync.load(configPath) : explorerSync.search();
cosmiConfigResult ? info(`Configuration found under: ${cosmiConfigResult.filepath}`) : info('No config found');
return cosmiConfigResult ? await mergeWithDefaults(cosmiConfigResult.config) : null;

if (!cosmiConfigResult) {
return null;
}
if (Array.isArray(cosmiConfigResult.config)) {
return Promise.all(
cosmiConfigResult.config.map(
(config: Partial<FileConversionOptions | ConstantsConversionOptions | ObjectConversionOptions>) =>
mergeWithDefaults(config)
)
);
}
return await mergeWithDefaults(cosmiConfigResult.config);
};

const mergeWithDefaults = async (
Expand Down
5 changes: 4 additions & 1 deletion src/lib/options/conversion-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export interface FileConversionOptions extends CommonConversionOptions {
}

export const getOptions = async (): Promise<
FileConversionOptions | ConstantsConversionOptions | ObjectConversionOptions
| FileConversionOptions
| ConstantsConversionOptions
| ObjectConversionOptions
| Array<ConstantsConversionOptions | FileConversionOptions | ObjectConversionOptions>
> => {
const configOptions = await collectConfigurationOptions();

Expand Down

0 comments on commit bc8befc

Please sign in to comment.