Closed pi0 closed 5 years ago
Thanks for the write-up @pi0 -- It's pretty clear to me that Option 1 is the way to go. Thanks for laying out all pros and cons so thoroughly. Unless anyone wants to voice more concerns, I'm gonna start making the necessary changes in https://github.com/nuxt/nuxt.js/pull/4314 towards that direction.
:+1: for Option 1 :relaxed:
Let's go for Option 1, awesome write up @pi0, really impressed by it and the time you put into it.
Implemented
Related to #10.
Related PR: nuxt/nuxt.js#4314
Original idea: @galvez
TLDR
A limitation of current module spec is that it doesn't allow adding new commands. We can't add helpers to the module function body because commands are executed before of nuxt instance initialization.
Different ways
1. Universal approach
The
@nuxt/cli
package exports useful functions to create a good CLI command interface and also benefits of nuxt built-in utilities like config loading and fancy CLI. This pattern allows module package authors to easily create their ownbin/
commands with help of a nuxt core module that is also semi-compatible with nuxt projects lower than2.3.0
.Positive points:
@nuxt/cli
version or extend/customizeNuxtCommand
class.Negative points:
nuxt help
is hard but lightweight (Scan path)Appendix:
For sub-commands support, nuxt CLI can simply map
nuxt foo
intonuxt-foo
andnuxt foo bar
intonuxt-foo-bar
command and execute it. Preferring convention over platform/PM complexities.2.
js
files that@nuxt/cli
execute themThe process of adding a command to modules is like creating
node_modules/my-module/bin/custom.js
that exports a command object like nuxt internal commands. Then by invokingnpx nuxt my-command custom
nuxt will search forcustom
command, wraps it intoNuxtCommand
and runs it.Positive points:
Negative points:
>=2.3.0
ModuleContainer
(#10). The command is implicitly wrapped and command author is not aware who is calling it and what functions exist innuxtCommand
node_modules
is not either easy or stable. Different package managers and ways to run are involved (NPX, global cli)3. Via module
commands
exportModule authors can attach module exported commands to the exported function
commands
object like this:Positive points:
nuxt.config.js
or writing commands in the same file ofmodule.js
options.modules
)Negative points:
>= 2.3.0
ModuleContainer
(#10). The command is implicitly wrapped and command author is not aware who is calling it and what functions exist innuxtCommand