* * @param {string} id * @returns
(id)
| 15 | * @returns |
| 16 | */ |
| 17 | static async new(id) { |
| 18 | try { |
| 19 | const state = new InstallState(); |
| 20 | state.id = await checksumText(id); |
| 21 | state.updatedStore = {}; |
| 22 | |
| 23 | if (!(await fsOperation(INSTALL_STATE_STORAGE).exists())) { |
| 24 | await fsOperation(DATA_STORAGE).createDirectory(".install-state"); |
| 25 | } |
| 26 | |
| 27 | state.storeUrl = Url.join(INSTALL_STATE_STORAGE, state.id); |
| 28 | if (await fsOperation(state.storeUrl).exists()) { |
| 29 | let raw = "{}"; |
| 30 | try { |
| 31 | raw = await fsOperation(state.storeUrl).readFile("utf-8"); |
| 32 | state.store = JSON.parse(raw); |
| 33 | } catch (err) { |
| 34 | console.error( |
| 35 | "InstallState: Failed to parse state file, deleting:", |
| 36 | err, |
| 37 | ); |
| 38 | // Delete corrupted state file to avoid parse errors such as 'Unexpected end of JSON' |
| 39 | state.store = {}; |
| 40 | try { |
| 41 | await fsOperation(state.storeUrl).delete(); |
| 42 | // Recreate a fresh empty file to keep invariant |
| 43 | await fsOperation(INSTALL_STATE_STORAGE).createFile(state.id); |
| 44 | } catch (writeErr) { |
| 45 | console.error( |
| 46 | "InstallState: Failed to recreate state file:", |
| 47 | writeErr, |
| 48 | ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | const patchedStore = {}; |
| 53 | for (const [key, value] of Object.entries(state.store)) { |
| 54 | patchedStore[key.toLowerCase()] = value; |
| 55 | } |
| 56 | |
| 57 | state.store = patchedStore; |
| 58 | } else { |
| 59 | state.store = {}; |
| 60 | await fsOperation(INSTALL_STATE_STORAGE).createFile(state.id); |
| 61 | } |
| 62 | |
| 63 | return state; |
| 64 | } catch (e) { |
| 65 | console.error(e); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * |
no test coverage detected