Skip to content

Perform replacement in one pass of the output #67

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
32 changes: 32 additions & 0 deletions examples/tools/batchGenerate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,38 @@ generateTemplateFilesBatch([
dynamicReplacers: [
{ slot: '__name__', slotValue: componentName },
{ slot: '__scope__', slotValue: componentScope },
{ slot: '__1scope__', slotValue: componentScope },
{ slot: '__s2cope__', slotValue: componentScope },
{ slot: '__sc2ope__', slotValue: componentScope },
{ slot: '__sco2pe__', slotValue: componentScope },
{ slot: '__scop2e__', slotValue: componentScope },
{ slot: '__scope2__', slotValue: componentScope },
{ slot: '__3scope__', slotValue: componentScope },
{ slot: '__s3cope__', slotValue: componentScope },
{ slot: '__sc3ope__', slotValue: componentScope },
{ slot: '__sco3pe__', slotValue: componentScope },
{ slot: '__scop3e__', slotValue: componentScope },
{ slot: '__4scope__', slotValue: componentScope },
{ slot: '__s4cope__', slotValue: componentScope },
{ slot: '__sc4ope__', slotValue: componentScope },
{ slot: '__sco44pe__', slotValue: componentScope },
{ slot: '__scope4__', slotValue: componentScope },
{ slot: '__s5cope__', slotValue: componentScope },
{ slot: '__s52cope__', slotValue: componentScope },
{ slot: '__sc5ope__', slotValue: componentScope },
{ slot: '__sco25pe__', slotValue: componentScope },
{ slot: '__sco6pe__', slotValue: componentScope },
{ slot: '__sco35pe__', slotValue: componentScope },
{ slot: '__sco5pe__', slotValue: componentScope },
{ slot: '__sco1pe__', slotValue: componentScope },
{ slot: '__s12cope__', slotValue: componentScope },
{ slot: '__sc53ope__', slotValue: componentScope },
{ slot: '__scfoape__', slotValue: componentScope },
{ slot: '__scopasde__', slotValue: componentScope },
{ slot: '__scosadfpe__', slotValue: componentScope },
{ slot: '__scosadafpe__', slotValue: componentScope },
{ slot: '__scofsdpe__', slotValue: componentScope },
{ slot: '__scopfe__', slotValue: componentScope },
],
output: {
path: `./src/component/__scope__(camelCase)`,
Expand Down
105 changes: 0 additions & 105 deletions package.json

This file was deleted.

58 changes: 51 additions & 7 deletions src/GenerateTemplateFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import recursiveCopy from 'recursive-copy';
import pathExists from 'path-exists';
import through from 'through2';
import replaceString from 'replace-string';
import trie from 'trie-prefix-tree';
import trieToRegExp from 'trie-regex';
import StringUtility from './utilities/StringUtility';
import CaseConverterEnum from './constants/CaseConverterEnum';
import IConfigItem from './models/IConfigItem';
Expand Down Expand Up @@ -223,7 +225,7 @@ export default class GenerateTemplateFiles {
* Create every variation for the for the replacement keys
*/
private _getReplacers(replacers: IReplacer[], defaultCase: CaseConverterEnum): IReplacer[] {
const caseTypes: string[] = Object.values(CaseConverterEnum);
const caseTypes: CaseConverterEnum[] = Object.values(CaseConverterEnum);

return replacers.reduce(
(previousReplacers: IReplacer[], answeredReplacer: IReplacer): IReplacer[] => {
Expand All @@ -232,10 +234,10 @@ export default class GenerateTemplateFiles {
return [
...previousReplacers,
...caseTypes.map(
(caseType: string): IReplacer => {
(caseType: CaseConverterEnum): IReplacer => {
return {
slot: `${slot}${caseType}`,
slotValue: StringUtility.toCase(slotValue, caseType as CaseConverterEnum),
slotValue: StringUtility.toCase(slotValue, caseType),
};
}
),
Expand Down Expand Up @@ -329,6 +331,26 @@ export default class GenerateTemplateFiles {
): Promise<string[]> {
const outputtedFilesAndFolders: string[] = [];

// Create a function to apply the transformations in one go
const regexEscape = (text: string) => text.replace(/([^a-zA-Z0-9_])/g, '\\$1');
const replacerLookup: Record<string, string> = {};
const replacerSlots: string[] = [];
const regexTrie = trieToRegExp(trie(replacerSlots).tree());

console.log(regexTrie);

const replacerRegexBase = replacers.map((replacer: IReplacer) => {
replacerLookup[replacer.slot] = replacer.slotValue;
replacerSlots.push(replacer.slot)
return regexEscape(replacer.slot);
}).join('|');

const replacerRegex = new RegExp(`^${replacerRegexBase}$`, 'g');
const simpleReplacer = (text: string) => text.replace(replacerRegex, (slot) => replacerLookup[slot]);

const replacer = (text: string) => text.replace(regexTrie, (slot) => replacerLookup[slot]);

// Apply the transformations on all files recursively
const recursiveCopyOptions: any = {
overwrite: true,
expand: false,
Expand All @@ -351,13 +373,35 @@ export default class GenerateTemplateFiles {
},
transform: (src: string, dest: string, stats: unknown) => {
return through((chunk: any, enc: any, done: any) => {
let output: string = chunk.toString();
let output: string = ""

console.time(`new ${src}`)
for(let i = 0; i < 100000; i++) {
output = replacer(chunk.toString());
}
console.timeEnd(`new ${src}`)


console.time(`simple ${src}`)
for(let i = 0; i < 100000; i++) {
output = simpleReplacer(chunk.toString());
}
console.timeEnd(`simple ${src}`)


console.time(`orig ${src}`)
for(let i = 0; i < 100000; i++) {
replacers.forEach((replacer: IReplacer) => {
output = replaceString(output, replacer.slot, replacer.slotValue);
});
}
console.timeEnd(`orig ${src}`)


replacers.forEach((replacer: IReplacer) => {
output = replaceString(output, replacer.slot, replacer.slotValue);
});

done(null, output);


});
},
};
Expand Down
Loading