From 350e5c021de91690e54ef06b45bf57c5eebc9b88 Mon Sep 17 00:00:00 2001 From: Bsian Date: Sat, 4 Jan 2020 13:47:54 +0000 Subject: [PATCH] Use Promise.allSettled --- src/commands/parseall.ts | 45 ++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/commands/parseall.ts b/src/commands/parseall.ts index bd2fe86..755b981 100644 --- a/src/commands/parseall.ts +++ b/src/commands/parseall.ts @@ -4,7 +4,7 @@ import { readdirSync } from 'fs'; import moment from 'moment'; import { Client } from '..'; import { Command, RichEmbed } from '../class'; -import { parseCertificate } from '../functions'; +import { parseCertificate, Certificate } from '../functions'; export default class Parseall extends Command { constructor(client: Client) { @@ -36,30 +36,31 @@ export default class Parseall extends Command { } return parseCertificate(this.client, `${a.homepath}/Validation/${certFile}`); }); + // @ts-ignore + const parsed: {status: 'fulfilled', value: Certificate}[] | {status: 'rejected', reason: Error}[] = await Promise.allSettled(certificates); const final = search.map(async (a) => { - try { - const parsed = await Promise.all(certificates); - const { notAfter } = parsed[search.findIndex((acc) => acc === a)]; - // @ts-ignore - const timeObject: {years: number, months: number, days: number, hours: number, minutes: number, seconds: number, firstDateWasLater: boolean} = moment.preciseDiff(new Date(), notAfter, true); - const precise: [number, string][] = []; - // @ts-ignore - const timeArray: number[] = Object.values(timeObject).filter((v) => typeof v === 'number'); - timeArray.forEach((t) => { // eslint-disable-line - const index = timeArray.indexOf(t); - const measurements = ['yr', 'mo', 'd', 'h', 'm', 's']; - precise.push([t, measurements[index]]); - }); - const time = precise.filter((n) => n[0]).map(((v) => v.join(''))).join(', '); - - if (notAfter < new Date()) return `${this.client.stores.emojis.error} **${a.username}** Expired ${time} ago`; - return `${this.client.stores.emojis.success} **${a.username}** Expires in ${time}`; - } catch (error) { - if (error.message.includes('no such file or directory') || error.message.includes('File doesn\'t exist.')) return `${this.client.stores.emojis.error} **${a.username}** Unable to locate certificate`; - if (error.message.includes('panic: Certificate PEM Encode == nil')) return `${this.client.stores.emojis.error} ** ${a.username}** Invalid certificate`; - throw error; + const result = parsed[search.findIndex((acc) => acc === a)]; + if (result.status === 'rejected') { + if (result.reason.message.includes('no such file or directory') || result.reason.message.includes('File doesn\'t exist.')) return `${this.client.stores.emojis.error} **${a.username}** Unable to locate certificate`; + if (result.reason.message.includes('panic: Certificate PEM Encode == nil')) return `${this.client.stores.emojis.error} ** ${a.username}** Invalid certificate`; + throw result.reason; } + const { notAfter } = result.value; + // @ts-ignore + const timeObject: {years: number, months: number, days: number, hours: number, minutes: number, seconds: number, firstDateWasLater: boolean} = moment.preciseDiff(new Date(), notAfter, true); + const precise: [number, string][] = []; + // @ts-ignore + const timeArray: number[] = Object.values(timeObject).filter((v) => typeof v === 'number'); + timeArray.forEach((t) => { // eslint-disable-line + const index = timeArray.indexOf(t); + const measurements = ['yr', 'mo', 'd', 'h', 'm', 's']; + precise.push([t, measurements[index]]); + }); + const time = precise.filter((n) => n[0]).map(((v) => v.join(''))).join(', '); + + if (notAfter < new Date()) return `${this.client.stores.emojis.error} **${a.username}** Expired ${time} ago`; + return `${this.client.stores.emojis.success} **${a.username}** Expires in ${time}`; }); if (final.join('\n').length < 2048) embed.setDescription(final.join('\n'));