|
1 |
| -import { readdir } from 'fs'; |
2 |
| -import { promisify } from 'util'; |
| 1 | +import { readdir, stat } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { convertHistoryFilenameToDisplayText } from 'uhk-common'; |
| 4 | +import { getMd5HashFromFilename } from 'uhk-common'; |
| 5 | +import { |
| 6 | + DeviceUserConfigHistory, |
| 7 | + sortStringDesc, |
| 8 | + UHK_DEVICES, |
| 9 | + UserConfigHistory, |
| 10 | +} from 'uhk-common'; |
3 | 11 |
|
4 | 12 | import { getUserConfigHistoryDirAsync } from './get-user-config-history-dir-async';
|
| 13 | +import { loadUserConfigFromBinaryFile } from './load-user-config-from-binary-file'; |
5 | 14 |
|
6 |
| -const readdirAsync = promisify(readdir); |
| 15 | +export async function loadUserConfigHistoryAsync(): Promise<UserConfigHistory> { |
| 16 | + const history: UserConfigHistory = { |
| 17 | + commonFiles: [], |
| 18 | + devices: [] |
| 19 | + }; |
7 | 20 |
|
8 |
| -export async function loadUserConfigHistoryAsync(): Promise<Array<string>> { |
9 |
| - const files = await readdirAsync(await getUserConfigHistoryDirAsync()); |
| 21 | + const userConfigHistoryDir = await getUserConfigHistoryDirAsync(); |
| 22 | + const entries = await readdir(userConfigHistoryDir); |
10 | 23 |
|
11 |
| - return files; |
| 24 | + for (const entry of entries) { |
| 25 | + const filePath = path.join(userConfigHistoryDir, entry); |
| 26 | + const entryStat = await stat(filePath); |
| 27 | + |
| 28 | + if (entryStat.isFile()) { |
| 29 | + if (path.extname(entry) === '.bin') { |
| 30 | + history.commonFiles.push({ |
| 31 | + filePath, |
| 32 | + md5Hash: getMd5HashFromFilename(entry), |
| 33 | + timestamp: convertHistoryFilenameToDisplayText(entry), |
| 34 | + }); |
| 35 | + } |
| 36 | + } else if (entryStat.isDirectory()) { |
| 37 | + const entrySplit = entry.split('-'); |
| 38 | + |
| 39 | + if (entrySplit.length !== 2) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + const deviceId = Number.parseInt(entrySplit[1], 10); |
| 44 | + |
| 45 | + if (isNaN(deviceId)) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + const deviceHistoryDir = path.join(userConfigHistoryDir, entry); |
| 50 | + const deviceHistory: DeviceUserConfigHistory = { |
| 51 | + uniqueId: Number.parseInt(entrySplit[0], 10), |
| 52 | + device: UHK_DEVICES.find(device => device.id === deviceId), |
| 53 | + deviceName: '', |
| 54 | + files: (await readdir(deviceHistoryDir)) |
| 55 | + .filter(file => path.extname(file) === '.bin') |
| 56 | + .sort(sortStringDesc) |
| 57 | + .map(file => { |
| 58 | + return { |
| 59 | + filePath: path.join(deviceHistoryDir, file), |
| 60 | + md5Hash: getMd5HashFromFilename(file), |
| 61 | + timestamp: convertHistoryFilenameToDisplayText(file), |
| 62 | + }; |
| 63 | + }), |
| 64 | + }; |
| 65 | + |
| 66 | + for (const file of deviceHistory.files) { |
| 67 | + try { |
| 68 | + const userConfig = await loadUserConfigFromBinaryFile(file.filePath); |
| 69 | + deviceHistory.deviceName = userConfig.deviceName; |
| 70 | + break; |
| 71 | + } catch { |
| 72 | + // Maybe the user config is newer than Agent supports, or corrupted. |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + history.devices.push(deviceHistory); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + history.commonFiles |
| 81 | + .sort((a, b) => sortStringDesc(a.timestamp, b.timestamp)); |
| 82 | + |
| 83 | + return history; |
12 | 84 | }
|
0 commit comments