Add command to list node-persist data

This commit is contained in:
Samuel Elliott
2022-10-15 22:20:54 +01:00
parent 65b36a384e
commit bf48cc201b
4 changed files with 167 additions and 29 deletions

View File

@@ -1,4 +1,5 @@
import * as path from 'node:path';
import * as fs from 'node:fs/promises';
import createDebug from 'debug';
import persist from 'node-persist';
import getPaths from 'env-paths';
@@ -15,3 +16,16 @@ export async function initStorage(dir: string) {
await storage.init();
return storage;
}
export async function* iterateLocalStorage(storage: persist.LocalStorage) {
const dir = (storage as unknown as {options: persist.InitOptions}).options.dir!;
for await (const file of await fs.opendir(dir)) {
if (!file.isFile()) continue;
const datum = await storage.readFile(path.join(dir, file.name)) as persist.Datum;
if (!datum || !datum.key) continue;
yield datum;
}
}