/** * * Agora Real Time Engagement * Created by Wei Hu in 2022-05. * Copyright (c) 2015 Agora IO. All rights reserved. * */ import * as fs from 'fs'; import * as path from 'path'; export async function fileExists(path: string): Promise { try { await fs.promises.access(path, fs.constants.F_OK); return true; } catch { return false; } } export function fileExistsSync(path: string): boolean { try { fs.accessSync(path, fs.constants.F_OK); return true; } catch { return false; } } export async function realpath(path: string): Promise { return new Promise((resolve) => { fs.realpath( path, (_err: NodeJS.ErrnoException | null, resolvedPath: string) => { resolve(resolvedPath); }, ); }); } export async function readFile( filePath: string, encoding: BufferEncoding = 'utf-8', ): Promise { return fs.promises.readFile(filePath, encoding); } export async function writeFile( filePath: string, content: string, encoding: BufferEncoding = 'utf-8', ): Promise { return fs.promises.writeFile(filePath, content, encoding); } export async function mkdir(filePath: string): Promise { return fs.promises.mkdir(filePath); } export function callsites(): { getFileName(): string | null }[] { const _prepareStackTrace = Error.prepareStackTrace; Error.prepareStackTrace = (_, stack): NodeJS.CallSite[] => stack; const stack_ = new Error().stack as unknown as NodeJS.CallSite[]; const stack = stack_.slice(1); Error.prepareStackTrace = _prepareStackTrace; return stack; } export function dirName(): string | null { const callsite = callsites()[1]; const fileName = callsite ? callsite.getFileName() : undefined; if (fileName) { const filePath = path.dirname(fileName); return filePath; } return null; }