1
0
Fork 0

Use Promise.allSettled

refactor/models
Bsian 2020-01-04 13:47:54 +00:00
parent 2e1bf68cc0
commit 350e5c021d
No known key found for this signature in database
GPG Key ID: 097FB9A291026091
1 changed files with 23 additions and 22 deletions

View File

@ -4,7 +4,7 @@ import { readdirSync } from 'fs';
import moment from 'moment'; import moment from 'moment';
import { Client } from '..'; import { Client } from '..';
import { Command, RichEmbed } from '../class'; import { Command, RichEmbed } from '../class';
import { parseCertificate } from '../functions'; import { parseCertificate, Certificate } from '../functions';
export default class Parseall extends Command { export default class Parseall extends Command {
constructor(client: Client) { constructor(client: Client) {
@ -36,30 +36,31 @@ export default class Parseall extends Command {
} }
return parseCertificate(this.client, `${a.homepath}/Validation/${certFile}`); 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) => { const final = search.map(async (a) => {
try { const result = parsed[search.findIndex((acc) => acc === a)];
const parsed = await Promise.all(certificates); if (result.status === 'rejected') {
const { notAfter } = parsed[search.findIndex((acc) => acc === a)]; 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`;
// @ts-ignore if (result.reason.message.includes('panic: Certificate PEM Encode == nil')) return `${this.client.stores.emojis.error} ** ${a.username}** Invalid certificate`;
const timeObject: {years: number, months: number, days: number, hours: number, minutes: number, seconds: number, firstDateWasLater: boolean} = moment.preciseDiff(new Date(), notAfter, true); throw result.reason;
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 { 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')); if (final.join('\n').length < 2048) embed.setDescription(final.join('\n'));