initDb
This commit is contained in:
parent
89d5f437f5
commit
0a9af1b964
2 changed files with 34 additions and 0 deletions
28
initDb.js
Normal file
28
initDb.js
Normal file
|
@ -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();
|
6
io.js
Normal file
6
io.js
Normal file
|
@ -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)
|
Loading…
Add table
Reference in a new issue