From bdcf4c8304a8f1d5db6d9154ed394157e3bfa457 Mon Sep 17 00:00:00 2001 From: Kevin Marrec Date: Wed, 4 Sep 2019 12:06:34 +0200 Subject: [PATCH] refactor(cli): call setup hook in run command with more args (#6353) --- packages/cli/src/command.js | 7 +++++++ packages/cli/src/run.js | 6 ------ packages/cli/test/unit/hooks.test.js | 29 ++++++++++++++++++++++++++++ packages/cli/test/unit/run.test.js | 9 --------- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/packages/cli/src/command.js b/packages/cli/src/command.js index d5d9a60010..9850b4c716 100644 --- a/packages/cli/src/command.js +++ b/packages/cli/src/command.js @@ -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 diff --git a/packages/cli/src/run.js b/packages/cli/src/run.js index 1067573c1b..e6847c5732 100644 --- a/packages/cli/src/run.js +++ b/packages/cli/src/run.js @@ -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 }) diff --git a/packages/cli/test/unit/hooks.test.js b/packages/cli/test/unit/hooks.test.js index e260fe316c..f9e8632191 100644 --- a/packages/cli/test/unit/hooks.test.js +++ b/packages/cli/test/unit/hooks.test.js @@ -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() diff --git a/packages/cli/test/unit/run.test.js b/packages/cli/test/unit/run.test.js index babd703f50..6134085795 100644 --- a/packages/cli/test/unit/run.test.js +++ b/packages/cli/test/unit/run.test.js @@ -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)