()
| 33 | } |
| 34 | |
| 35 | export async function getLastKnownGood(): Promise<Record<string, string>> { |
| 36 | let raw: string; |
| 37 | try { |
| 38 | raw = await fs.promises.readFile(getLastKnownGoodFilePath(), `utf8`); |
| 39 | } catch (err) { |
| 40 | if ((err as NodeError)?.code === `ENOENT`) { |
| 41 | debugUtils.log(`No LastKnownGood version found in Corepack home.`); |
| 42 | return {}; |
| 43 | } |
| 44 | throw err; |
| 45 | } |
| 46 | |
| 47 | try { |
| 48 | const parsed = JSON.parse(raw); |
| 49 | if (!parsed) { |
| 50 | debugUtils.log(`Invalid LastKnowGood file in Corepack home (JSON parsable, but falsy)`); |
| 51 | return {}; |
| 52 | } |
| 53 | if (typeof parsed !== `object`) { |
| 54 | debugUtils.log(`Invalid LastKnowGood file in Corepack home (JSON parsable, but non-object)`); |
| 55 | return {}; |
| 56 | } |
| 57 | Object.entries(parsed).forEach(([key, value]) => { |
| 58 | if (typeof value !== `string`) { |
| 59 | // Ensure that all entries are strings. |
| 60 | debugUtils.log(`Ignoring key ${key} in LastKnownGood file as its value is not a string`); |
| 61 | delete parsed[key]; |
| 62 | } |
| 63 | }); |
| 64 | return parsed; |
| 65 | } catch { |
| 66 | // Ignore errors; too bad |
| 67 | debugUtils.log(`Invalid LastKnowGood file in Corepack home (maybe not JSON parsable)`); |
| 68 | return {}; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | async function createLastKnownGoodFile(lastKnownGood: Record<string, string>) { |
| 73 | const content = `${JSON.stringify(lastKnownGood, null, 2)}\n`; |
no test coverage detected
searching dependent graphs…