@mail-core/cli
Version:
Инструментарий для написания cli-скриптов
98 lines • 4.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPackageExtendedConsole = exports.createAppExtendedConsole = exports.verboseEnabled = exports.verbose = exports.createExtendedConsole = exports.applyStyle = void 0;
const pkg_1 = require("../pkg/pkg");
const spinner_1 = require("./spinner");
const color_1 = require("../color");
const interactive_1 = require("../interactive/interactive");
const list_1 = require("./list");
let VERBOSE_ENABLED = (process.argv.includes('--verb') ||
process.argv.includes('--verbose') ||
process.argv.includes('--verbose=true'));
function applyStyle(style, ...args) {
return args.map(v => /string/.test(typeof v) ? style(v) : v);
}
exports.applyStyle = applyStyle;
function createExtendedConsole(label, pkg, yes) {
const logger = {
label,
pkg: pkg || pkg_1.readPackageJson(),
cli: interactive_1.createInteractiveTools({ prefix: label, yes }),
nl() {
interactive_1.interactiveToolNewLine.before = false;
interactive_1.interactiveToolNewLine.after = false;
console.log('');
},
hr(len = 10) {
interactive_1.interactiveToolNewLine.before = false;
interactive_1.interactiveToolNewLine.after = false;
console.log(color_1.gray(label, '·'.repeat(len)));
},
spinner(text, opts = {}) {
return spinner_1.createSpinner(Object.assign(Object.assign({}, opts), { text, prefixText: label }));
},
ok(...args) {
interactive_1.interactiveToolNewLine.touch(label);
console.log(...applyStyle(color_1.green, label, ...args));
},
done(...args) {
interactive_1.interactiveToolNewLine.touch(label);
console.log(...applyStyle(color_1.green, label, ...args));
},
fail(...args) {
interactive_1.interactiveToolNewLine.touch(label);
console.log(...applyStyle(color_1.red, label, ...args));
},
info(...args) {
interactive_1.interactiveToolNewLine.touch(label);
console.info(...applyStyle(color_1.cyan, label, ...args));
},
log(...args) {
interactive_1.interactiveToolNewLine.touch(label);
console.log(label, ...args);
},
error(...args) {
interactive_1.interactiveToolNewLine.touch(label);
console.error(...applyStyle(color_1.red, label, ...args));
},
warn(...args) {
interactive_1.interactiveToolNewLine.touch(label);
console.warn(...applyStyle(color_1.yellow, label, '⚠️ ', ...args));
},
important(...args) {
interactive_1.interactiveToolNewLine.touch(label);
console.log(label, ...applyStyle(color_1.bold, ...args));
},
verbose(...args) {
interactive_1.interactiveToolNewLine.touch(label);
verbose(label, ...args);
},
list(items, options) {
list_1.list(logger, items, options);
},
};
return Object.assign(Object.assign(Object.assign({}, console), logger), { raw: console });
}
exports.createExtendedConsole = createExtendedConsole;
function verbose(...args) {
if (VERBOSE_ENABLED) {
console.warn(...applyStyle(color_1.magenta, ...args));
}
}
exports.verbose = verbose;
function verboseEnabled(state) {
VERBOSE_ENABLED = state;
}
exports.verboseEnabled = verboseEnabled;
function createAppExtendedConsole() {
const pkg = pkg_1.readPackageJson();
return createPackageExtendedConsole(pkg);
}
exports.createAppExtendedConsole = createAppExtendedConsole;
function createPackageExtendedConsole(dirnameOrPkg, yes) {
const pkg = typeof dirnameOrPkg === 'string' ? pkg_1.readPackageJson(dirnameOrPkg) : dirnameOrPkg;
const label = `${color_1.underline(pkg.name)}${color_1.gray(`@${pkg.version}`)}`;
return createExtendedConsole(label, pkg, yes);
}
exports.createPackageExtendedConsole = createPackageExtendedConsole;
//# sourceMappingURL=console.js.map