'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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const YAML = __importStar(require("yaml"));
const hdbTerms = __importStar(require("../utility/hdbTerms.js"));
const harper_logger_ts_1 = __importDefault(require("../utility/logging/harper_logger.js"));
const systemInformation = __importStar(require("../utility/environment/systemInformation.js"));
const envMgr = __importStar(require("../utility/environment/environmentManager.js"));
const installation = __importStar(require("../utility/installation.js"));
envMgr.initSync();
const STATUSES = {
    RUNNING: 'running',
    STOPPED: 'stopped',
    ERRORED: 'errored',
    NOT_INSTALLED: 'not installed',
};
let hdbRoot;
exports.default = status;
async function status() {
    let status = {
        harperdb: {
            status: STATUSES.STOPPED,
        },
    };
    if (!installation.isHdbInstalled(envMgr, harper_logger_ts_1.default)) {
        status.harperdb.status = STATUSES.NOT_INSTALLED;
        console.log(YAML.stringify(status));
        return;
    }
    hdbRoot = envMgr.get(hdbTerms.CONFIG_PARAMS.ROOTPATH);
    let hdbPid;
    try {
        hdbPid = Number.parseInt(await fs.readFile(path.join(hdbRoot, hdbTerms.HDB_PID_FILE), 'utf8'));
    }
    catch (err) {
        if (err.code === hdbTerms.NODE_ERROR_CODES.ENOENT) {
            harper_logger_ts_1.default.info('`harperdb status` did not find a hdb.pid file');
            status.harperdb.status = STATUSES.STOPPED;
            console.log(YAML.stringify(status));
            return;
        }
        throw err;
    }
    // Check the saved pid against any running hdb processes
    const hdbSysInfo = await systemInformation.getHDBProcessInfo();
    for (const proc of hdbSysInfo.core) {
        if (proc.pid === hdbPid) {
            status.harperdb.status = STATUSES.RUNNING;
            status.harperdb.pid = hdbPid;
            break;
        }
    }
    console.log(YAML.stringify(status));
    process.exit();
}
//# sourceMappingURL=status.js.map