Alice Gaudon
6aa37eb9e4
Reorganize views into new "assets" folder structure
Turn locals into a store so locals don't have to be passed through files that don't need them
Some fixes to previous commit (esm) 82ab0b963c
Remove afs in favor of fs.promises (renamed afs.exists to Utils.doesFileExist
Rename Utils.readdirRecursively to Utils.listFilesRecursively
79 lines
2.0 KiB
TypeScript
79 lines
2.0 KiB
TypeScript
import {promises as fs} from "fs";
|
|
import path from "path";
|
|
|
|
export async function sleep(ms: number): Promise<void> {
|
|
return await new Promise(resolve => {
|
|
setTimeout(() => resolve(), ms);
|
|
});
|
|
}
|
|
|
|
export abstract class WrappingError extends Error {
|
|
public readonly cause?: Error;
|
|
|
|
protected constructor(message: string, cause?: Error) {
|
|
super(message);
|
|
this.cause = cause;
|
|
|
|
if (cause !== undefined) {
|
|
this.stack = (this.stack || '') + `\n> Caused by: ${cause.stack}`;
|
|
}
|
|
}
|
|
|
|
public get name(): string {
|
|
return this.constructor.name;
|
|
}
|
|
}
|
|
|
|
export type Type<T> = { new(...args: never[]): T };
|
|
|
|
export function bufferToUuid(buffer: Buffer): string {
|
|
const chars = buffer.toString('hex');
|
|
let out = '';
|
|
let i = 0;
|
|
for (const l of [8, 4, 4, 4, 12]) {
|
|
if (i > 0) out += '-';
|
|
out += chars.substr(i, l);
|
|
i += l;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export function getMethods<T extends { [p: string]: unknown }>(obj: T): string[] {
|
|
const properties = new Set<string>();
|
|
let currentObj: T | unknown = obj;
|
|
do {
|
|
Object.getOwnPropertyNames(currentObj).map(item => properties.add(item));
|
|
currentObj = Object.getPrototypeOf(currentObj);
|
|
} while (currentObj);
|
|
return [...properties.keys()].filter(item => typeof obj[item] === 'function');
|
|
}
|
|
|
|
export async function listFilesRecursively(dir: string): Promise<string[]> {
|
|
const localFiles = await fs.readdir(dir);
|
|
const files: string[] = [];
|
|
for (const file of localFiles.map(file => path.join(dir, file))) {
|
|
const stat = await fs.stat(file);
|
|
|
|
if (stat.isDirectory()) {
|
|
files.push(...await listFilesRecursively(file));
|
|
} else {
|
|
files.push(file);
|
|
}
|
|
}
|
|
return files;
|
|
}
|
|
|
|
|
|
export async function doesFileExist(file: string): Promise<boolean> {
|
|
try {
|
|
await fs.stat(file);
|
|
} catch (err) {
|
|
if (err.code === 'ENOENT') {
|
|
return false;
|
|
} else {
|
|
throw err;
|
|
}
|
|
}
|
|
return true;
|
|
}
|