Skip to content

Commit

Permalink
fixup! feat: added regex support to bulk remap
Browse files Browse the repository at this point in the history
  • Loading branch information
xsu1010 committed Feb 23, 2024
1 parent 53eaa5f commit 8467f02
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('bulkRemapTokens', () => {
]));
await bulkRemapTokens({
type: AsyncMessageTypes.BULK_REMAP_TOKENS,
oldName: 'old-size.*',
oldName: '/old-size.*/g',
newName: 'new-size.1000',
updateMode: UpdateMode.SELECTION,
});
Expand Down
5 changes: 4 additions & 1 deletion src/plugin/asyncMessageHandlers/bulkRemapTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ export const bulkRemapTokens: AsyncMessageChannelHandlers[AsyncMessageTypes.BULK

const tracker = new ProgressTracker(BackgroundJobs.PLUGIN_UPDATEPLUGINDATA);
const promises: Set<Promise<void>> = new Set();
const regexPattern = /^\/(.*)\/([gimsuy]*)$/;

allWithData.forEach(({ node, tokens }) => {
promises.add(defaultWorker.schedule(async () => {
Object.entries(tokens).forEach(([key, value]) => {
const pattern = (/^\/.*\/$/.test(oldName)) ? new RegExp(oldName.slice(1, -1)) : new RegExp(oldName);
const regexTest = oldName.match(regexPattern);
// If the pattern passed is a regex, use it, otherwise use the old name with a global flag
const pattern = regexTest ? new RegExp(regexTest[1], regexTest[2]) : new RegExp(oldName, 'g');
if (pattern.test(value)) {
const newValue = value.replace(pattern, newName);
const jsonValue = JSON.stringify(newValue);
Expand Down

0 comments on commit 8467f02

Please sign in to comment.