diff --git a/initDb.js b/initDb.js new file mode 100644 index 0000000..7e16b86 --- /dev/null +++ b/initDb.js @@ -0,0 +1,28 @@ +import { writeFile } from 'fs/promises'; + +import { getArg } from './args.js'; +import { getChildren } from './io.js'; +import { error, log } from './log.js'; + +const ctx = 'initDb.js'; + +const initDb = async () => { + log(ctx, 'Grabbing existing directories'); + let directory = ''; + try { + directory = getArg('path'); + } catch (err) { + error(ctx, err); + return; + } + const children = await getChildren(directory); + const db = children.map(child => ({ + 'user': child, + 'lastUpdated': 'never', + })); + log(ctx, 'Writing database'); + await writeFile(`${directory}/db.json`, JSON.stringify(db, null, 2)); + log(ctx, 'Writing complete!'); +}; + +initDb(); diff --git a/io.js b/io.js new file mode 100644 index 0000000..b62b8f8 --- /dev/null +++ b/io.js @@ -0,0 +1,6 @@ +import { readdir } from 'fs/promises' + +export const getChildren = async source => + (await readdir(source, { withFileTypes: true })) + .filter(dirent => dirent.isDirectory()) + .map(dirent => dirent.name)