From bf07999ed8b58b7eedce1ca0476bf5888f1373cf Mon Sep 17 00:00:00 2001 From: Sylvie Date: Sun, 26 Nov 2023 11:59:34 -0700 Subject: [PATCH] fix: missing `data.json` breaking auth.json migration --- src/auth.ts | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/auth.ts b/src/auth.ts index eb9382c..06fe30d 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -69,38 +69,40 @@ const migrate = (authFileName = 'auth.json'): Promise => new Promise(asyn .catch(reject) // Migrate the datafile (token => uploader) - .then(() => data().get()) - .then((fileData: [string, FileData][]) => + .then(() => (!data()) + ? (log.warn('data.json not found. This may be a new install?'), Promise.resolve()) + : data().get().then((fileData: [string, FileData][]) => - // ! A note about this block. - // I know it's gross. But using Promise.all crashes low-spec servers, so I had to do it this way. Sorry. - // Thanks to CoPilot for writing `runQueue` :D + // ! A note about this block. + // I know it's gross. But using Promise.all crashes low-spec servers, so I had to do it this way. Sorry. + // Thanks to CoPilot for writing `runQueue` :D - // Wait for all the deletions and puts to finish - new Promise((resolve, reject) => { + // Wait for all the deletions and puts to finish + new Promise((resolve, reject) => { - // Create a queue of functions to run - const queue = fileData.map(([key, file]) => async () => { + // Create a queue of functions to run + const queue = fileData.map(([key, file]) => async () => { - // We need to use `newUsers` because `users` hasn't been re-assigned yet - const user = newUsers.users.find((user) => user.token === file.token!)?.unid ?? ''; // ? This is probably fine + // We need to use `newUsers` because `users` hasn't been re-assigned yet + const user = newUsers.users.find((user) => user.token === file.token!)?.unid ?? ''; // ? This is probably fine - // Because of the stupid way I wrote papito, we need to DEL before we can PUT - await data().del(key); + // Because of the stupid way I wrote papito, we need to DEL before we can PUT + await data().del(key); - // PUT the new data - return data().put(key, { ...file, uploader: user }); - }); + // PUT the new data + return data().put(key, { ...file, uploader: user }); + }); - // Recursively run the queue, hopefully sequentially without running out of memory - const runQueue = (index: number) => { - if (index >= queue.length) return resolve(void 0); - queue[index]().then(() => runQueue(index + 1)).catch(reject); - }; - - runQueue(0); - })) + // Recursively run the queue, hopefully sequentially without running out of memory + const runQueue = (index: number) => { + if (index >= queue.length) return resolve(void 0); + queue[index]().then(() => runQueue(index + 1)).catch(reject); + }; + runQueue(0); + })) + .catch((err: any) => log.warn(err.message)) + ) // We did it hoofuckingray .then(() => log.success('Migrated all auth & file data to new auth system')) .then(() => resolve(newUsers))