mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
chore(cli): accept hooks (#6274)
This commit is contained in:
parent
619e6d8adc
commit
f0ab042bf4
@ -21,6 +21,7 @@
|
||||
"execa": "^2.0.4",
|
||||
"exit": "^0.1.2",
|
||||
"fs-extra": "^8.1.0",
|
||||
"hable": "^2.2.1",
|
||||
"minimist": "^1.2.0",
|
||||
"opener": "1.5.1",
|
||||
"pretty-bytes": "^5.3.0",
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
import consola from 'consola'
|
||||
import minimist from 'minimist'
|
||||
import Hookable from 'hable'
|
||||
import { name, version } from '../package.json'
|
||||
import { forceExit } from './utils'
|
||||
import { loadNuxtConfig } from './utils/config'
|
||||
@ -8,8 +9,11 @@ import { indent, foldLines, colorize } from './utils/formatting'
|
||||
import { startSpaces, optionSpaces, forceExitTimeout } from './utils/constants'
|
||||
import * as imports from './imports'
|
||||
|
||||
export default class NuxtCommand {
|
||||
constructor (cmd = { name: '', usage: '', description: '' }, argv = process.argv.slice(2)) {
|
||||
export default class NuxtCommand extends Hookable {
|
||||
constructor (cmd = { name: '', usage: '', description: '' }, argv = process.argv.slice(2), hooks = {}) {
|
||||
super(consola)
|
||||
this.addHooks(hooks)
|
||||
|
||||
if (!cmd.options) {
|
||||
cmd.options = {}
|
||||
}
|
||||
@ -19,15 +23,15 @@ export default class NuxtCommand {
|
||||
this._parsedArgv = null // Lazy evaluate
|
||||
}
|
||||
|
||||
static run (cmd, argv) {
|
||||
return NuxtCommand.from(cmd, argv).run()
|
||||
static run (cmd, argv, hooks) {
|
||||
return NuxtCommand.from(cmd, argv, hooks).run()
|
||||
}
|
||||
|
||||
static from (cmd, argv) {
|
||||
static from (cmd, argv, hooks) {
|
||||
if (cmd instanceof NuxtCommand) {
|
||||
return cmd
|
||||
}
|
||||
return new NuxtCommand(cmd, argv)
|
||||
return new NuxtCommand(cmd, argv, hooks)
|
||||
}
|
||||
|
||||
async run () {
|
||||
@ -100,6 +104,8 @@ export default class NuxtCommand {
|
||||
this.cmd.options[name].prepare && this.cmd.options[name].prepare(this, options, this.argv)
|
||||
}
|
||||
|
||||
await this.callHook('config', 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
|
||||
const dupPkg = '@nuxt/' + (pkgName === '@nuxt/cli-edge' ? 'cli' : 'cli-edge')
|
||||
if (packageExists(dupPkg)) {
|
||||
@ -33,12 +33,21 @@ export default async function run (_argv) {
|
||||
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({ dev: argv[0] === 'dev' })
|
||||
setup({ dev })
|
||||
|
||||
// Try internal command
|
||||
if (cmd) {
|
||||
return NuxtCommand.run(cmd, argv.slice(1))
|
||||
return NuxtCommand.run(cmd, argv.slice(1), hooks)
|
||||
}
|
||||
|
||||
// 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 () => {
|
||||
await run([])
|
||||
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 () => {
|
||||
await run(['--foo'])
|
||||
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 () => {
|
||||
await run([__dirname])
|
||||
expect(getCommand).toHaveBeenCalledWith('dev')
|
||||
expect(NuxtCommand.run).toHaveBeenCalledWith(expect.anything(), [__dirname])
|
||||
expect(NuxtCommand.run).toHaveBeenCalledWith(expect.anything(), [__dirname], {})
|
||||
})
|
||||
|
||||
test('external commands', async () => {
|
||||
|
Loading…
Reference in New Issue
Block a user