"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HotIO = void 0;
const fs = __importStar(require("fs"));
const crypto_1 = __importDefault(require("crypto"));
const ppath = __importStar(require("path"));
const fse = __importStar(require("fs-extra"));
const yaml = __importStar(require("yaml"));
const util = require("util");
const child_process = __importStar(require("child_process"));
const asyncExec = util.promisify(child_process.exec);
/**
 * Handles IO for the server.
 *
 * @todo Add tests for all these functions!
 */
class HotIO {
    /**
     * Read a text file.
     */
    static readTextFile(path) {
        return __awaiter(this, void 0, void 0, function* () {
            return (new Promise((resolve, reject) => {
                fs.readFile(path, (err, data) => {
                    if (err != null)
                        throw err;
                    let content = data.toString();
                    resolve(content);
                });
            }));
        });
    }
    /**
     * Write a file stream.
     */
    static writeFileStream(path, stream) {
        let writeStream = fs.createWriteStream(path);
        stream.pipe(writeStream);
        return (writeStream);
    }
    /**
     * Read a file and create a stream from it.
     */
    static readFileStream(path) {
        let stream = fs.createReadStream(path);
        return (stream);
    }
    /**
     * Read a text file, parse it, and return the parsed JSON.
     */
    static readJSONFile(path) {
        return __awaiter(this, void 0, void 0, function* () {
            const textStr = yield HotIO.readTextFile(path);
            const jsonObj = JSON.parse(textStr);
            return (jsonObj);
        });
    }
    /**
     * Read a YAML file, parse it, and return the parsed object.
     */
    static readYAMLFile(path) {
        return __awaiter(this, void 0, void 0, function* () {
            const textStr = yield HotIO.readTextFile(path);
            const jsonObj = yaml.parse(textStr);
            return (jsonObj);
        });
    }
    /**
     * Get the SHA256 hash of a file.
     */
    static sha256File(path) {
        return __awaiter(this, void 0, void 0, function* () {
            return (new Promise((resolve, reject) => {
                const input = HotIO.readFileStream(path);
                const hash = crypto_1.default.createHash("sha256");
                input.on("readable", () => {
                    const data = input.read();
                    if (data)
                        hash.update(data);
                    else {
                        const hashStr = hash.digest("hex");
                        resolve(hashStr);
                    }
                });
                input.on("error", (err) => {
                    reject(err);
                });
            }));
        });
    }
    /**
     * Write to a text file.
     */
    static writeTextFile(path, content) {
        return __awaiter(this, void 0, void 0, function* () {
            return (new Promise((resolve, reject) => {
                fs.writeFile(path, content, (err) => {
                    if (err != null)
                        throw err;
                    resolve();
                });
            }));
        });
    }
    /**
     * Make a directory recursively.
     */
    static mkdir(path_1) {
        return __awaiter(this, arguments, void 0, function* (path, options = { "recursive": true }) {
            return (new Promise((resolve, reject) => {
                fs.mkdir(path, options, (err, path) => {
                    if (err != null)
                        throw err;
                    resolve();
                });
            }));
        });
    }
    /**
     * Delete a file.
     */
    static rm(path_1) {
        return __awaiter(this, arguments, void 0, function* (path, options = { "force": false, maxRetries: 0, recursive: false, retryDelay: 100 }) {
            return (new Promise((resolve, reject) => {
                fs.rm(path, options, (err) => {
                    if (err != null)
                        throw err;
                    resolve();
                });
            }));
        });
    }
    /**
     * Copy files to a location.
     */
    static copyFiles(src_1, dest_1) {
        return __awaiter(this, arguments, void 0, function* (src, dest, options = undefined) {
            return (fse.copy(src, dest, options));
        });
    }
    /**
     * Copy a file to a location.
     */
    static copyFile(src, dest, flags) {
        return __awaiter(this, void 0, void 0, function* () {
            return (fse.copyFile(src, dest, flags));
        });
    }
    /**
     * Move a file to a location.
     */
    static moveFile(src_1, dest_1) {
        return __awaiter(this, arguments, void 0, function* (src, dest, options = { overwrite: false }) {
            return (fse.move(src, dest, options));
        });
    }
    /**
     * Checks if a file is at a location.
     */
    static exists(path) {
        return __awaiter(this, void 0, void 0, function* () {
            return (new Promise((resolve, reject) => {
                fs.stat(path, (err, stats) => {
                    if (err != null) {
                        if (err.code === "ENOENT")
                            resolve(false);
                        else
                            throw err;
                    }
                    resolve(true);
                });
            }));
        });
    }
    /**
     * List files at a location.
     */
    static listFiles(path) {
        return __awaiter(this, void 0, void 0, function* () {
            return (new Promise((resolve, reject) => {
                fs.readdir(path, (err, files) => {
                    if (err != null)
                        throw err;
                    resolve(files);
                });
            }));
        });
    }
    /**
     * Normalize a file path.
     */
    static normalizePath(path) {
        return (ppath.normalize(path));
    }
    /**
     * Execute a command.
     */
    static exec(cmd) {
        return __awaiter(this, void 0, void 0, function* () {
            let output = yield asyncExec(cmd);
            return (output.stdout);
        });
    }
    /**
     * Execute a command as a grabbable process.
     */
    static spawn(cmd, args) {
        let output = child_process.spawn(cmd, args);
        return (output);
    }
}
exports.HotIO = HotIO;
//# sourceMappingURL=HotIO.js.map