UNPKG

2.51 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const events_1 = require("events");
12const Logger_1 = require("./Logger");
13const ProcessArgs_1 = require("./ProcessArgs");
14class Console extends events_1.EventEmitter {
15 constructor(options, args) {
16 super();
17 this.options = options;
18 this.args = args;
19 this.logger = Logger_1.Logger;
20 this.shutdown = (resolve, reject, reason) => __awaiter(this, void 0, void 0, function* () {
21 this.logger.info('[SHUTDOWN]', process.pid, process.exitCode, reason);
22 if (process.exitCode && process.exitCode !== 0) {
23 reject(new Error(`${process.exitCode}: ${reason}`));
24 }
25 else {
26 resolve();
27 }
28 });
29 }
30 static create(options, args) {
31 return new Console(options, args || ProcessArgs_1.ProcessArgs.from(process.argv));
32 }
33 static run(options, args) {
34 return Console.create(options, args).start();
35 }
36 start() {
37 return __awaiter(this, void 0, void 0, function* () {
38 if (this.instance === undefined) {
39 this.logger.info(`starting "${this.args.exe}":`, ...this.args.normalized);
40 return (this.instance = new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
41 process.on('uncaughtException', () => this.shutdown(resolve, reject, 'uncaught-exception'));
42 process.on('exit', () => this.shutdown(resolve, reject, 'exit'));
43 if (this.options.initializer) {
44 yield this.options.initializer(this, ...this.args.normalized);
45 }
46 })));
47 }
48 return this.instance;
49 });
50 }
51 stop() {
52 process.exit(0);
53 }
54}
55exports.Console = Console;
56//# sourceMappingURL=Console.js.map
\No newline at end of file