Skip to content

Commit 9b846ac

Browse files
authored
Merge pull request nuxt#2783 from qm3ster/refactor/cli
refactor(cli)
2 parents 0a82325 + ad06499 commit 9b846ac

File tree

6 files changed

+94
-116
lines changed

6 files changed

+94
-116
lines changed

bin/common/utils.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const { Utils } = require('../..')
2+
const { resolve } = require('path')
3+
const { existsSync } = require('fs')
4+
5+
const getRootDir = argv => resolve(argv._[0] || '.')
6+
const getNuxtConfigFile = argv => resolve(getRootDir(argv), argv['config-file'])
7+
8+
exports.nuxtConfigFile = getNuxtConfigFile
9+
10+
exports.loadNuxtConfig = argv => {
11+
const rootDir = getRootDir(argv)
12+
const nuxtConfigFile = getNuxtConfigFile(argv)
13+
14+
let options = {}
15+
16+
if (existsSync(nuxtConfigFile)) {
17+
delete require.cache[nuxtConfigFile]
18+
options = require(nuxtConfigFile)
19+
} else if (argv['config-file'] !== 'nuxt.config.js') {
20+
Utils.fatalError('Could not load config file: ' + argv['config-file'])
21+
}
22+
23+
if (typeof options.rootDir !== 'string') {
24+
options.rootDir = rootDir
25+
}
26+
27+
// Nuxt Mode
28+
options.mode =
29+
(argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
30+
31+
return options
32+
}
33+
34+
exports.getLatestHost = argv => {
35+
const port =
36+
argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
37+
const host =
38+
argv.hostname ||
39+
process.env.HOST ||
40+
process.env.npm_package_config_nuxt_host
41+
42+
return { port, host }
43+
}

bin/nuxt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44
process.env.DEBUG = process.env.DEBUG || 'nuxt:*'
55

66
const { join } = require('path')
7-
const { name, engines } = require('../package.json')
87
const semver = require('semver')
8+
99
const { Utils } = require('..')
10+
const { name, engines } = require('../package.json')
1011

1112
// Global error handler
1213
process.on('unhandledRejection', _error => {
1314
Utils.printError(_error)
1415
})
1516

1617
if (!semver.satisfies(process.version, engines.node)) {
17-
Utils.fatalError(`The engine "node" is incompatible with ${name}. Expected version "${engines.node}".`)
18+
Utils.fatalError(
19+
`The engine "node" is incompatible with ${name}. Expected version "${
20+
engines.node
21+
}".`
22+
)
1823
}
1924

2025
const defaultCommand = 'dev'

bin/nuxt-build

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/env node
22
/* eslint-disable no-console */
33

4-
const fs = require('fs')
54
const parseArgs = require('minimist')
6-
const { Nuxt, Builder, Generator, Utils } = require('..')
7-
const resolve = require('path').resolve
85
const debug = require('debug')('nuxt:build')
96
debug.color = 2 // Force green color
107

8+
const { Nuxt, Builder, Generator, Utils } = require('..')
9+
const { loadNuxtConfig } = require('./common/utils')
10+
1111
const argv = parseArgs(process.argv.slice(2), {
1212
alias: {
1313
h: 'help',
@@ -39,26 +39,11 @@ if (argv.help) {
3939
process.exit(0)
4040
}
4141

42-
const rootDir = resolve(argv._[0] || '.')
43-
const nuxtConfigFile = resolve(rootDir, argv['config-file'])
44-
45-
var options = {}
46-
if (fs.existsSync(nuxtConfigFile)) {
47-
options = require(nuxtConfigFile)
48-
} else if (argv['config-file'] !== 'nuxt.config.js') {
49-
Utils.fatalError(`Could not load config file`, argv['config-file'])
50-
}
51-
if (typeof options.rootDir !== 'string') {
52-
options.rootDir = rootDir
53-
}
42+
const options = loadNuxtConfig(argv)
5443

5544
// Create production build when calling `nuxt build`
5645
options.dev = false
5746

58-
// Nuxt Mode
59-
options.mode =
60-
(argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
61-
6247
// Analyze option
6348
options.build = options.build || {}
6449
if (argv.analyze) {
@@ -86,9 +71,7 @@ if (options.mode !== 'spa') {
8671
.build()
8772
.then(() => debug('Building done'))
8873
.then(() => close())
89-
.catch(err => {
90-
Utils.fatalError(err)
91-
})
74+
.catch(Utils.fatalError)
9275
} else {
9376
// -- Build for SPA app --
9477
const s = Date.now()

bin/nuxt-dev

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
#!/usr/bin/env node
22
/* eslint-disable no-console */
33

4-
const _ = require('lodash')
4+
const defaultsDeep = require('lodash/defaultsDeep')
55
const debug = require('debug')('nuxt:build')
66
debug.color = 2 // force green color
7-
const fs = require('fs')
87
const parseArgs = require('minimist')
9-
const { Nuxt, Builder, Utils } = require('..')
108
const chokidar = require('chokidar')
11-
const path = require('path')
12-
const resolve = path.resolve
13-
const pkg = require(path.join('..', 'package.json'))
9+
const { version } = require('../package.json')
10+
11+
const { Nuxt, Builder, Utils } = require('..')
12+
const {
13+
loadNuxtConfig,
14+
getLatestHost,
15+
nuxtConfigFile
16+
} = require('./common/utils')
1417

1518
const argv = parseArgs(process.argv.slice(2), {
1619
alias: {
@@ -30,7 +33,7 @@ const argv = parseArgs(process.argv.slice(2), {
3033
})
3134

3235
if (argv.version) {
33-
console.log(pkg.version)
36+
console.log(version)
3437
process.exit(0)
3538
}
3639

@@ -56,39 +59,33 @@ if (argv.help) {
5659
process.exit(0)
5760
}
5861

59-
const rootDir = resolve(argv._[0] || '.')
60-
const nuxtConfigFile = resolve(rootDir, argv['config-file'])
61-
6262
// Load config once for chokidar
63-
const nuxtConfig = loadNuxtConfig()
64-
_.defaultsDeep(nuxtConfig, { watchers: { chokidar: { ignoreInitial: true } } })
63+
const nuxtConfig = loadNuxtConfig(argv)
64+
defaultsDeep(nuxtConfig, { watchers: { chokidar: { ignoreInitial: true } } })
6565

6666
// Start dev
6767
let dev = startDev()
6868
let needToRestart = false
6969

7070
// Start watching for nuxt.config.js changes
71-
chokidar.watch(nuxtConfigFile, nuxtConfig.watchers.chokidar).on('all', () => {
72-
debug('[nuxt.config.js] changed')
73-
needToRestart = true
74-
75-
dev = dev.then(instance => {
76-
if (needToRestart === false) return instance
77-
needToRestart = false
78-
79-
debug('Rebuilding the app...')
80-
return startDev(instance)
71+
chokidar
72+
.watch(nuxtConfigFile(argv), nuxtConfig.watchers.chokidar)
73+
.on('all', () => {
74+
debug('[nuxt.config.js] changed')
75+
needToRestart = true
76+
77+
dev = dev.then(instance => {
78+
if (needToRestart === false) return instance
79+
needToRestart = false
80+
81+
debug('Rebuilding the app...')
82+
return startDev(instance)
83+
})
8184
})
82-
})
8385

8486
function startDev(oldInstance) {
8587
// Get latest environment variables
86-
const port =
87-
argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
88-
const host =
89-
argv.hostname ||
90-
process.env.HOST ||
91-
process.env.npm_package_config_nuxt_host
88+
const { port, host } = getLatestHost(argv)
9289

9390
// Error handler
9491
const onError = (err, instance) => {
@@ -99,7 +96,7 @@ function startDev(oldInstance) {
9996
// Load options
10097
let options = {}
10198
try {
102-
options = loadNuxtConfig()
99+
options = loadAndAugmentNuxtConfig()
103100
} catch (err) {
104101
return onError(err, oldInstance)
105102
}
@@ -111,9 +108,9 @@ function startDev(oldInstance) {
111108
try {
112109
nuxt = new Nuxt(options)
113110
builder = new Builder(nuxt)
114-
instance = { nuxt: nuxt, builder: builder }
111+
instance = { nuxt, builder }
115112
} catch (err) {
116-
return onError(err, instance || oldInstance)
113+
return onError(err, oldInstance)
117114
}
118115

119116
return (
@@ -142,26 +139,11 @@ function startDev(oldInstance) {
142139
)
143140
}
144141

145-
function loadNuxtConfig() {
146-
let options = {}
147-
148-
if (fs.existsSync(nuxtConfigFile)) {
149-
delete require.cache[nuxtConfigFile]
150-
options = require(nuxtConfigFile)
151-
} else if (argv['config-file'] !== 'nuxt.config.js') {
152-
Utils.fatalError('Could not load config file: ' + argv['config-file'])
153-
}
154-
155-
if (typeof options.rootDir !== 'string') {
156-
options.rootDir = rootDir
157-
}
142+
function loadAndAugmentNuxtConfig() {
143+
const options = loadNuxtConfig(argv)
158144

159145
// Force development mode for add hot reloading and watching changes
160146
options.dev = true
161147

162-
// Nuxt Mode
163-
options.mode =
164-
(argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
165-
166148
return options
167149
}

bin/nuxt-generate

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#!/usr/bin/env node
22
/* eslint-disable no-console */
33

4-
const fs = require('fs')
54
const parseArgs = require('minimist')
65
const debug = require('debug')('nuxt:generate')
76

87
const { Nuxt, Builder, Generator, Utils } = require('..')
9-
const resolve = require('path').resolve
8+
const { loadNuxtConfig } = require('./common/utils')
109

1110
const argv = parseArgs(process.argv.slice(2), {
1211
alias: {
@@ -39,24 +38,10 @@ if (argv.help) {
3938
process.exit(0)
4039
}
4140

42-
const rootDir = resolve(argv._[0] || '.')
43-
const nuxtConfigFile = resolve(rootDir, argv['config-file'])
41+
const options = loadNuxtConfig(argv)
4442

45-
var options = {}
46-
if (fs.existsSync(nuxtConfigFile)) {
47-
options = require(nuxtConfigFile)
48-
} else if (argv['config-file'] !== 'nuxt.config.js') {
49-
Utils.fatalError('Could not load config file: ' + argv['config-file'])
50-
}
51-
if (typeof options.rootDir !== 'string') {
52-
options.rootDir = rootDir
53-
}
5443
options.dev = false // Force production mode (no webpack middleware called)
5544

56-
// Nuxt Mode
57-
options.mode =
58-
(argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
59-
6045
debug('Generating...')
6146
const nuxt = new Nuxt(options)
6247
const builder = new Builder(nuxt)
@@ -97,6 +82,4 @@ generator
9782
debug('Generate done')
9883
process.exit(0)
9984
})
100-
.catch(err => {
101-
Utils.fatalError(err)
102-
})
85+
.catch(Utils.fatalError)

bin/nuxt-start

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
const fs = require('fs')
55
const parseArgs = require('minimist')
6-
const { Nuxt, Utils } = require('..')
76
const { resolve } = require('path')
87

8+
const { Nuxt, Utils } = require('..')
9+
const { loadNuxtConfig, getLatestHost } = require('./common/utils')
10+
911
const argv = parseArgs(process.argv.slice(2), {
1012
alias: {
1113
h: 'help',
@@ -44,28 +46,11 @@ if (argv.help) {
4446
process.exit(0)
4547
}
4648

47-
const rootDir = resolve(argv._[0] || '.')
48-
const nuxtConfigFile = resolve(rootDir, argv['config-file'])
49-
50-
let options = {}
51-
52-
if (fs.existsSync(nuxtConfigFile)) {
53-
options = require(nuxtConfigFile)
54-
} else if (argv['config-file'] !== 'nuxt.config.js') {
55-
Utils.fatalError('Could not load config file: ' + argv['config-file'])
56-
}
57-
58-
if (typeof options.rootDir !== 'string') {
59-
options.rootDir = rootDir
60-
}
49+
const options = loadNuxtConfig(argv)
6150

6251
// Force production mode (no webpack middleware called)
6352
options.dev = false
6453

65-
// Nuxt Mode
66-
options.mode =
67-
(argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
68-
6954
const nuxt = new Nuxt(options)
7055

7156
// Setup hooks
@@ -93,9 +78,6 @@ if (nuxt.options.render.ssr === true) {
9378
}
9479
}
9580

96-
const port =
97-
argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
98-
const host =
99-
argv.hostname || process.env.HOST || process.env.npm_package_config_nuxt_host
81+
const { port, host } = getLatestHost(argv)
10082

10183
nuxt.listen(port, host)

0 commit comments

Comments
 (0)