aws-cli-util-logger
Version:
Simple logging library for use with AWS CLI utilities
133 lines (132 loc) • 4.96 kB
JavaScript
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 });
exports.AwsCliUtilLogger = void 0;
const chalk_1 = __importDefault(require("chalk"));
const os_1 = __importDefault(require("os"));
const util_1 = require("util");
const child_process_1 = require("child_process");
class AwsCliUtilLogger {
constructor({ packageName, disabled, verbose, binCommand, debugFlag = '--debug', }) {
this.handleError = (e, debug = false) => {
this.enable();
if (e instanceof Error) {
this.error(e.message);
if (e.stack && debug) {
this.log(e.stack);
}
}
else {
this.error('An unknown error has occured');
}
if (!debug) {
this.info(chalk_1.default.dim(`Run ${this.binCommand || 'the command again'} with ${chalk_1.default.dim(this.debugFlag)} flag for more details.`));
}
};
this.name = packageName;
this.binCommand = binCommand;
this.debugFlag = debugFlag;
this.disabled = disabled !== null && disabled !== void 0 ? disabled : false;
this.LOG_PREFIX = `[${this.name}]:`;
this.SEPARATOR = ', ';
this.verbose = verbose !== null && verbose !== void 0 ? verbose : false;
}
success(...messages) {
this.print({ color: 'green', messages });
}
info(...messages) {
this.print({ color: 'cyan', messages });
}
warn(...messages) {
this.print({ color: 'yellow', messages });
}
error(...messages) {
this.print({ color: 'red', messages, error: true });
}
debug(...messages) {
if (this.verbose) {
this.print({ color: 'gray', messages });
}
}
log(...messages) {
this.print({ color: 'white', messages });
}
setVerbose(level) {
this.verbose = level;
}
isVerbose() {
return this.verbose;
}
disable() {
this.disabled = true;
}
enable() {
this.disabled = false;
}
logSystemInfo(profile) {
return __awaiter(this, void 0, void 0, function* () {
const [cliVersion, profileConfig] = yield Promise.all([
this.getCliVersion(),
this.getCliConfig(profile),
]);
this.debug('===========');
this.debug('SYSTEM INFO');
this.debug('===========');
this.debug(`AWS CLI Version ${cliVersion}`);
this.debug(`OS ${os_1.default.platform()} ${os_1.default.release()}`);
this.debug(`Node ${process.version}`);
this.debug('==============');
this.debug('PROFILE CONFIG');
this.debug('==============');
this.debug(profileConfig);
});
}
formatMessages(messages) {
return messages.join(this.SEPARATOR);
}
print({ color, messages, error = false }) {
if (!this.disabled) {
console[error ? 'error' : 'log'](`${chalk_1.default[color].bold(this.LOG_PREFIX, this.formatMessages(messages))}`);
}
}
getCliVersion() {
return __awaiter(this, void 0, void 0, function* () {
const pexec = (0, util_1.promisify)(child_process_1.exec);
let version = '';
try {
const { stdout = '' } = yield pexec('aws --version');
version = stdout.replace('\n', '');
}
catch (e) {
version = 'NOT FOUND';
}
return version;
});
}
getCliConfig(profile = 'default') {
return __awaiter(this, void 0, void 0, function* () {
const pexec = (0, util_1.promisify)(child_process_1.exec);
let config = '';
try {
const { stdout = '' } = yield pexec(`aws configure list --profile ${profile}`);
config += `\n${stdout}`;
}
catch (e) {
config = 'NOT FOUND';
}
return config;
});
}
}
exports.AwsCliUtilLogger = AwsCliUtilLogger;
;