create db during downloadDb if it is missing
This commit is contained in:
parent
4efcaf013b
commit
352bb03a90
2 changed files with 24 additions and 6 deletions
|
@ -3,6 +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';
|
||||||
|
|
||||||
const ctx = 'downloadDb.js';
|
const ctx = 'downloadDb.js';
|
||||||
|
|
||||||
|
@ -10,8 +11,11 @@ const ctx = 'downloadDb.js';
|
||||||
* Downloads all media possible for the users stored in db.json at the specified `--path`.
|
* Downloads all media possible for the users stored in db.json at the specified `--path`.
|
||||||
* Useful for first run or for augmenting existing media
|
* Useful for first run or for augmenting existing media
|
||||||
* if it may be only partially archived in an uncertain state.
|
* if it may be only partially archived in an uncertain state.
|
||||||
* The db.json must be present in order for this to run,
|
*
|
||||||
* as it will run for the users specified in the db.json and not the directories in the folder.
|
* If the db.json is missing, it will be automatically created,
|
||||||
|
* as this depends on users being defined in the db.json and not the folders present
|
||||||
|
* (as there could be some users whose accounts no longer exist
|
||||||
|
* or otherwise may not be maintained by the db.json anymore)
|
||||||
*/
|
*/
|
||||||
const downloadDb = async () => {
|
const downloadDb = async () => {
|
||||||
log(ctx, 'Grabbing db');
|
log(ctx, 'Grabbing db');
|
||||||
|
@ -28,12 +32,26 @@ const downloadDb = async () => {
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log(ctx, 'Using 1 thread');
|
log(ctx, 'Using 1 thread');
|
||||||
}
|
}
|
||||||
try {
|
const tryReadDb = async () => {
|
||||||
let file = await readFile(`${directory}/db.json`, { encoding: 'utf8' });
|
let file = await readFile(`${directory}/db.json`, { encoding: 'utf8' });
|
||||||
db = JSON.parse(file);
|
db = JSON.parse(file);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await tryReadDb();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
error(ctx, err);
|
if (err.toString().includes('ENOENT')) {
|
||||||
return;
|
try {
|
||||||
|
log(ctx, 'Database was not yet present. Creating it now.');
|
||||||
|
await initDb();
|
||||||
|
await tryReadDb();
|
||||||
|
} catch (err2) {
|
||||||
|
error(ctx, err2);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
error(ctx, err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let processes = db.map(entry => ({
|
let processes = db.map(entry => ({
|
||||||
|
|
|
@ -11,7 +11,7 @@ const ctx = 'initDb.js';
|
||||||
* at the specified `--path` parameter when executing the command.
|
* at the specified `--path` parameter when executing the command.
|
||||||
* Useful when there is already a collection of folders.
|
* Useful when there is already a collection of folders.
|
||||||
*/
|
*/
|
||||||
const initDb = async () => {
|
export const initDb = async () => {
|
||||||
log(ctx, 'Grabbing existing directories');
|
log(ctx, 'Grabbing existing directories');
|
||||||
let directory = '';
|
let directory = '';
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Add table
Reference in a new issue