Skip to content

Commit

Permalink
Fix jsonbin settings migration (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
gossi authored Jul 17, 2022
1 parent 1b52aa5 commit 6a5f4fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
21 changes: 0 additions & 21 deletions packages/figma-theemo/src/commands/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default class MigrateCommand extends Command {

execute() {
this.migratePluginData();
this.migrateJsonbin();
}

private migratePluginData() {
Expand All @@ -28,24 +27,4 @@ export default class MigrateCommand extends Command {
figma.root.setPluginData('nodes', '');
}
}

private migrateJsonbin() {
const jsonbinUrl = this.container.settings.get('tools.jsonbin.url') as string;

if (jsonbinUrl) {
const parts = jsonbinUrl.split('/');
const id = parts.pop();

const settings = new Map(this.container.settings.settings);
settings.delete('tools.jsonbin.url');
settings.set('tools.jsonbin.id', id);

const data = {};
for (const [k,v] of settings.entries()) {
data[k] = v;
}
this.container.settings.save(data);
this.container.commander.run('read-settings');
}
}
}
44 changes: 34 additions & 10 deletions packages/figma-theemo/src/container/settings-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { DEFAULT_CONFIG } from '../../shared/config';

function mapToObject(map: Map<string, unknown>): Record<string, unknown> {
const data = {};
for (const [k,v] of map.entries()) {
data[k] = v;
}
return data;
}

export default class SettingsManager {

private cache: Map<string, any>;
Expand All @@ -24,7 +32,10 @@ export default class SettingsManager {
const settings = this.readPluginData();
settings['tools.jsonbin.key'] = await figma.clientStorage.getAsync('jsonBinApiKey') ?? '';

return { ...DEFAULT_CONFIG, ...settings };
return mapToObject(this.migrate(new Map(Object.entries({
...DEFAULT_CONFIG,
...settings
}))));
} catch (e) {
console.warn(e);
}
Expand All @@ -35,15 +46,7 @@ export default class SettingsManager {
const key = data['tools.jsonbin.key'];
delete data['tools.jsonbin.key'];

const temp = {};
for (const [k,v] of this.settings.entries()) {
temp[k] = v;
}

this.savePluginData({
...temp,
...data
});
this.savePluginData(data);
await figma.clientStorage.setAsync('jsonBinApiKey', key);

data['tools.jsonbin.key'] = key;
Expand All @@ -69,4 +72,25 @@ export default class SettingsManager {

this.savePluginData(settings);
}

private migrate(settings: Map<string, string>): Map<string, string> {
const migrated = this.migrateJsonbin(settings);

this.save(mapToObject(migrated));

return migrated;
}

private migrateJsonbin(settings: Map<string, string>) {
if (settings.has('tools.jsonbin.url')) {
const jsonbinUrl = settings.get('tools.jsonbin.url') as string;
const parts = jsonbinUrl.split('/');
const id = parts.pop();

settings.delete('tools.jsonbin.url');
settings.set('tools.jsonbin.id', id);
}

return settings;
}
}

0 comments on commit 6a5f4fe

Please sign in to comment.