-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
142 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import fs from 'node:fs' | ||
import path from 'pathe' | ||
import glob from 'fast-glob' | ||
import SuperJSON from 'superjson' | ||
|
||
/** | ||
* Migrate snapshot folder to new format with each resource type in a single file | ||
* @param snapshotFolder The snapshot folder to migrate | ||
* @returns Whether the folder was migrated | ||
*/ | ||
export async function migrateSnapshotFolder(snapshotFolder: string) { | ||
const files = await glob('*/*.json', { | ||
cwd: snapshotFolder, | ||
onlyFiles: true, | ||
}) | ||
|
||
if (files.length === 0) { | ||
return false | ||
} | ||
|
||
const dataPerResource = new Map<string, Record<string, any>>() | ||
|
||
for (const file of files) { | ||
const [resourceName, id] = file.split('/') | ||
const filePath = path.join(snapshotFolder, file) | ||
const content = await fs.promises.readFile(filePath, 'utf8') | ||
const fileData = SuperJSON.parse(content) | ||
let dataForResource = dataPerResource.get(resourceName) | ||
if (!dataForResource) { | ||
dataForResource = {} | ||
dataPerResource.set(resourceName, dataForResource) | ||
} | ||
dataForResource[id] = fileData | ||
} | ||
|
||
for (const [resourceName, data] of dataPerResource) { | ||
const resourceFile = path.join(snapshotFolder, `${resourceName}.res.json`) | ||
// Sort by key to ensure consistent order | ||
const sortedData = Object.fromEntries(Object.entries(data).sort(([a], [b]) => a.localeCompare(b))) | ||
await fs.promises.writeFile(resourceFile, JSON.stringify(SuperJSON.serialize(sortedData), null, 2), 'utf8') | ||
} | ||
|
||
await Promise.all(files.map(file => fs.promises.rm(path.join(snapshotFolder, file)))) | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters