From f0ab042bf452638669fc15acfb2c79b7d147bb7b Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 24 Aug 2019 18:45:08 +0430 Subject: [PATCH] chore(cli): accept hooks (#6274) --- packages/cli/package.json | 1 + packages/cli/src/command.js | 18 ++++++++++++------ packages/cli/src/run.js | 15 ++++++++++++--- packages/cli/test/unit/hooks.test.js | 23 +++++++++++++++++++++++ packages/cli/test/unit/run.test.js | 22 +++++++++++++++++++--- 5 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 packages/cli/test/unit/hooks.test.js diff --git a/packages/cli/package.json b/packages/cli/package.json index 68c253cb98..5509f2e826 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -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", diff --git a/packages/cli/src/command.js b/packages/cli/src/command.js index fd965ce8c9..d5d9a60010 100644 --- a/packages/cli/src/command.js +++ b/packages/cli/src/command.js @@ -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 } diff --git a/packages/cli/src/run.js b/packages/cli/src/run.js index bfe7fe49ef..1067573c1b 100644 --- a/packages/cli/src/run.js +++ b/packages/cli/src/run.js @@ -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 diff --git a/packages/cli/test/unit/hooks.test.js b/packages/cli/test/unit/hooks.test.js new file mode 100644 index 0000000000..e260fe316c --- /dev/null +++ b/packages/cli/test/unit/hooks.test.js @@ -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 + })) + }) +}) diff --git a/packages/cli/test/unit/run.test.js b/packages/cli/test/unit/run.test.js index 26ff0ed6b3..babd703f50 100644 --- a/packages/cli/test/unit/run.test.js +++ b/packages/cli/test/unit/run.test.js @@ -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 aliases to nuxt dev ', 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 () => {