From 857425911517c3dfe9cd34334ce32677322ad288 Mon Sep 17 00:00:00 2001 From: Roberto Tonino Date: Wed, 19 May 2021 20:41:40 +0200 Subject: [PATCH] chore(server): linting --- server/src/main.ts | 65 ++++++++++--------- server/src/routes/api/get/getQueue.ts | 19 +++--- server/src/routes/index.ts | 6 +- .../websocket/modules/cancelAllDownloads.ts | 2 +- server/src/websocket/modules/index.ts | 7 +- .../modules/removeFinishedDownloads.ts | 2 +- .../src/websocket/modules/removeFromQueue.ts | 2 +- 7 files changed, 50 insertions(+), 53 deletions(-) diff --git a/server/src/main.ts b/server/src/main.ts index e42aed0..866df02 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -33,7 +33,7 @@ export function saveSettings(newSettings: any) { } export let queueOrder: string[] = [] -export let queue: any = {} +export const queue: any = {} export let currentJob: any = null restoreQueueFromDisk() @@ -52,7 +52,8 @@ export async function addToQueue(dz: any, url: string, bitrate: number) { downloadObjs.forEach((downloadObj: any) => { // Check if element is already in queue - if (Object.keys(queue).includes(downloadObj.uuid)) throw new AlreadyInQueue(downloadObj.getEssentialDict(), !isSingleObject) + if (Object.keys(queue).includes(downloadObj.uuid)) + throw new AlreadyInQueue(downloadObj.getEssentialDict(), !isSingleObject) // Save queue status when adding something to the queue if (!fs.existsSync(configFolder + 'queue')) fs.mkdirSync(configFolder + 'queue') @@ -60,10 +61,10 @@ export async function addToQueue(dz: any, url: string, bitrate: number) { queueOrder.push(downloadObj.uuid) fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder)) queue[downloadObj.uuid] = downloadObj.getEssentialDict() - queue[downloadObj.uuid].status = "inQueue" + queue[downloadObj.uuid].status = 'inQueue' - let savedObject = downloadObj.toDict() - savedObject.status = "inQueue" + const savedObject = downloadObj.toDict() + savedObject.status = 'inQueue' fs.writeFileSync(configFolder + `queue${sep}${downloadObj.uuid}.json`, JSON.stringify(savedObject)) slimmedObjects.push(downloadObj.getSlimmedDict()) @@ -83,9 +84,9 @@ async function startQueue(dz: any): Promise { } currentJob = true // lock currentJob - const currentUUID: string = queueOrder.shift() || "" + const currentUUID: string = queueOrder.shift() || '' console.log(currentUUID) - queue[currentUUID].status = "downloading" + queue[currentUUID].status = 'downloading' const currentItem: any = JSON.parse(fs.readFileSync(configFolder + `queue${sep}${currentUUID}.json`).toString()) let downloadObject: any switch (currentItem.__type__) { @@ -104,17 +105,17 @@ async function startQueue(dz: any): Promise { listener.send('startDownload', currentUUID) await currentJob.start() - if (! downloadObject.isCanceled){ + if (!downloadObject.isCanceled) { // Set status - if (downloadObject.failed == downloadObject.size){ - queue[currentUUID].status = "failed" - } else if(downloadObject.failed > 0){ - queue[currentUUID].status = "withErrors" + if (downloadObject.failed == downloadObject.size) { + queue[currentUUID].status = 'failed' + } else if (downloadObject.failed > 0) { + queue[currentUUID].status = 'withErrors' } else { - queue[currentUUID].status = "completed" + queue[currentUUID].status = 'completed' } - let savedObject = downloadObject.getSlimmedDict() + const savedObject = downloadObject.getSlimmedDict() savedObject.status = queue[currentUUID].status // Save queue status @@ -127,18 +128,20 @@ async function startQueue(dz: any): Promise { } while (queueOrder.length) } -export function cancelDownload(uuid: string){ - if (Object.keys(queue).includes(uuid)){ +export function cancelDownload(uuid: string) { + if (Object.keys(queue).includes(uuid)) { switch (queue[uuid].status) { - case "downloading": + case 'downloading': currentJob.downloadObject.isCanceled = true - listener.send("cancellingCurrentItem", uuid) + listener.send('cancellingCurrentItem', uuid) break - case "inQueue": + case 'inQueue': queueOrder.splice(queueOrder.indexOf(uuid), 1) fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder)) + // break default: - listener.send("removedFromQueue", uuid) + // This gets called even in the 'inQueue' case. Is this the expected behaviour? If no, de-comment the break + listener.send('removedFromQueue', uuid) break } fs.unlinkSync(configFolder + `queue${sep}${uuid}.json`) @@ -146,41 +149,41 @@ export function cancelDownload(uuid: string){ } } -export function cancelAllDownloads(){ +export function cancelAllDownloads() { queueOrder = [] let currentItem: string | null = null Object.values(queue).forEach((downloadObject: any) => { - if (downloadObject.status == "downloading"){ + if (downloadObject.status == 'downloading') { currentJob.downloadObject.isCanceled = true - listener.send("cancellingCurrentItem", downloadObject.uuid) + listener.send('cancellingCurrentItem', downloadObject.uuid) currentItem = downloadObject.uuid } fs.unlinkSync(configFolder + `queue${sep}${downloadObject.uuid}.json`) delete queue[downloadObject.uuid] }) fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder)) - listener.send("removedAllDownloads", currentItem) + listener.send('removedAllDownloads', currentItem) } -export function clearCompletedDownloads(){ +export function clearCompletedDownloads() { Object.values(queue).forEach((downloadObject: any) => { - if (downloadObject.status === "completed"){ + if (downloadObject.status === 'completed') { fs.unlinkSync(configFolder + `queue${sep}${downloadObject.uuid}.json`) delete queue[downloadObject.uuid] } }) - listener.send("removedFinishedDownloads") + listener.send('removedFinishedDownloads') } -export function restoreQueueFromDisk(){ +export function restoreQueueFromDisk() { if (!fs.existsSync(configFolder + 'queue')) fs.mkdirSync(configFolder + 'queue') const allItems: string[] = fs.readdirSync(configFolder + 'queue') allItems.forEach((filename: string) => { - if (filename == 'order.json'){ + if (filename == 'order.json') { queueOrder = JSON.parse(fs.readFileSync(configFolder + `queue${sep}order.json`).toString()) } else { const currentItem: any = JSON.parse(fs.readFileSync(configFolder + `queue${sep}${filename}`).toString()) - if (currentItem.status === 'inQueue'){ + if (currentItem.status === 'inQueue') { let downloadObject: any switch (currentItem.__type__) { case 'Single': @@ -194,7 +197,7 @@ export function restoreQueueFromDisk(){ break } queue[downloadObject.uuid] = downloadObject.getEssentialDict() - queue[downloadObject.uuid].status = "inQueue" + queue[downloadObject.uuid].status = 'inQueue' } else { queue[currentItem.uuid] = currentItem } diff --git a/server/src/routes/api/get/getQueue.ts b/server/src/routes/api/get/getQueue.ts index fca3a7a..f39b5ac 100644 --- a/server/src/routes/api/get/getQueue.ts +++ b/server/src/routes/api/get/getQueue.ts @@ -1,19 +1,18 @@ -// @ts-expect-error -import { Deezer } from 'deezer-js' +// import { Deezer } from 'deezer-js' import { ApiHandler } from '../../../types' import { queueOrder, queue, currentJob } from '../../../main' const path: ApiHandler['path'] = '/getQueue' -let homeCache: any +// let homeCache: any -const handler: ApiHandler['handler'] = async (req, res) => { - let result = { - queue, - order: queueOrder, - currentItem: "", - } - if (currentJob) result.currentItem = currentJob.downloadObject.uuid +const handler: ApiHandler['handler'] = (req, res) => { + const result = { + queue, + order: queueOrder, + currentItem: '' + } + if (currentJob) result.currentItem = currentJob.downloadObject.uuid res.send(result) } diff --git a/server/src/routes/index.ts b/server/src/routes/index.ts index f8a022f..9d76971 100644 --- a/server/src/routes/index.ts +++ b/server/src/routes/index.ts @@ -18,7 +18,7 @@ router.get('/connect', (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() const dz = sessionDZ[req.session.id] - let result: any = { + const result: any = { update: { currentCommit: 'testing', latestCommit: 'testing', @@ -30,12 +30,12 @@ router.get('/connect', (req, res) => { deezerNotAvailable: false } - if (Object.keys(queue).length > 0){ + if (Object.keys(queue).length > 0) { result.queue = { queue, queueOrder } - if (currentJob && currentJob !== true){ + if (currentJob && currentJob !== true) { result.queue.current = currentJob.downloadObject.uuid } } diff --git a/server/src/websocket/modules/cancelAllDownloads.ts b/server/src/websocket/modules/cancelAllDownloads.ts index f8753cb..7f39d4f 100644 --- a/server/src/websocket/modules/cancelAllDownloads.ts +++ b/server/src/websocket/modules/cancelAllDownloads.ts @@ -5,7 +5,7 @@ import { cancelAllDownloads } from '../../main' const eventName = 'cancelAllDownloads' const cb = (_: any, __: any, ___: WsServer) => { - cancelAllDownloads() + cancelAllDownloads() consoleInfo(`Queue cleared`) } diff --git a/server/src/websocket/modules/index.ts b/server/src/websocket/modules/index.ts index 51f1393..1f0c6f4 100644 --- a/server/src/websocket/modules/index.ts +++ b/server/src/websocket/modules/index.ts @@ -3,9 +3,4 @@ import removeFinishedDownloads from './removeFinishedDownloads' import removeFromQueue from './removeFromQueue' import cancelAllDownloads from './cancelAllDownloads' -export default [ - saveSettings, - removeFinishedDownloads, - removeFromQueue, - cancelAllDownloads -] +export default [saveSettings, removeFinishedDownloads, removeFromQueue, cancelAllDownloads] diff --git a/server/src/websocket/modules/removeFinishedDownloads.ts b/server/src/websocket/modules/removeFinishedDownloads.ts index 49c79ff..9c4ea9a 100644 --- a/server/src/websocket/modules/removeFinishedDownloads.ts +++ b/server/src/websocket/modules/removeFinishedDownloads.ts @@ -5,7 +5,7 @@ import { clearCompletedDownloads } from '../../main' const eventName = 'removeFinishedDownloads' const cb = (_: any, __: any, ___: WsServer) => { - clearCompletedDownloads() + clearCompletedDownloads() consoleInfo('Completed downloads cleared') } diff --git a/server/src/websocket/modules/removeFromQueue.ts b/server/src/websocket/modules/removeFromQueue.ts index 67b6026..6410379 100644 --- a/server/src/websocket/modules/removeFromQueue.ts +++ b/server/src/websocket/modules/removeFromQueue.ts @@ -5,7 +5,7 @@ import { cancelDownload } from '../../main' const eventName = 'removeFromQueue' const cb = (data: any, __: any, ___: WsServer) => { - cancelDownload(data) + cancelDownload(data) consoleInfo(`Cancelled ${data}`) }