import * as childProcess from "child_process"; import * as fs from "fs"; import * as path from "path"; export function exists(filePath: string): boolean { try { return fs.existsSync(filePath); } catch (ignore) { return false; } } export function readFile(filePath: string): string { const _filePath = resolveFilePath(filePath); return fs.readFileSync(_filePath, "utf8"); } export const resolveFilePath = (filePath: string): string => { if (filePath.length === 0) { return filePath; } if (filePath[0] === "~") { return path.join(process.env.HOME!, filePath.slice(1)); } return filePath; } export function readStdin(): Promise { return new Promise((resolve): void => { let content = ""; process.stdin.resume(); process.stdin.on("data", (buf) => { content += buf.toString("utf8"); }); process.stdin.on("end", () => resolve(content)); }); } export function writeFile(filePath: string, content: string): void { return fs.writeFileSync(filePath, content); } export function appendFileSync(filePath: string, content: string): void { return fs.appendFileSync(filePath, content); } export function execute(cmd: string): string { return childProcess.execSync(cmd).toString("utf8"); } export function removeFile(filePath: string): void { if (fs.existsSync(filePath)) { return fs.unlinkSync(filePath); } }