"use strict";
/**
 * This file is used to automatically bootstrap a NodeCG Server instance.
 * It exports nothing and offers no controls.
 *
 * At this time, other means of starting NodeCG are not officially supported,
 * but they are used internally by our tests.
 *
 * Tests directly instantiate the NodeCGServer class, so that they may have full control
 * over its lifecycle and when the process exits.
 */
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 });
// Packages
const semver_1 = __importDefault(require("semver"));
const node_fetch_commonjs_1 = __importDefault(require("node-fetch-commonjs"));
// Ours
const rootPath_1 = __importDefault(require("../shared/utils/rootPath"));
const cwd = process.cwd();
if (cwd !== rootPath_1.default.path) {
    console.warn('[nodecg] process.cwd is %s, expected %s', cwd, rootPath_1.default.path);
    process.chdir(rootPath_1.default.path);
    console.info('[nodecg] Changed process.cwd to %s', rootPath_1.default.path);
}
if (!process.env.NODECG_ROOT) {
    // This must happen before we import any of our other application code.
    process.env.NODECG_ROOT = process.cwd();
}
// Ours
const util_1 = require("./util");
const server_1 = __importDefault(require("./server"));
const exit_hook_1 = __importDefault(require("./util/exit-hook"));
process.title = 'NodeCG';
process.title += ` - ${String(util_1.pjson.version)}`;
const server = new server_1.default();
void server.start();
(0, exit_hook_1.default)(() => {
    server.stop();
});
(0, node_fetch_commonjs_1.default)('https://registry.npmjs.org/nodecg/latest')
    .then((res) => res.json())
    .then((body) => {
    if (!body || typeof body !== 'object' || !('version' in body) || typeof body.version !== 'string') {
        return;
    }
    if (semver_1.default.gt(body.version, util_1.pjson.version)) {
        console.warn('An update is available for NodeCG: %s (current: %s)', body.version, util_1.pjson.version);
    }
})
    .catch((error) => {
    console.error('Failed to check for updates', error);
});
const hoge = () => __awaiter(void 0, void 0, void 0, function* () {
    yield new Promise((resolve) => setTimeout(resolve, 1000));
    throw new Error('hoge');
});
void hoge();
//# sourceMappingURL=bootstrap.js.map