david
3 years ago
7 changed files with 246 additions and 99 deletions
@ -1,21 +0,0 @@ |
|||||||
import dayjs from 'dayjs'; |
|
||||||
|
|
||||||
const backupTimeDiff = (lastDate) => { |
|
||||||
try { |
|
||||||
const now = dayjs(); |
|
||||||
const last = dayjs(lastDate); |
|
||||||
const difference = now.diff(last, 'seconds'); |
|
||||||
return difference; |
|
||||||
} catch (error) { |
|
||||||
console.error(error); |
|
||||||
return null; |
|
||||||
} |
|
||||||
}; |
|
||||||
|
|
||||||
const backupsToDelete = (backupArray) => { |
|
||||||
// get the list of backups to delete and return only the names
|
|
||||||
if (!backupArray) return []; |
|
||||||
return backupArray.map(({ name }) => name); |
|
||||||
}; |
|
||||||
|
|
||||||
export { backupTimeDiff, backupsToDelete }; |
|
@ -0,0 +1,20 @@ |
|||||||
|
import dotenv from 'dotenv'; |
||||||
|
dotenv.config(); |
||||||
|
|
||||||
|
const { |
||||||
|
BASE_URL, |
||||||
|
TOKEN, |
||||||
|
BACKUP_INTERVAL, |
||||||
|
REMOTE_BACKUPS_MAX, |
||||||
|
EXPORT_ATTEMPTS, |
||||||
|
} = process.env; |
||||||
|
|
||||||
|
const envVars = { |
||||||
|
BASE_URL, |
||||||
|
TOKEN, |
||||||
|
BACKUP_INTERVAL, |
||||||
|
REMOTE_BACKUPS_MAX, |
||||||
|
EXPORT_ATTEMPTS, |
||||||
|
}; |
||||||
|
|
||||||
|
export { envVars }; |
@ -0,0 +1,66 @@ |
|||||||
|
import dayjs from 'dayjs'; |
||||||
|
import { envVars } from './config.js'; |
||||||
|
|
||||||
|
const logger = ({ type = 'info', msg }) => { |
||||||
|
const timestamp = new Date().toLocaleString(); |
||||||
|
|
||||||
|
let colour = '\x1b[39m'; |
||||||
|
let prefix; |
||||||
|
switch (type) { |
||||||
|
case 'error': |
||||||
|
colour = '\x1b[91m'; |
||||||
|
prefix = '[ERROR] '; |
||||||
|
break; |
||||||
|
case 'warn': |
||||||
|
colour = '\x1b[33m'; |
||||||
|
prefix = '[WARN] '; |
||||||
|
break; |
||||||
|
default: |
||||||
|
colour = '\x1b[32m'; |
||||||
|
prefix = '[INFO] '; |
||||||
|
} |
||||||
|
console.log(colour + prefix + timestamp + ': ' + msg); |
||||||
|
}; |
||||||
|
|
||||||
|
const validationErrorCheck = (error) => { |
||||||
|
try { |
||||||
|
if (error) throw error?.details[0]?.message; |
||||||
|
} catch (error) { |
||||||
|
logger({ type: 'error', msg: error }); |
||||||
|
process.exit(0); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
// rubbish idea not being used
|
||||||
|
const backupTimeDiff = async (lastDate) => { |
||||||
|
try { |
||||||
|
const now = dayjs(); |
||||||
|
const last = dayjs(lastDate); |
||||||
|
const difference = now.diff(last, 'seconds'); |
||||||
|
return difference; |
||||||
|
} catch (error) { |
||||||
|
logger({ type: 'error', msg: error }); |
||||||
|
return null; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
const backupsToDelete = async (backupArray) => { |
||||||
|
// get the list of backups to delete and return only the names
|
||||||
|
if (!backupArray) return []; |
||||||
|
return backupArray.map(({ name }) => name); |
||||||
|
}; |
||||||
|
|
||||||
|
const haveSomeCoffee = async () => { |
||||||
|
await new Promise( |
||||||
|
(resolve) => setTimeout(resolve, envVars.BACKUP_INTERVAL * 1000 * 60) // convert to mins
|
||||||
|
// (resolve) => setTimeout(resolve, 500) // for testing
|
||||||
|
); |
||||||
|
}; |
||||||
|
|
||||||
|
export { |
||||||
|
validationErrorCheck, |
||||||
|
backupTimeDiff, |
||||||
|
backupsToDelete, |
||||||
|
haveSomeCoffee, |
||||||
|
logger, |
||||||
|
}; |
Loading…
Reference in new issue