refactor(cli): call setup hook in run command with more args (#6353)

This commit is contained in:
Kevin Marrec 2019-09-04 12:06:34 +02:00 committed by Pooya Parsa
parent 519ced4c4a
commit bdcf4c8304
4 changed files with 36 additions and 15 deletions

View File

@ -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

View File

@ -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 })

View File

@ -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()

View File

@ -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)