From 275f6ce989e733d1942740a6ccf12f2b95043b83 Mon Sep 17 00:00:00 2001 From: nhan2804 Date: Sun, 28 May 2023 02:33:47 +0700 Subject: [PATCH] update: custom string replacer result and pass rest replacers to dynamic replacer --- examples/tools/items.js | 17 +++++++++++++++++ src/GenerateTemplateFiles.ts | 23 ++++++++++++++++++++++- src/models/IObject.ts | 3 +++ src/models/IReplacer.ts | 4 ++++ src/models/IReplacerSlotQuestion.ts | 1 + 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/models/IObject.ts diff --git a/examples/tools/items.js b/examples/tools/items.js index 88bb3e7..88f4122 100644 --- a/examples/tools/items.js +++ b/examples/tools/items.js @@ -33,6 +33,21 @@ const items = [ folderPath: './tools/templates/vue/vuex-store/', }, stringReplacers: ['__store__', '__model__'], + dynamicReplacers: [ + { + slot:"__store1__custom__", + newSlot:({__store__})=>{ + return `${__store__} from other string replacer` + } + }, + { + slot:"__store2_from_other_custom1", + newSlot:({__store1__custom__})=>{ + return __store1__custom__+ " and custom dynamic replacer" + } + } + + ], output: { path: './src/stores/__store__(kebabCase)', pathAndFileNameDefaultCase: '(pascalCase)', @@ -50,6 +65,7 @@ const items = [ folderPath: './tools/templates/react/redux-store/', }, stringReplacers: ['__store__', '__model__'], + output: { path: './src/stores/__store__(kebabCase)', pathAndFileNameDefaultCase: '(pascalCase)', @@ -77,6 +93,7 @@ const items = [ entry: { folderPath: './tools/templates/react/connected-component/', }, + stringReplacers: ['__name__'], output: { path: './src/views/__name__(kebabCase)', diff --git a/src/GenerateTemplateFiles.ts b/src/GenerateTemplateFiles.ts index 40315de..3837d12 100644 --- a/src/GenerateTemplateFiles.ts +++ b/src/GenerateTemplateFiles.ts @@ -21,6 +21,7 @@ import { } from './utilities/CheckUtility'; import IReplacerSlotQuestion from './models/IReplacerSlotQuestion'; import yargs from 'yargs'; +import IObjectParamReplacer from './models/IObject'; export default class GenerateTemplateFiles { private _isCommandLine: boolean = false; @@ -190,6 +191,8 @@ export default class GenerateTemplateFiles { return isValid || 'You must provide an answer.'; }, + //@ts-ignore + ...(item.result ? { result: item.result } : {}), }; }); @@ -203,9 +206,27 @@ export default class GenerateTemplateFiles { }; } ); + const dynamicReplacers = await this._getDynamicReplacerSlotValues(selectedConfigItem); + const mergedReplacer = [...replacers, ...dynamicReplacers]; + + const replacersToParams: IObjectParamReplacer = mergedReplacer.reduce((obj, replacer) => { + obj[replacer.slot] = replacer.slotValue; + return obj; + }, {}); + const finalReplacer = mergedReplacer.map((e) => { + // get new slotValue + const newSlotValue = e.newSlot ? e.newSlot(replacersToParams) : e?.slotValue; + + // override and add assign new dynamic slot + replacersToParams[e.slot] = newSlotValue; - return [...replacers, ...dynamicReplacers]; + return { + ...e, + slotValue: newSlotValue, + }; + }); + return finalReplacer; } /** diff --git a/src/models/IObject.ts b/src/models/IObject.ts new file mode 100644 index 0000000..6a0b408 --- /dev/null +++ b/src/models/IObject.ts @@ -0,0 +1,3 @@ +export default interface IObjectParamReplacer { + [key: string]: string; +} diff --git a/src/models/IReplacer.ts b/src/models/IReplacer.ts index c3875b2..1d49edd 100644 --- a/src/models/IReplacer.ts +++ b/src/models/IReplacer.ts @@ -1,4 +1,8 @@ +import IObjectParamReplacer from './IObject'; + export default interface IReplacer { readonly slot: string; readonly slotValue: string; + + readonly newSlot?: (params: IObjectParamReplacer) => string; } diff --git a/src/models/IReplacerSlotQuestion.ts b/src/models/IReplacerSlotQuestion.ts index 8ef65b8..ba1131a 100644 --- a/src/models/IReplacerSlotQuestion.ts +++ b/src/models/IReplacerSlotQuestion.ts @@ -1,4 +1,5 @@ export default interface IReplacerSlotQuestion { readonly question: string; readonly slot: string; + readonly result?: (value: string) => string; }