Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding custom string replacer for flex replace #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/tools/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
Expand All @@ -50,6 +65,7 @@ const items = [
folderPath: './tools/templates/react/redux-store/',
},
stringReplacers: ['__store__', '__model__'],

output: {
path: './src/stores/__store__(kebabCase)',
pathAndFileNameDefaultCase: '(pascalCase)',
Expand Down Expand Up @@ -77,6 +93,7 @@ const items = [
entry: {
folderPath: './tools/templates/react/connected-component/',
},

stringReplacers: ['__name__'],
output: {
path: './src/views/__name__(kebabCase)',
Expand Down
23 changes: 22 additions & 1 deletion src/GenerateTemplateFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -190,6 +191,8 @@ export default class GenerateTemplateFiles {

return isValid || 'You must provide an answer.';
},
//@ts-ignore
...(item.result ? { result: item.result } : {}),
};
});

Expand All @@ -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;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/models/IObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default interface IObjectParamReplacer {
[key: string]: string;
}
4 changes: 4 additions & 0 deletions src/models/IReplacer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import IObjectParamReplacer from './IObject';

export default interface IReplacer {
readonly slot: string;
readonly slotValue: string;

readonly newSlot?: (params: IObjectParamReplacer) => string;
}
1 change: 1 addition & 0 deletions src/models/IReplacerSlotQuestion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default interface IReplacerSlotQuestion {
readonly question: string;
readonly slot: string;
readonly result?: (value: string) => string;
}