()
| 15 | * The command state contains the timestamp of the first command issued. |
| 16 | */ |
| 17 | export async function loadOrInitializeCommandState(): Promise<CommandState> { |
| 18 | const filePath = getCommandStatePath() |
| 19 | const data = await fs.promises |
| 20 | .readFile(filePath, 'utf-8') |
| 21 | .catch((err) => (err.code === 'ENOENT' ? Promise.resolve(undefined) : Promise.reject(err))) |
| 22 | const state = data === undefined ? { firstCommandTimestamp: new Date().toISOString() } : JSON.parse(data) |
| 23 | |
| 24 | if (data === undefined) { |
| 25 | await fs.promises.writeFile(filePath, JSON.stringify(state)) |
| 26 | } |
| 27 | |
| 28 | if (typeof state.firstCommandTimestamp === 'string') { |
| 29 | return state |
| 30 | } else { |
| 31 | throw new Error('Invalid command state schema') |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * Calculates the number of days since the first command was issued. |
no test coverage detected