Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom CLI Commands #11

Closed
pi0 opened this issue Nov 18, 2018 · 4 comments
Closed

Custom CLI Commands #11

pi0 opened this issue Nov 18, 2018 · 4 comments
Assignees
Labels

Comments

@pi0
Copy link
Member

pi0 commented Nov 18, 2018

Related to #10.

Related PR: nuxt/nuxt#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 own bin/ commands with help of a nuxt core module that is also semi-compatible with nuxt projects lower than 2.3.0.

#!/bin/env node -r esm

import { NuxtCommand, run } from '@nuxt/cli'

const cmd = NuxtCommand.from({
    name: 'custom',
    description: 'A custom nuxt command',
    usage: 'nuxt custom',
    async run(cmd) {
      const argv = cmd.getArgv()
      // ...
    }
})

run(cmd)

Positive points:

  • Universal compatibility with all nuxt versions
  • The is the Node.js standard way of creating new commands
  • Commands automatically installed by installing modules
  • Module authors have the choice to select the @nuxt/cli version or extend/customize NuxtCommand class.
  • Covers local commands

Negative points:

  • Listing extra commands with nuxt help is hard but lightweight (Scan path)

Appendix:

For sub-commands support, nuxt CLI can simply map nuxt foo into nuxt-foo and nuxt foo bar into nuxt-foo-bar command and execute it. Preferring convention over platform/PM complexities.

2. js files that @nuxt/cli execute them

The 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 invoking npx nuxt my-command custom nuxt will search for custom command, wraps it into NuxtCommand and runs it.

Positive points:

  • Commands automatically installed by installing modules
  • Covers local commands

Negative points:

  • Only supported for >=2.3.0
  • This is the same wrong way of ModuleContainer (Module Improvements #10). The command is implicitly wrapped and command author is not aware who is calling it and what functions exist in nuxtCommand
  • Needs extra complexities in the nuxt core (cli package)
  • Searching node_modules is not either easy or stable. Different package managers and ways to run are involved (NPX, global cli)
  • Custom commands listing hard and costly (Need to scan node_modules)

3. Via module commands export

Module authors can attach module exported commands to the exported function commands object like this:

function myModule(options) {
 // ...
}

myCommand.commands = {
  custom: {
    name: 'custom',
    description: 'A custom nuxt command',
    usage: 'nuxt custom',
    async run(cmd) {
      const argv = cmd.getArgv()
      // ...
    }
  }
}

Positive points:

  • Extending modules spec. Allows adding custom commands using nuxt.config.js or writing commands in the same file of module.js
  • Listing custom commands are easy (Check for options.modules)

Negative points:

  • Only supported for >= 2.3.0
  • This is the same wrong way of ModuleContainer (Module Improvements #10). The command is implicitly wrapped and command author is not aware who is calling it and what functions exist in nuxtCommand
  • Needs extra complexities in the nuxt core (cli package)
  • Modules are nuxt runtime addons by design, not for CLI. (Antipattern)
@pi0 pi0 changed the title [RFC] support adding sub-command to cli support adding sub-command to cli Nov 18, 2018
@pi0 pi0 changed the title support adding sub-command to cli Support adding Sub-Command to CLI Nov 18, 2018
@pi0 pi0 changed the title Support adding Sub-Command to CLI Custom CLI Commands Nov 18, 2018
@galvez galvez assigned galvez and pi0 Nov 25, 2018
@galvez
Copy link

galvez commented Nov 25, 2018

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 nuxt/nuxt#4314 towards that direction.

@manniL
Copy link
Member

manniL commented Nov 26, 2018

👍 for Option 1 ☺️

@atinux
Copy link
Member

atinux commented Nov 26, 2018

Let's go for Option 1, awesome write up @pi0, really impressed by it and the time you put into it.

@nuxt nuxt locked and limited conversation to collaborators Dec 11, 2018
@pi0 pi0 removed the RFC label Dec 11, 2018
@pi0
Copy link
Member Author

pi0 commented Jan 22, 2019

Implemented

@pi0 pi0 closed this as completed Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants