mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-17 06:01:34 +00:00
refactor(cli): call setup hook in run command with more args (#6353)
This commit is contained in:
parent
519ced4c4a
commit
bdcf4c8304
@ -1,4 +1,5 @@
|
||||
|
||||
import path from 'path'
|
||||
import consola from 'consola'
|
||||
import minimist from 'minimist'
|
||||
import Hookable from 'hable'
|
||||
@ -35,6 +36,12 @@ export default class NuxtCommand extends Hookable {
|
||||
}
|
||||
|
||||
async run () {
|
||||
await this.callHook('setup', {
|
||||
argv: this._argv,
|
||||
cmd: this.cmd,
|
||||
rootDir: path.resolve(this.argv._[0] || '.')
|
||||
})
|
||||
|
||||
if (this.argv.help) {
|
||||
this.showHelp()
|
||||
return
|
||||
|
@ -36,12 +36,6 @@ export default async function run (_argv, hooks = {}) {
|
||||
// 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 })
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
import path from 'path'
|
||||
import { NuxtCommand } from '../utils'
|
||||
|
||||
describe('dev', () => {
|
||||
@ -9,6 +10,34 @@ describe('dev', () => {
|
||||
|
||||
afterEach(() => jest.clearAllMocks())
|
||||
|
||||
test('setup hook', async () => {
|
||||
const hooks = {
|
||||
setup: jest.fn()
|
||||
}
|
||||
|
||||
await NuxtCommand.run(dev, [], hooks)
|
||||
|
||||
expect(hooks.setup).toHaveBeenCalledWith({
|
||||
argv: [],
|
||||
cmd: dev,
|
||||
rootDir: path.resolve('.')
|
||||
})
|
||||
})
|
||||
|
||||
test('setup hook (custom CLI options & rootDir)', async () => {
|
||||
const hooks = {
|
||||
setup: jest.fn()
|
||||
}
|
||||
|
||||
await NuxtCommand.run(dev, ['-p', '3001', 'path/to/project'], hooks)
|
||||
|
||||
expect(hooks.setup).toHaveBeenCalledWith({
|
||||
argv: ['-p', '3001', 'path/to/project'],
|
||||
cmd: dev,
|
||||
rootDir: path.resolve('path/to/project')
|
||||
})
|
||||
})
|
||||
|
||||
test('config hook', async () => {
|
||||
const hooks = {
|
||||
config: jest.fn()
|
||||
|
@ -29,15 +29,6 @@ describe('run', () => {
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user