"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const child = require("child_process");
const options_1 = require("./options");
const activelogger_1 = require("@activeledger/activelogger");
class ActiveDataStore {
    constructor() {
        this.dsLocation = options_1.ActiveOptions.get("db", {}).selfhost.dir || "./.ds";
        if (!fs.existsSync(this.dsLocation)) {
            fs.mkdirSync(this.dsLocation);
        }
        activelogger_1.ActiveLogger.info("Self-hosted data engine @ http://127.0.0.1:" +
            options_1.ActiveOptions.get("db", {}).selfhost.port);
    }
    launch() {
        this.process = child.spawn("node", [
            __dirname + "/selfhost.js",
            this.dsLocation,
            `${options_1.ActiveOptions.get("db", {}).selfhost.port}`
        ], {
            stdio: "inherit"
        });
        activelogger_1.ActiveLogger.info(`Self-hosted data engine : Starting Up (${this.process.pid})`);
        this.process.on("exit", (code, signal) => {
            activelogger_1.ActiveLogger.error(`Self-hosted data engine has shutdown (${code} : ${signal ||
                "No Signal"})`);
            this.launch();
        });
        return "http://127.0.0.1:" + options_1.ActiveOptions.get("db", {}).selfhost.port;
    }
}
exports.ActiveDataStore = ActiveDataStore;
//# sourceMappingURL=datastore.js.map