mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 09:57:02 +01:00
Convert the migration template to TypeScript (#17836)
The migration template has been converted to TypeScript. We can start writing migrations in TypeScript today; they have no dependencies on JavaScript modules.
This commit is contained in:
parent
e223348fb3
commit
cb113dd2a6
@ -1,29 +0,0 @@
|
||||
// next version number
|
||||
/*
|
||||
|
||||
description of migration and what it does
|
||||
|
||||
*/
|
||||
|
||||
import { cloneDeep } from 'lodash';
|
||||
|
||||
const version = 0;
|
||||
|
||||
export default {
|
||||
version,
|
||||
|
||||
async migrate(originalVersionedData) {
|
||||
const versionedData = cloneDeep(originalVersionedData);
|
||||
versionedData.meta.version = version;
|
||||
const state = versionedData.data;
|
||||
const newState = transformState(state);
|
||||
versionedData.data = newState;
|
||||
return versionedData;
|
||||
},
|
||||
};
|
||||
|
||||
function transformState(state) {
|
||||
const newState = state;
|
||||
// transform state here
|
||||
return newState;
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
import migrationTemplate from './template';
|
||||
import { migrate, version } from './template';
|
||||
|
||||
const storage = {
|
||||
meta: {},
|
||||
meta: { version: -1 },
|
||||
data: {},
|
||||
};
|
||||
|
||||
describe('storage is migrated successfully', () => {
|
||||
it('should work', async () => {
|
||||
const migratedData = await migrationTemplate.migrate(storage);
|
||||
expect(migratedData.meta.version).toStrictEqual(0);
|
||||
const migratedData = await migrate(storage);
|
||||
expect(migratedData.meta.version).toStrictEqual(version);
|
||||
});
|
||||
});
|
||||
|
28
app/scripts/migrations/template.ts
Normal file
28
app/scripts/migrations/template.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { cloneDeep } from 'lodash';
|
||||
|
||||
export const version = 0;
|
||||
|
||||
/**
|
||||
* Explain the purpose of the migration here.
|
||||
*
|
||||
* @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist.
|
||||
* @param originalVersionedData.meta - State metadata.
|
||||
* @param originalVersionedData.meta.version - The current state version.
|
||||
* @param originalVersionedData.data - The persisted MetaMask state, keyed by controller.
|
||||
* @returns Updated versioned MetaMask extension state.
|
||||
*/
|
||||
export async function migrate(originalVersionedData: {
|
||||
meta: { version: number };
|
||||
data: Record<string, unknown>;
|
||||
}) {
|
||||
const versionedData = cloneDeep(originalVersionedData);
|
||||
versionedData.meta.version = version;
|
||||
versionedData.data = transformState(versionedData.data);
|
||||
return versionedData;
|
||||
}
|
||||
|
||||
function transformState(state: Record<string, unknown>) {
|
||||
const newState = state;
|
||||
// transform state here
|
||||
return newState;
|
||||
}
|
Loading…
Reference in New Issue
Block a user