(fullMetaEditsFolderPath: string, openDialog: ShowMessageBoxFunc, state: BackState)
| 88 | * @param state Back State |
| 89 | */ |
| 90 | export async function importAllMetaEdits(fullMetaEditsFolderPath: string, openDialog: ShowMessageBoxFunc, state: BackState): Promise<ImportMetaEditResult> { |
| 91 | const errors: Error[] = []; |
| 92 | |
| 93 | // Load all meta edit files |
| 94 | const files: LoadedMetaEditFile[] = []; |
| 95 | try { |
| 96 | const filenames = await fs.promises.readdir(fullMetaEditsFolderPath); |
| 97 | |
| 98 | for (const filename of filenames) { |
| 99 | try { |
| 100 | const fullFilename = path.join(fullMetaEditsFolderPath, filename); |
| 101 | |
| 102 | const stats = await fs.promises.stat(fullFilename); |
| 103 | |
| 104 | const raw = await readJsonFile(fullFilename); |
| 105 | const parsed = parseMetaEdit(raw); |
| 106 | |
| 107 | files.push({ |
| 108 | filename: filename, |
| 109 | mtime: stats.mtimeMs, |
| 110 | content: parsed, |
| 111 | }); |
| 112 | } catch (error: any) { errors.push(error); } |
| 113 | } |
| 114 | } catch (error: any) { errors.push(error); } |
| 115 | |
| 116 | // Abort if any file failed to load |
| 117 | if (errors.length > 0) { |
| 118 | return { |
| 119 | aborted: true, |
| 120 | errors: errors, |
| 121 | }; |
| 122 | } |
| 123 | |
| 124 | // Order meta edits |
| 125 | files.sort((a, b) => a.mtime - b.mtime); // Oldest first |
| 126 | |
| 127 | // Combine edits from all meta edits |
| 128 | const combinedMetas: CombinedMetaRecord = {}; |
| 129 | for (const file of files) { |
| 130 | for (const meta of file.content.metas) { |
| 131 | let combined = combinedMetas[meta.id]; |
| 132 | if (!combined) { |
| 133 | combined = combinedMetas[meta.id] = {}; |
| 134 | } |
| 135 | |
| 136 | const keys = Object.keys(meta) as (keyof typeof meta)[]; |
| 137 | for (const key of keys) { |
| 138 | if (key !== 'id') { // (ID is for identification, not to be modified) |
| 139 | // TODO: Fix values typing later |
| 140 | let values: any = combined[key]; |
| 141 | if (!values) { |
| 142 | values = combined[key] = []; |
| 143 | } |
| 144 | |
| 145 | const metaValue = meta[key]; |
| 146 | const isDuplicate = values.some((val: MetaChangeBase<keyof MetaEditMetaMap>) => ( |
| 147 | (val.value === metaValue) || |
no test coverage detected