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