@mail-core/cli
Version:
Инструментарий для написания cli-скриптов
81 lines • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.startProcessSummary = exports.addProcessSummary = void 0;
const os_1 = require("os");
const fs_1 = require("fs");
const path_1 = require("path");
const id_1 = require("./id");
const boxen = require("boxen");
const chalk_1 = require("chalk");
const SUMMARY_FILE = path_1.join(os_1.tmpdir(), '.mail-core-cli.summary.json');
const STYLE_BY_TYPE = {
'info': {
boxen: {
margin: 1,
padding: { left: 4, right: 4, top: 1, bottom: 1 },
borderStyle: 'double',
borderColor: 'green',
},
},
'warn': {
boxen: {
margin: 1,
padding: { left: 4, right: 4, top: 1, bottom: 1 },
borderStyle: 'single',
borderColor: 'yellow',
},
},
'error': {
boxen: {
margin: 1,
padding: { left: 4, right: 4, top: 1, bottom: 1 },
borderStyle: 'singleDouble',
borderColor: 'red',
},
},
};
function addProcessSummary(title, detail = '', type = 'info') {
const item = Object.assign(Object.assign({}, (typeof title !== 'string' ? title : { type, title, detail })), { pid: id_1.PROCESS_PID });
fs_1.appendFileSync(SUMMARY_FILE, `${JSON.stringify(item)}\n`);
}
exports.addProcessSummary = addProcessSummary;
function startProcessSummary() {
if (!fs_1.existsSync(SUMMARY_FILE)) {
fs_1.writeFileSync(SUMMARY_FILE, `${id_1.PROCESS_PID}\n`);
}
// Вывод Summary
return () => {
const rows = fs_1.readFileSync(SUMMARY_FILE).toString().trim().split('\n');
if (parseInt(rows[0]) !== id_1.PROCESS_PID) {
return;
}
fs_1.unlinkSync(SUMMARY_FILE);
const group = {};
// COLLECT
rows.slice(1).forEach((raw) => {
const item = JSON.parse(raw);
const { type } = item;
if (!Array.isArray(group[type])) {
group[type] = [];
}
group[type].push(item);
});
// OUTPUT
['warn', 'info', 'error'].forEach((type) => {
const items = group[type];
const style = STYLE_BY_TYPE[type];
if (!items || !items.length) {
return;
}
const content = items
.map(({ title, detail }) => {
const formatedDetail = detail.trim().split('\n').map((v) => v.trim()).join('\n');
return `${chalk_1.bold(title)}${detail ? `\n${formatedDetail}` : ''}`;
})
.join('\n\n');
console[type](boxen(content, style.boxen));
});
};
}
exports.startProcessSummary = startProcessSummary;
//# sourceMappingURL=summary.js.map