@bitrix/cli
Version:
Bitrix CLI tools
110 lines (100 loc) • 4.69 kB
JavaScript
;
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var minimist = _interopDefault(require('minimist'));
var colors = _interopDefault(require('colors'));
var Logger = _interopDefault(require('@bitrix/logger'));
var alias = {
w: 'watch',
p: 'path',
m: 'modules',
t: 'test',
h: 'help',
v: 'version',
c: 'create',
n: 'name',
e: 'extensions'
};
var argv = minimist(process.argv.slice(2), {
alias
});
function space(count) {
return ' '.repeat(Math.max(count, 2));
}
class Help {
constructor() {
this.data = [];
this.offset = 5;
}
usage(text) {
this.data.push(`${space(this.offset)}${'Usage:'.bold} ${text}\n`);
return this;
}
header(text) {
this.data.push(`${space(this.offset)}${text}`.bold);
return this;
}
subheader(text) {
this.data.push(`${space(this.offset + 2)}${text}`.bold);
return this;
}
command(command, value, description) {
let preparedDescription = description;
if (preparedDescription.includes('\n')) {
preparedDescription = description.split('\n').reduce((acc, item, index) => {
if (index) {
return `${acc}\n${space(20 + this.offset + 2)}${item}`;
}
return `${acc}${item}`;
}, '');
}
this.data.push([`${space(this.offset + 2)}${command}`, value, preparedDescription]);
return this;
}
option(option, value = '', description = '') {
this.data.push([`${space(this.offset + 3)}${option}`, value, description]);
return this;
}
separator() {
this.data.push(`${space(this.offset)}${colors.gray('-'.repeat(80))}`);
return this;
}
print() {
const [col1Size, col2Size, col3Size] = this.data.reduce((acc, item) => {
if (Array.isArray(item)) {
acc[0] = Math.max(acc[0], item[0].length);
acc[1] = Math.max(acc[1], item[1].length);
acc[2] = Math.max(acc[2], item[2].length);
}
return acc;
}, [0, 0, 0]);
['\n', ...this.data, '\n'].forEach(item => {
if (Array.isArray(item)) {
const col1Spaces = space(Math.max(item[0].length, col1Size) - Math.min(item[0].length, col1Size) + (this.offset - 2));
const col2Spaces = space(Math.max(item[1].length, col2Size) - Math.min(item[1].length, col2Size) + (this.offset - 2));
const col3Spaces = space(Math.max(item[2].length, col3Size) - Math.min(item[2].length, col3Size) + (this.offset - 2));
Logger.log(`${item[0]}${col1Spaces}${item[1]}${col2Spaces}${item[2]}${col3Spaces}`);
} else {
Logger.log(item);
}
});
}
}
const helper = new Help();
helper.usage('bitrix <command> [options]').header('Build code').command('bitrix build', '[options ...]', 'Builds all bundles from current directory').subheader('Options').option('-w, --watch', '[<fileExtension>[, ...]]', 'Run file watcher').option('-m, --modules', '<moduleName>[, ...]', 'Build specified modules').option('-p, --path', '<directoryPath>', 'Build by directory path').option('-t, --test', '', 'Run tests after build').option('-e, --extensions', '<extensionName>[, ...]', 'Build specified extension[s] only').separator().header('Testing').command('bitrix test', '', 'Runs tests').subheader('Options').option('-w, --watch', '[<fileExtension>[, ...]]', 'Run file watcher').option('-m, --modules', '<moduleName>[, ...]', 'Build specified modules').option('-p, --path', '<directoryPath>', 'Build by directory path').option('-e, --extensions', '<extensionName>[, ...]', 'Build specified extension[s] only').separator().header('Create extension').command('bitrix create', '[<extensionName>]', 'Run create extension wizard').subheader('Options').option('-y', '', 'Create extension with default values').separator().header('Custom code generation').command('bitrix run', '<templatePath>', 'Run custom template').subheader('Options').option('any', 'any', 'You can use any parameters that the template supports').separator().header('Settings').command('bitrix settings', '', 'Runs settings wizard').separator().header('Info').command('bitrix info', '', 'Print information about tools').separator().header('Help').command('bitrix --help, -h', '', 'Print help information').separator().header('More information').subheader('https://github.com/bitrix-tools/cli');
function help() {
helper.print();
}
var name = "@bitrix/cli";
var version = "3.3.9";
function bitrixUnhandledCommand(params = argv) {
if (params.help) {
help();
return;
}
if (params.version) {
Logger.log(name, version);
return;
}
Logger.log('Unknown command. Try run "bitrix --help" for more information');
}
module.exports = bitrixUnhandledCommand;