"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fs = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const textFileOptions = { encoding: "utf8" };
async function testPath(at, fn) {
    try {
        const st = await fs_1.promises.stat(at);
        return fn(st);
    }
    catch (e) {
        return false;
    }
}
function fileExists(at) {
    return testPath(at, st => st.isFile());
}
function folderExists(at) {
    return testPath(at, st => st.isDirectory());
}
async function readTextFile(at) {
    return (await fs_1.promises.readFile(at, textFileOptions));
}
async function writeTextFile(at, contents) {
    const folder = path_1.default.dirname(at);
    if (!(await folderExists(folder))) {
        await fs_1.promises.mkdir(folder);
    }
    await fs_1.promises.writeFile(at, contents, textFileOptions);
}
exports.fs = {
    ...fs_1.promises,
    fileExists,
    folderExists,
    readTextFile,
    writeTextFile
};
