1
0
Fork 0

fix initDb always running in downloadDb

This commit is contained in:
lightling 2024-02-23 23:31:07 -05:00
parent 5c73399f5c
commit 78ceefe33f
4 changed files with 21 additions and 15 deletions

View file

@ -1,4 +1,4 @@
import { readdir } from 'fs/promises'
import { readdir } from 'fs/promises';
/**
* Gets the directories under the specified source directory

View file

@ -1,3 +1,5 @@
import { log } from './log.js';
/**
* Returns valid db schema
*/
@ -18,3 +20,16 @@ export const userSchema = (entry) => {
pastUsernames: entry.pastUsernames,
}
}
/**
* Creates db at the specified directory
* @param {string} directory the directory the db.json file will be stored in
*/
export const createDb = async (directory) => {
log(ctx, 'Grabbing existing directories');
const children = await getChildDirectories(directory);
const db = dbSchema(Object.fromEntries(children.map(e => [e, userSchema({})])));
log(ctx, 'Writing database');
await writeFile(`${directory}/db.json`, JSON.stringify(db, null, 2));
log(ctx, 'Writing complete!');
}

View file

@ -3,8 +3,7 @@ import { readFile, writeFile } from 'fs/promises';
import { getArg } from './lib/args.js';
import { getMany } from './lib/dl.js';
import { error, log } from './lib/log.js';
import { initDb } from './run-initDb.js';
import { userSchema } from './lib/schema.js';
import { createDb, userSchema } from './lib/schema.js';
const ctx = 'downloadDb.js';
@ -43,7 +42,7 @@ const downloadDb = async () => {
if (err.toString().includes('ENOENT')) {
try {
log(ctx, 'Database was not yet present. Creating it now.');
await initDb();
await createDb();
await tryReadDb();
} catch (err2) {
error(ctx, err2);

View file

@ -1,9 +1,6 @@
import { writeFile } from 'fs/promises';
import { getArg } from './lib/args.js';
import { getChildDirectories } from './lib/io.js';
import { error, log } from './lib/log.js';
import { userSchema, dbSchema } from './lib/schema.js';
import { createDb } from './lib/schema.js';
import { error } from './lib/log.js';
const ctx = 'initDb.js';
@ -13,7 +10,6 @@ const ctx = 'initDb.js';
* Useful when there is already a collection of folders.
*/
export const initDb = async () => {
log(ctx, 'Grabbing existing directories');
let directory = '';
try {
directory = getArg('path');
@ -21,11 +17,7 @@ export const initDb = async () => {
error(ctx, err);
return;
}
const children = await getChildDirectories(directory);
const db = dbSchema(Object.fromEntries(children.map(e => [e, userSchema({})])));
log(ctx, 'Writing database');
await writeFile(`${directory}/db.json`, JSON.stringify(db, null, 2));
log(ctx, 'Writing complete!');
createDb();
};
initDb();