feat: allow to extend cli parser via CLI event#852
Conversation
| let exitCode; | ||
|
|
||
| exports.run = () => { | ||
| const program = new Command(); |
There was a problem hiding this comment.
используем отдельный инстанс commander'а - это позволяет избежать проблем, когда кто-нибудь у себя в плагинах использует глобальный (который идет по умолчанию)
| .action((paths, options) => runGemini('test', paths, options).done()); | ||
| .action((paths, options) => mkRunFn(gemini, 'test', program)(paths, options).done()); | ||
|
|
||
| program.command('list <key>') |
There was a problem hiding this comment.
это почему-то было опцией, сделал командой
doc/events.md
Outdated
| @@ -1,5 +1,7 @@ | |||
| # Gemini events | |||
|
|
|||
| * `CLI` - emitted right at start, before cli is parsed. Allows to add new commands and extend help message. The event is emmited with 1 argument `parser` which is the [commander](https://github.com/tj/commander.js) instance used inside gemini itself. | |||
lib/cli/index.js
Outdated
| handleUncaughtExceptions(); | ||
| function mkRunFn(gemini, method, globalOpts) { | ||
| return (paths, opts) => { | ||
| opts = opts || {}; |
There was a problem hiding this comment.
https://learn.javascript.ru/es-function#параметры-по-умолчанию
return (paths, opts = {}) => {
...
}
lib/cli/index.js
Outdated
| console.log(data[key] || `Cannot list option ${key}. Available options are: sets, browsers`); | ||
| } | ||
|
|
||
| function collect(newValue, array) { |
There was a problem hiding this comment.
function collect(newValue, array = []) {
...
}|
|
||
| function collect(newValue, array) { | ||
| array = array || []; | ||
| return array.concat(newValue); |
There was a problem hiding this comment.
Как вариант: return [newValue, ...array]
There was a problem hiding this comment.
это доклеит вперед, ну и не так чтоб код проще стал читаться
| @@ -1,5 +1,7 @@ | |||
| # Gemini events | |||
|
|
|||
| * `CLI` - emitted right at start, before cli is parsed. Allows to add new commands and extend help message. The event is emitted with 1 argument `parser` which is the [commander](https://github.com/tj/commander.js) instance used inside gemini itself. | |||
There was a problem hiding this comment.
я не могу сказать, что на 100 % уверен, но тире тут не должно быть:
with 1 argument – `parser`
???
| .version(pkg.version) | ||
| .option('-c, --config <file>', 'config file'); | ||
|
|
||
| const configPath = preparseOption(program, 'config'); |
There was a problem hiding this comment.
а точно эту функцию нужно делать "общей"? ну типо.. чтобы она принимала вторым аргументом config?
Если она сейчас нужна только для получения пути до конфига, то может она и пусть называется parseConfigOption?
There was a problem hiding this comment.
а чем это будет лучше?
There was a problem hiding this comment.
ну а реально это функция где-то еще, кроме как для конфига может быть использована?
There was a problem hiding this comment.
это не повод хардкодить в ней какую-то конкретную опцию. Я повторю вопрос: чем предлагаемый тобой вариант будет лучше текущего?
There was a problem hiding this comment.
в реализации выполняется определенная магия, например, с хелпом, которая сильно специфична, кажется, только для опции config.
ответ на вопрос: изменение имени позволит не лучше понять, что делает функция не заглядывая в ее реалиазацию
| console.log(''); | ||
| }) | ||
| .action((paths, options) => runGemini('test', paths, options).done()); | ||
| .action((paths, options) => mkRunFn(gemini, 'test', program)(paths, options).done()); |
There was a problem hiding this comment.
а может такое API:
.actions((paths, options) => run(gemini, 'test', paths, mergeOpts(program, options)).done())???
Ну просто.. не тяжко ли читается функция, которая возвращает функцию, которая сразу же вызывается?
c314df0 to
81fb51f
Compare
No description provided.