Skip to content

Commit bdcf4c8

Browse files
kevinmarrecpi0
authored andcommitted
refactor(cli): call setup hook in run command with more args (nuxt#6353)
1 parent 519ced4 commit bdcf4c8

4 files changed

Lines changed: 36 additions & 15 deletions

File tree

packages/cli/src/command.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
import path from 'path'
23
import consola from 'consola'
34
import minimist from 'minimist'
45
import Hookable from 'hable'
@@ -35,6 +36,12 @@ export default class NuxtCommand extends Hookable {
3536
}
3637

3738
async run () {
39+
await this.callHook('setup', {
40+
argv: this._argv,
41+
cmd: this.cmd,
42+
rootDir: path.resolve(this.argv._[0] || '.')
43+
})
44+
3845
if (this.argv.help) {
3946
this.showHelp()
4047
return

packages/cli/src/run.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ export default async function run (_argv, hooks = {}) {
3636
// Check for dev
3737
const dev = argv[0] === 'dev'
3838

39-
// Call setup hook
40-
if (typeof hooks.setup === 'function') {
41-
await hooks.setup({ cmd, dev, argv })
42-
delete hooks.setup
43-
}
44-
4539
// Setup env
4640
setup({ dev })
4741

packages/cli/test/unit/hooks.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import { NuxtCommand } from '../utils'
23

34
describe('dev', () => {
@@ -9,6 +10,34 @@ describe('dev', () => {
910

1011
afterEach(() => jest.clearAllMocks())
1112

13+
test('setup hook', async () => {
14+
const hooks = {
15+
setup: jest.fn()
16+
}
17+
18+
await NuxtCommand.run(dev, [], hooks)
19+
20+
expect(hooks.setup).toHaveBeenCalledWith({
21+
argv: [],
22+
cmd: dev,
23+
rootDir: path.resolve('.')
24+
})
25+
})
26+
27+
test('setup hook (custom CLI options & rootDir)', async () => {
28+
const hooks = {
29+
setup: jest.fn()
30+
}
31+
32+
await NuxtCommand.run(dev, ['-p', '3001', 'path/to/project'], hooks)
33+
34+
expect(hooks.setup).toHaveBeenCalledWith({
35+
argv: ['-p', '3001', 'path/to/project'],
36+
cmd: dev,
37+
rootDir: path.resolve('path/to/project')
38+
})
39+
})
40+
1241
test('config hook', async () => {
1342
const hooks = {
1443
config: jest.fn()

packages/cli/test/unit/run.test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@ describe('run', () => {
2929
expect(NuxtCommand.run).toHaveBeenCalledWith(expect.anything(), ['--foo'], {})
3030
})
3131

32-
test('setup hook', async () => {
33-
const setup = jest.fn()
34-
await run(['--foo'], { setup })
35-
expect(setup).toHaveBeenCalledWith(expect.objectContaining({
36-
argv: ['dev', '--foo'],
37-
dev: true
38-
}))
39-
})
40-
4132
test('all hooks passed to NuxtCommand', async () => {
4233
const hooks = { foo: jest.fn() }
4334
await run([], hooks)

0 commit comments

Comments
 (0)