diff options
Diffstat (limited to 'src/server/ActionUtilities.ts')
-rw-r--r-- | src/server/ActionUtilities.ts | 71 |
1 files changed, 36 insertions, 35 deletions
diff --git a/src/server/ActionUtilities.ts b/src/server/ActionUtilities.ts index 55b50cc12..6f5b9272a 100644 --- a/src/server/ActionUtilities.ts +++ b/src/server/ActionUtilities.ts @@ -1,14 +1,14 @@ import { exec } from 'child_process'; import { Color, yellow } from 'colors'; import { createWriteStream, exists, mkdir, readFile, unlink, writeFile } from 'fs'; -import * as nodemailer from "nodemailer"; -import { MailOptions } from "nodemailer/lib/json-transport"; +import * as nodemailer from 'nodemailer'; +import { MailOptions } from 'nodemailer/lib/json-transport'; import * as path from 'path'; -import { rimraf } from "rimraf"; +import { rimraf } from 'rimraf'; import { ExecOptions } from 'shelljs'; import * as Mail from 'nodemailer/lib/mailer'; -const projectRoot = path.resolve(__dirname, "../../"); +const projectRoot = path.resolve(__dirname, '../../'); export function pathFromRoot(relative?: string) { if (!relative) { return projectRoot; @@ -18,7 +18,7 @@ export function pathFromRoot(relative?: string) { export async function fileDescriptorFromStream(path: string) { const logStream = createWriteStream(path); - return new Promise<number>(resolve => logStream.on("open", resolve)); + return new Promise<number>(resolve => logStream.on('open', resolve)); } export const command_line = (command: string, fromDirectory?: string) => { @@ -27,25 +27,25 @@ export const command_line = (command: string, fromDirectory?: string) => { if (fromDirectory) { options.cwd = fromDirectory ? path.resolve(projectRoot, fromDirectory) : projectRoot; } - exec(command, options, (err, stdout) => err ? reject(err) : resolve(stdout)); + exec(command, options, (err, stdout) => (err ? reject(err) : resolve(stdout))); }); }; export const read_text_file = (relativePath: string) => { const target = path.resolve(__dirname, relativePath); return new Promise<string>((resolve, reject) => { - readFile(target, (err, data) => err ? reject(err) : resolve(data.toString())); + readFile(target, (err, data) => (err ? reject(err) : resolve(data.toString()))); }); }; export const write_text_file = (relativePath: string, contents: any) => { const target = path.resolve(__dirname, relativePath); return new Promise<void>((resolve, reject) => { - writeFile(target, contents, (err) => err ? reject(err) : resolve()); + writeFile(target, contents, err => (err ? reject(err) : resolve())); }); }; -export type Messager<T> = (outcome: { result: T | undefined, error: Error | null }) => string; +export type Messager<T> = (outcome: { result: T | undefined; error: Error | null }) => string; export interface LogData<T> { startMessage: string; @@ -56,22 +56,23 @@ export interface LogData<T> { } let current = Math.ceil(Math.random() * 20); -export async function log_execution<T>({ startMessage, endMessage, action, color }: LogData<T>): Promise<T | undefined> { - let result: T | undefined = undefined, error: Error | null = null; - const resolvedColor = color || `\x1b[${31 + ++current % 6}m%s\x1b[0m`; +export async function logExecution<T>({ startMessage, endMessage, action, color }: LogData<T>): Promise<T | undefined> { + let result: T | undefined = undefined, + error: Error | null = null; + const resolvedColor = color || `\x1b[${31 + (++current % 6)}m%s\x1b[0m`; log_helper(`${startMessage}...`, resolvedColor); try { result = await action(); } catch (e: any) { error = e; } finally { - log_helper(typeof endMessage === "string" ? endMessage : endMessage({ result, error }), resolvedColor); + log_helper(typeof endMessage === 'string' ? endMessage : endMessage({ result, error }), resolvedColor); } return result; } function log_helper(content: string, color: Color | string) { - if (typeof color === "string") { + if (typeof color === 'string') { console.log(color, content); } else { console.log(color(content)); @@ -88,11 +89,11 @@ export function msToTime(duration: number) { minutes = Math.floor((duration / (1000 * 60)) % 60), hours = Math.floor((duration / (1000 * 60 * 60)) % 24); - const hoursS = (hours < 10) ? "0" + hours : hours; - const minutesS = (minutes < 10) ? "0" + minutes : minutes; - const secondsS = (seconds < 10) ? "0" + seconds : seconds; + const hoursS = hours < 10 ? '0' + hours : hours; + const minutesS = minutes < 10 ? '0' + minutes : minutes; + const secondsS = seconds < 10 ? '0' + seconds : seconds; - return hoursS + ":" + minutesS + ":" + secondsS + "." + milliseconds; + return hoursS + ':' + minutesS + ':' + secondsS + '.' + milliseconds; } export const createIfNotExists = async (path: string) => { @@ -112,13 +113,12 @@ export async function Prune(rootDirectory: string): Promise<boolean> { export const Destroy = (mediaPath: string) => new Promise<boolean>(resolve => unlink(mediaPath, error => resolve(error === null))); export namespace Email { - const smtpTransport = nodemailer.createTransport({ service: 'Gmail', auth: { user: 'browndashptc@gmail.com', - pass: 'TsarNicholas#2' - } + pass: 'TsarNicholas#2', + }, }); export interface DispatchOptions<T extends string | string[]> { @@ -135,16 +135,18 @@ export namespace Email { export async function dispatchAll({ to, subject, content, attachments }: DispatchOptions<string[]>) { const failures: DispatchFailure[] = []; - await Promise.all(to.map(async recipient => { - let error: Error | null; - const resolved = attachments ? "length" in attachments ? attachments : [attachments] : undefined; - if ((error = await Email.dispatch({ to: recipient, subject, content, attachments: resolved })) !== null) { - failures.push({ - recipient, - error - }); - } - })); + await Promise.all( + to.map(async recipient => { + let error: Error | null; + const resolved = attachments ? ('length' in attachments ? attachments : [attachments]) : undefined; + if ((error = await Email.dispatch({ to: recipient, subject, content, attachments: resolved })) !== null) { + failures.push({ + recipient, + error, + }); + } + }) + ); return failures.length ? failures : undefined; } @@ -153,10 +155,9 @@ export namespace Email { to, from: 'browndashptc@gmail.com', subject, - text: `Hello ${to.split("@")[0]},\n\n${content}`, - attachments + text: `Hello ${to.split('@')[0]},\n\n${content}`, + attachments, } as MailOptions; return new Promise<Error | null>(resolve => smtpTransport.sendMail(mailOptions, resolve)); } - -}
\ No newline at end of file +} |