From bc8befc97090bc287f707dfb015bb2c9db163bb0 Mon Sep 17 00:00:00 2001 From: kreuzerk Date: Tue, 6 Oct 2020 18:59:17 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20handle=20multiple=20conf?= =?UTF-8?q?igurations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit handle array configurations defined in package.json or external config object ✅ Closes: 56 --- src/bin/svg-to-ts.ts | 22 +++++++++++++++++----- src/lib/options/config-collector.ts | 20 ++++++++++++++++++-- src/lib/options/conversion-options.ts | 5 ++++- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/bin/svg-to-ts.ts b/src/bin/svg-to-ts.ts index 0612ab4..2fa215d 100755 --- a/src/bin/svg-to-ts.ts +++ b/src/bin/svg-to-ts.ts @@ -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); @@ -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); + } })(); diff --git a/src/lib/options/config-collector.ts b/src/lib/options/config-collector.ts index 73cf730..ece315c 100644 --- a/src/lib/options/config-collector.ts +++ b/src/lib/options/config-collector.ts @@ -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 + | 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) => + mergeWithDefaults(config) + ) + ); + } + return await mergeWithDefaults(cosmiConfigResult.config); }; const mergeWithDefaults = async ( diff --git a/src/lib/options/conversion-options.ts b/src/lib/options/conversion-options.ts index cc2824f..7c575d2 100644 --- a/src/lib/options/conversion-options.ts +++ b/src/lib/options/conversion-options.ts @@ -50,7 +50,10 @@ export interface FileConversionOptions extends CommonConversionOptions { } export const getOptions = async (): Promise< - FileConversionOptions | ConstantsConversionOptions | ObjectConversionOptions + | FileConversionOptions + | ConstantsConversionOptions + | ObjectConversionOptions + | Array > => { const configOptions = await collectConfigurationOptions();