fix initDb always running in downloadDb
This commit is contained in:
parent
5c73399f5c
commit
78ceefe33f
4 changed files with 21 additions and 15 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { readdir } from 'fs/promises'
|
import { readdir } from 'fs/promises';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the directories under the specified source directory
|
* Gets the directories under the specified source directory
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { log } from './log.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns valid db schema
|
* Returns valid db schema
|
||||||
*/
|
*/
|
||||||
|
@ -18,3 +20,16 @@ export const userSchema = (entry) => {
|
||||||
pastUsernames: entry.pastUsernames,
|
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!');
|
||||||
|
}
|
||||||
|
|
|
@ -3,8 +3,7 @@ import { readFile, writeFile } from 'fs/promises';
|
||||||
import { getArg } from './lib/args.js';
|
import { getArg } from './lib/args.js';
|
||||||
import { getMany } from './lib/dl.js';
|
import { getMany } from './lib/dl.js';
|
||||||
import { error, log } from './lib/log.js';
|
import { error, log } from './lib/log.js';
|
||||||
import { initDb } from './run-initDb.js';
|
import { createDb, userSchema } from './lib/schema.js';
|
||||||
import { userSchema } from './lib/schema.js';
|
|
||||||
|
|
||||||
const ctx = 'downloadDb.js';
|
const ctx = 'downloadDb.js';
|
||||||
|
|
||||||
|
@ -43,7 +42,7 @@ const downloadDb = async () => {
|
||||||
if (err.toString().includes('ENOENT')) {
|
if (err.toString().includes('ENOENT')) {
|
||||||
try {
|
try {
|
||||||
log(ctx, 'Database was not yet present. Creating it now.');
|
log(ctx, 'Database was not yet present. Creating it now.');
|
||||||
await initDb();
|
await createDb();
|
||||||
await tryReadDb();
|
await tryReadDb();
|
||||||
} catch (err2) {
|
} catch (err2) {
|
||||||
error(ctx, err2);
|
error(ctx, err2);
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
import { writeFile } from 'fs/promises';
|
|
||||||
|
|
||||||
import { getArg } from './lib/args.js';
|
import { getArg } from './lib/args.js';
|
||||||
import { getChildDirectories } from './lib/io.js';
|
import { createDb } from './lib/schema.js';
|
||||||
import { error, log } from './lib/log.js';
|
import { error } from './lib/log.js';
|
||||||
import { userSchema, dbSchema } from './lib/schema.js';
|
|
||||||
|
|
||||||
const ctx = 'initDb.js';
|
const ctx = 'initDb.js';
|
||||||
|
|
||||||
|
@ -13,7 +10,6 @@ const ctx = 'initDb.js';
|
||||||
* Useful when there is already a collection of folders.
|
* Useful when there is already a collection of folders.
|
||||||
*/
|
*/
|
||||||
export const initDb = async () => {
|
export const initDb = async () => {
|
||||||
log(ctx, 'Grabbing existing directories');
|
|
||||||
let directory = '';
|
let directory = '';
|
||||||
try {
|
try {
|
||||||
directory = getArg('path');
|
directory = getArg('path');
|
||||||
|
@ -21,11 +17,7 @@ export const initDb = async () => {
|
||||||
error(ctx, err);
|
error(ctx, err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const children = await getChildDirectories(directory);
|
createDb();
|
||||||
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!');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
initDb();
|
initDb();
|
||||||
|
|
Loading…
Add table
Reference in a new issue