envio
Version:
A latency and sync speed optimized, developer friendly blockchain data indexer.
97 lines (89 loc) • 3.06 kB
JavaScript
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Core from "./Core.res.mjs";
import * as Main from "./Main.res.mjs";
import * as Utils from "./Utils.res.mjs";
import * as Config from "./Config.res.mjs";
import * as Logging from "./Logging.res.mjs";
import * as Process from "process";
import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
import * as Stdlib_JSON from "@rescript/runtime/lib/es6/Stdlib_JSON.js";
import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
let setEnvVar = ((k, v) => { process.env[k] = v; });
process.on("unhandledRejection", reason => {
Logging.errorWithExn(Utils.prettifyExn(reason), "Unhandled promise rejection");
Process.exit(1);
});
function decodeCommand(json) {
let o = Stdlib_JSON.Decode.object(json);
let obj = o !== undefined ? o : Stdlib_JsError.throwWithMessage("Invalid command payload: not an object");
let k = Stdlib_Option.flatMap(obj["kind"], Stdlib_JSON.Decode.string);
let kind = k !== undefined ? k : Stdlib_JsError.throwWithMessage("Invalid command payload: missing kind");
switch (kind) {
case "drop-schema" :
return {
TAG: "DropSchema",
_0: json
};
case "migrate" :
return {
TAG: "Migrate",
_0: json
};
case "start" :
return {
TAG: "Start",
_0: json
};
default:
return Stdlib_JsError.throwWithMessage(`Unknown command kind: ` + kind);
}
}
function applyEnv(env) {
Stdlib_Dict.forEachWithKey(env, (value, key) => {
let v = Stdlib_JSON.Decode.string(value);
if (v !== undefined) {
return setEnvVar(key, v);
}
});
}
async function run(args) {
try {
let json = await Core.runCli(args);
if (json === null) {
return;
}
let match = decodeCommand(JSON.parse(json));
switch (match.TAG) {
case "Start" :
let match$1 = match._0;
Config.prime(match$1.config);
process.chdir(match$1.cwd);
applyEnv(match$1.env);
return await Main.start(undefined, match$1.reset, undefined, undefined, undefined);
case "Migrate" :
let match$2 = match._0;
Config.prime(match$2.config);
return await Main.migrate(match$2.reset);
case "DropSchema" :
Config.prime(match._0.config);
return await Main.dropSchema();
}
} catch (raw_exn) {
let exn = Primitive_exceptions.internalToException(raw_exn);
let e = Primitive_exceptions.internalToException(exn);
let message = e.RE_EXN_ID === "JsExn" ? Stdlib_Option.getOr(Stdlib_JsExn.message(e._1), "Failed at initialization") : "Failed at initialization";
Logging.error(message);
Process.exit(1);
return;
}
}
export {
setEnvVar,
decodeCommand,
applyEnv,
run,
}
/* Not a pure module */