Skip to content

Commit

Permalink
adds bulkremap support (#2726)
Browse files Browse the repository at this point in the history
This is just a rebase of
#2511 to release-139.
Merging as code is not mine and I'm merely approving.
  • Loading branch information
six7 authored May 6, 2024
1 parent ef7af55 commit 19242fd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-spoons-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/figma-plugin": patch
---

Added regex support to bulk remap
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,53 @@ describe('bulkRemapTokens', () => {
expect(mockSetSharedPluginData.mock.calls[0]).toEqual(['tokens', 'borderRadius', '"new.border-radius"']);
expect(mockSetSharedPluginData.mock.calls[5]).toEqual(['tokens', 'fill', '"new-color.gray.300"']);
});

it('should be able to replace all matching token names with new token name using regex', async () => {
findNodesSpy.mockImplementationOnce(() => Promise.resolve([
{
id: '295:3',
node: {
id: '295:3',
setSharedPluginData: mockSetSharedPluginData,
} as unknown as BaseNode,
tokens: {
borderRadius: 'old.border-radius',
fill: 'old.slate.400',
sizing: 'old-size.450',
spacing: 'something-else.foobar',
},
},
{
id: '295:4',
node: {
id: '295:4',
setSharedPluginData: mockSetSharedPluginData,
} as unknown as RectangleNode,
tokens: {
fill: 'old.red.500',
sizing: 'old-size.1600',
},
},
{
id: '295:5',
node: {
id: '295:5',
setSharedPluginData: mockSetSharedPluginData,
} as unknown as TextNode,
tokens: {
fill: 'old-color.gray.300',
},
},
]));
await bulkRemapTokens({
type: AsyncMessageTypes.BULK_REMAP_TOKENS,
oldName: '/old-size.*/g',
newName: 'new-size.1000',
updateMode: UpdateMode.SELECTION,
});

expect(mockSetSharedPluginData).toHaveBeenCalledTimes(2);
expect(mockSetSharedPluginData.mock.calls[0]).toEqual(['tokens', 'sizing', '"new-size.1000"']);
expect(mockSetSharedPluginData.mock.calls[1]).toEqual(['tokens', 'sizing', '"new-size.1000"']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ 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]) => {
if (value.includes(oldName)) {
const newValue = value.replace(oldName, newName);
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);
node.setSharedPluginData(namespace, key, jsonValue);
}
Expand Down

0 comments on commit 19242fd

Please sign in to comment.