mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-17 06:01:34 +00:00
chore(cli): accept hooks (#6274)
This commit is contained in:
parent
619e6d8adc
commit
f0ab042bf4
@ -21,6 +21,7 @@
|
|||||||
"execa": "^2.0.4",
|
"execa": "^2.0.4",
|
||||||
"exit": "^0.1.2",
|
"exit": "^0.1.2",
|
||||||
"fs-extra": "^8.1.0",
|
"fs-extra": "^8.1.0",
|
||||||
|
"hable": "^2.2.1",
|
||||||
"minimist": "^1.2.0",
|
"minimist": "^1.2.0",
|
||||||
"opener": "1.5.1",
|
"opener": "1.5.1",
|
||||||
"pretty-bytes": "^5.3.0",
|
"pretty-bytes": "^5.3.0",
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import minimist from 'minimist'
|
import minimist from 'minimist'
|
||||||
|
import Hookable from 'hable'
|
||||||
import { name, version } from '../package.json'
|
import { name, version } from '../package.json'
|
||||||
import { forceExit } from './utils'
|
import { forceExit } from './utils'
|
||||||
import { loadNuxtConfig } from './utils/config'
|
import { loadNuxtConfig } from './utils/config'
|
||||||
@ -8,8 +9,11 @@ import { indent, foldLines, colorize } from './utils/formatting'
|
|||||||
import { startSpaces, optionSpaces, forceExitTimeout } from './utils/constants'
|
import { startSpaces, optionSpaces, forceExitTimeout } from './utils/constants'
|
||||||
import * as imports from './imports'
|
import * as imports from './imports'
|
||||||
|
|
||||||
export default class NuxtCommand {
|
export default class NuxtCommand extends Hookable {
|
||||||
constructor (cmd = { name: '', usage: '', description: '' }, argv = process.argv.slice(2)) {
|
constructor (cmd = { name: '', usage: '', description: '' }, argv = process.argv.slice(2), hooks = {}) {
|
||||||
|
super(consola)
|
||||||
|
this.addHooks(hooks)
|
||||||
|
|
||||||
if (!cmd.options) {
|
if (!cmd.options) {
|
||||||
cmd.options = {}
|
cmd.options = {}
|
||||||
}
|
}
|
||||||
@ -19,15 +23,15 @@ export default class NuxtCommand {
|
|||||||
this._parsedArgv = null // Lazy evaluate
|
this._parsedArgv = null // Lazy evaluate
|
||||||
}
|
}
|
||||||
|
|
||||||
static run (cmd, argv) {
|
static run (cmd, argv, hooks) {
|
||||||
return NuxtCommand.from(cmd, argv).run()
|
return NuxtCommand.from(cmd, argv, hooks).run()
|
||||||
}
|
}
|
||||||
|
|
||||||
static from (cmd, argv) {
|
static from (cmd, argv, hooks) {
|
||||||
if (cmd instanceof NuxtCommand) {
|
if (cmd instanceof NuxtCommand) {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
return new NuxtCommand(cmd, argv)
|
return new NuxtCommand(cmd, argv, hooks)
|
||||||
}
|
}
|
||||||
|
|
||||||
async run () {
|
async run () {
|
||||||
@ -100,6 +104,8 @@ export default class NuxtCommand {
|
|||||||
this.cmd.options[name].prepare && this.cmd.options[name].prepare(this, options, this.argv)
|
this.cmd.options[name].prepare && this.cmd.options[name].prepare(this, options, this.argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this.callHook('config', options)
|
||||||
|
|
||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ function packageExists (name) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function run (_argv) {
|
export default async function run (_argv, hooks = {}) {
|
||||||
// Check for not installing both nuxt and nuxt-edge
|
// Check for not installing both nuxt and nuxt-edge
|
||||||
const dupPkg = '@nuxt/' + (pkgName === '@nuxt/cli-edge' ? 'cli' : 'cli-edge')
|
const dupPkg = '@nuxt/' + (pkgName === '@nuxt/cli-edge' ? 'cli' : 'cli-edge')
|
||||||
if (packageExists(dupPkg)) {
|
if (packageExists(dupPkg)) {
|
||||||
@ -33,12 +33,21 @@ export default async function run (_argv) {
|
|||||||
cmd = await getCommand('dev')
|
cmd = await getCommand('dev')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for dev
|
||||||
|
const dev = argv[0] === 'dev'
|
||||||
|
|
||||||
|
// Call setup hook
|
||||||
|
if (typeof hooks.setup === 'function') {
|
||||||
|
await hooks.setup({ cmd, dev, argv })
|
||||||
|
delete hooks.setup
|
||||||
|
}
|
||||||
|
|
||||||
// Setup env
|
// Setup env
|
||||||
setup({ dev: argv[0] === 'dev' })
|
setup({ dev })
|
||||||
|
|
||||||
// Try internal command
|
// Try internal command
|
||||||
if (cmd) {
|
if (cmd) {
|
||||||
return NuxtCommand.run(cmd, argv.slice(1))
|
return NuxtCommand.run(cmd, argv.slice(1), hooks)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try external command
|
// Try external command
|
||||||
|
23
packages/cli/test/unit/hooks.test.js
Normal file
23
packages/cli/test/unit/hooks.test.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { NuxtCommand } from '../utils'
|
||||||
|
|
||||||
|
describe('dev', () => {
|
||||||
|
let dev
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
dev = await import('../../src/commands/dev').then(m => m.default)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => jest.clearAllMocks())
|
||||||
|
|
||||||
|
test('config hook', async () => {
|
||||||
|
const hooks = {
|
||||||
|
config: jest.fn()
|
||||||
|
}
|
||||||
|
|
||||||
|
await NuxtCommand.run(dev, [], hooks)
|
||||||
|
|
||||||
|
expect(hooks.config).toHaveBeenCalledWith(expect.objectContaining({
|
||||||
|
_cli: true
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
})
|
@ -20,19 +20,35 @@ describe('run', () => {
|
|||||||
test('nuxt aliases to nuxt dev', async () => {
|
test('nuxt aliases to nuxt dev', async () => {
|
||||||
await run([])
|
await run([])
|
||||||
expect(getCommand).toHaveBeenCalledWith('dev')
|
expect(getCommand).toHaveBeenCalledWith('dev')
|
||||||
expect(NuxtCommand.run).toHaveBeenCalledWith(expect.anything(), [])
|
expect(NuxtCommand.run).toHaveBeenCalledWith(expect.anything(), [], {})
|
||||||
})
|
})
|
||||||
|
|
||||||
test('nuxt --foo aliases to nuxt dev --foo', async () => {
|
test('nuxt --foo aliases to nuxt dev --foo', async () => {
|
||||||
await run(['--foo'])
|
await run(['--foo'])
|
||||||
expect(getCommand).toHaveBeenCalledWith('dev')
|
expect(getCommand).toHaveBeenCalledWith('dev')
|
||||||
expect(NuxtCommand.run).toHaveBeenCalledWith(expect.anything(), ['--foo'])
|
expect(NuxtCommand.run).toHaveBeenCalledWith(expect.anything(), ['--foo'], {})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('setup hook', async () => {
|
||||||
|
const setup = jest.fn()
|
||||||
|
await run(['--foo'], { setup })
|
||||||
|
expect(setup).toHaveBeenCalledWith(expect.objectContaining({
|
||||||
|
argv: ['dev', '--foo'],
|
||||||
|
dev: true
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
|
||||||
|
test('all hooks passed to NuxtCommand', async () => {
|
||||||
|
const hooks = { foo: jest.fn() }
|
||||||
|
await run([], hooks)
|
||||||
|
|
||||||
|
expect(NuxtCommand.run).toHaveBeenCalledWith(expect.anything(), [], hooks)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('nuxt <dir> aliases to nuxt dev <dir>', async () => {
|
test('nuxt <dir> aliases to nuxt dev <dir>', async () => {
|
||||||
await run([__dirname])
|
await run([__dirname])
|
||||||
expect(getCommand).toHaveBeenCalledWith('dev')
|
expect(getCommand).toHaveBeenCalledWith('dev')
|
||||||
expect(NuxtCommand.run).toHaveBeenCalledWith(expect.anything(), [__dirname])
|
expect(NuxtCommand.run).toHaveBeenCalledWith(expect.anything(), [__dirname], {})
|
||||||
})
|
})
|
||||||
|
|
||||||
test('external commands', async () => {
|
test('external commands', async () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user