firmament-yargs
Version:
Typescript classes for building CLI node applications
152 lines • 5.61 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const inversify_1 = require("inversify");
const path = require("path");
const force_error_impl_1 = require("./force-error-impl");
const blackHoleStream = new (require('black-hole-stream'))();
const fs = require('fs');
let CommandUtilImpl = class CommandUtilImpl extends force_error_impl_1.ForceErrorImpl {
constructor(postal) {
super();
this.postal = postal;
this._console = {
log: this.stdoutLog.bind(this),
info: this.stdoutLog.bind(this),
warn: this.stdoutLog.bind(this),
error: this.stderrLog.bind(this)
};
this.registerProcessManagementEvents();
const cacheStdoutWrite = process.stdout.write;
const cacheStderrWrite = process.stderr.write;
this.postal.subscribe({
channel: 'CommandUtil',
topic: 'SuppressConsoleOutput',
callback: (data) => {
if (data.suppressConsoleOutput) {
process.stdout.write = process.stderr.write = blackHoleStream.write.bind(blackHoleStream);
}
else {
process.stdout.write = cacheStdoutWrite;
process.stderr.write = cacheStderrWrite;
}
}
});
this.postal.subscribe({
channel: 'CommandUtil',
topic: 'ProgressBarStarted',
callback: (data) => {
let dataConsole = data.console;
this._console.log = dataConsole.log;
this._console.info = dataConsole.info;
this._console.warn = dataConsole.warn;
this._console.error = dataConsole.error;
}
});
}
exitHandler(options, err) {
if (options.cleanup) {
}
if (err) {
if (err && err.message) {
this.stderrLog(err.message);
}
}
if (options.exit) {
process.exit();
}
}
registerProcessManagementEvents() {
process.stdin.resume();
process.on('exit', this.exitHandler.bind(this, { cleanup: true }));
process.on('SIGINT', this.exitHandler.bind(this, { exit: true }));
process.on('uncaughtException', this.exitHandler.bind(this, { exit: true }));
}
stderrWrite(msg) {
process.stderr.write(msg);
}
stdoutWrite(msg) {
process.stdout.write(msg);
}
stderrLog(msg) {
this.stderrWrite(`${msg}\n`);
}
stdoutLog(msg) {
this.stdoutWrite(`${msg}\n`);
}
returnErrorStringOrMessage(err, message) {
let errorMessage = this.logError(err, false);
return errorMessage.length ? errorMessage : message;
}
error(msg) {
this._console.error(msg);
}
log(msg) {
this._console.log(msg);
}
logErrors(errs, writeErrorToConsole = true) {
let retVal = [];
errs.forEach(err => {
retVal.push(this.logError(err, writeErrorToConsole));
});
return retVal;
}
logError(err, writeErrorToConsole = true) {
if (err) {
if (writeErrorToConsole) {
this._console.error(err.message);
}
return err.message;
}
return '';
}
processExitIfError(err) {
if (err) {
this.processExitWithError(err);
}
}
processExitWithError(err, nonErrorMessage = null) {
this.processExit(!!err ? 1 : 0, !!err ? err.message : !!nonErrorMessage ? nonErrorMessage : '');
}
processExit(exitCode = 0, msg = null) {
this._console.log(!!msg ? msg : '');
process.exit(exitCode);
}
callbackIfError(cb, err = null, result = null) {
cb = this.checkCallback(cb);
err && cb(err, result);
return !!err;
}
logAndCallback(msg, cb, err = null, result = null) {
this._console.log(err ? err.message : msg);
cb = this.checkCallback(cb);
cb(err, result);
return !!err;
}
getConfigFilePath(filename, extension = '.json') {
let regex = new RegExp('(.*)\\' + extension + '$', 'i');
let cwd = process.cwd();
filename = regex.test(filename)
? filename.replace(regex, '$1' + extension)
: filename + extension;
return path.resolve(cwd, filename);
}
};
CommandUtilImpl = __decorate([
inversify_1.injectable(),
__param(0, inversify_1.inject('IPostal')),
__metadata("design:paramtypes", [Object])
], CommandUtilImpl);
exports.CommandUtilImpl = CommandUtilImpl;
//# sourceMappingURL=command-util-impl.js.map