@gkalpak/cli-utils
Version:
A private collection of utilities for developing cli tools.
43 lines • 1.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.internalUtils = exports.InternalUtils = void 0;
class InternalUtils {
escapeSeqs = {
resetBold: '\u001b[0m',
showCursor: '\u001b[?25h',
};
escapeSeqRes = {
/* eslint-disable no-control-regex */
moveCursor: /\u001b\[\d+[a-d]/gi,
resetBold: /\u001b\[0m/gi,
showCursor: /\u001b\[\?25h/gi,
/* eslint-enable no-control-regex */
};
outputStyleResetSeqNames = ['resetBold', 'showCursor'];
async finallyAsPromised(promise, callback) {
return promise.then(val => Promise.resolve(callback()).then(() => val), err => Promise.resolve(callback()).then(() => Promise.reject(err)));
}
noop() {
return undefined;
}
onError(err) {
const { red } = require('chalk'); // eslint-disable-line @typescript-eslint/no-var-requires
const isExitCode = !!err && (typeof err === 'number');
const errorMsg = (err instanceof Error) ?
err.stack : isExitCode ?
`Exit code: ${err}` :
`Error: ${err}`;
console.error(red(errorMsg));
process.exit(isExitCode ? err : 1);
}
resetOutputStyle(stream) {
// Reset the output style (e.g. bold) and show the cursor.
this.outputStyleResetSeqNames.forEach(seqName => stream.write(this.escapeSeqs[seqName]));
}
stripOutputStyleResetSequences(str) {
return this.outputStyleResetSeqNames.reduce((aggr, seqName) => aggr.replace(this.escapeSeqRes[seqName], ''), str);
}
}
exports.InternalUtils = InternalUtils;
exports.internalUtils = new InternalUtils();
//# sourceMappingURL=internal-utils.js.map