mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
Refactor cli
Extracted common logic Improved style consistency
This commit is contained in:
parent
8fcef54a23
commit
ad0649925d
43
bin/common/utils.js
Normal file
43
bin/common/utils.js
Normal file
@ -0,0 +1,43 @@
|
||||
const { Utils } = require('../..')
|
||||
const { resolve } = require('path')
|
||||
const { existsSync } = require('fs')
|
||||
|
||||
const getRootDir = argv => resolve(argv._[0] || '.')
|
||||
const getNuxtConfigFile = argv => resolve(getRootDir(argv), argv['config-file'])
|
||||
|
||||
exports.nuxtConfigFile = getNuxtConfigFile
|
||||
|
||||
exports.loadNuxtConfig = argv => {
|
||||
const rootDir = getRootDir(argv)
|
||||
const nuxtConfigFile = getNuxtConfigFile(argv)
|
||||
|
||||
let options = {}
|
||||
|
||||
if (existsSync(nuxtConfigFile)) {
|
||||
delete require.cache[nuxtConfigFile]
|
||||
options = require(nuxtConfigFile)
|
||||
} else if (argv['config-file'] !== 'nuxt.config.js') {
|
||||
Utils.fatalError('Could not load config file: ' + argv['config-file'])
|
||||
}
|
||||
|
||||
if (typeof options.rootDir !== 'string') {
|
||||
options.rootDir = rootDir
|
||||
}
|
||||
|
||||
// Nuxt Mode
|
||||
options.mode =
|
||||
(argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
|
||||
|
||||
return options
|
||||
}
|
||||
|
||||
exports.getLatestHost = argv => {
|
||||
const port =
|
||||
argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
|
||||
const host =
|
||||
argv.hostname ||
|
||||
process.env.HOST ||
|
||||
process.env.npm_package_config_nuxt_host
|
||||
|
||||
return { port, host }
|
||||
}
|
9
bin/nuxt
9
bin/nuxt
@ -4,9 +4,10 @@
|
||||
process.env.DEBUG = process.env.DEBUG || 'nuxt:*'
|
||||
|
||||
const { join } = require('path')
|
||||
const { name, engines } = require('../package.json')
|
||||
const semver = require('semver')
|
||||
|
||||
const { Utils } = require('..')
|
||||
const { name, engines } = require('../package.json')
|
||||
|
||||
// Global error handler
|
||||
process.on('unhandledRejection', _error => {
|
||||
@ -14,7 +15,11 @@ process.on('unhandledRejection', _error => {
|
||||
})
|
||||
|
||||
if (!semver.satisfies(process.version, engines.node)) {
|
||||
Utils.fatalError(`The engine "node" is incompatible with ${name}. Expected version "${engines.node}".`)
|
||||
Utils.fatalError(
|
||||
`The engine "node" is incompatible with ${name}. Expected version "${
|
||||
engines.node
|
||||
}".`
|
||||
)
|
||||
}
|
||||
|
||||
const defaultCommand = 'dev'
|
||||
|
@ -1,13 +1,13 @@
|
||||
#!/usr/bin/env node
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const fs = require('fs')
|
||||
const parseArgs = require('minimist')
|
||||
const { Nuxt, Builder, Generator, Utils } = require('..')
|
||||
const resolve = require('path').resolve
|
||||
const debug = require('debug')('nuxt:build')
|
||||
debug.color = 2 // Force green color
|
||||
|
||||
const { Nuxt, Builder, Generator, Utils } = require('..')
|
||||
const { loadNuxtConfig } = require('./common/utils')
|
||||
|
||||
const argv = parseArgs(process.argv.slice(2), {
|
||||
alias: {
|
||||
h: 'help',
|
||||
@ -39,26 +39,11 @@ if (argv.help) {
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
const rootDir = resolve(argv._[0] || '.')
|
||||
const nuxtConfigFile = resolve(rootDir, argv['config-file'])
|
||||
|
||||
var options = {}
|
||||
if (fs.existsSync(nuxtConfigFile)) {
|
||||
options = require(nuxtConfigFile)
|
||||
} else if (argv['config-file'] !== 'nuxt.config.js') {
|
||||
Utils.fatalError(`Could not load config file`, argv['config-file'])
|
||||
}
|
||||
if (typeof options.rootDir !== 'string') {
|
||||
options.rootDir = rootDir
|
||||
}
|
||||
const options = loadNuxtConfig(argv)
|
||||
|
||||
// Create production build when calling `nuxt build`
|
||||
options.dev = false
|
||||
|
||||
// Nuxt Mode
|
||||
options.mode =
|
||||
(argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
|
||||
|
||||
// Analyze option
|
||||
options.build = options.build || {}
|
||||
if (argv.analyze) {
|
||||
@ -86,9 +71,7 @@ if (options.mode !== 'spa') {
|
||||
.build()
|
||||
.then(() => debug('Building done'))
|
||||
.then(() => close())
|
||||
.catch(err => {
|
||||
Utils.fatalError(err)
|
||||
})
|
||||
.catch(Utils.fatalError)
|
||||
} else {
|
||||
// -- Build for SPA app --
|
||||
const s = Date.now()
|
||||
|
62
bin/nuxt-dev
62
bin/nuxt-dev
@ -1,16 +1,19 @@
|
||||
#!/usr/bin/env node
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const _ = require('lodash')
|
||||
const defaultsDeep = require('lodash/defaultsDeep')
|
||||
const debug = require('debug')('nuxt:build')
|
||||
debug.color = 2 // force green color
|
||||
const fs = require('fs')
|
||||
const parseArgs = require('minimist')
|
||||
const { Nuxt, Builder, Utils } = require('..')
|
||||
const chokidar = require('chokidar')
|
||||
const path = require('path')
|
||||
const resolve = path.resolve
|
||||
const pkg = require(path.join('..', 'package.json'))
|
||||
const { version } = require('../package.json')
|
||||
|
||||
const { Nuxt, Builder, Utils } = require('..')
|
||||
const {
|
||||
loadNuxtConfig,
|
||||
getLatestHost,
|
||||
nuxtConfigFile
|
||||
} = require('./common/utils')
|
||||
|
||||
const argv = parseArgs(process.argv.slice(2), {
|
||||
alias: {
|
||||
@ -30,7 +33,7 @@ const argv = parseArgs(process.argv.slice(2), {
|
||||
})
|
||||
|
||||
if (argv.version) {
|
||||
console.log(pkg.version)
|
||||
console.log(version)
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
@ -56,19 +59,18 @@ if (argv.help) {
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
const rootDir = resolve(argv._[0] || '.')
|
||||
const nuxtConfigFile = resolve(rootDir, argv['config-file'])
|
||||
|
||||
// Load config once for chokidar
|
||||
const nuxtConfig = loadNuxtConfig()
|
||||
_.defaultsDeep(nuxtConfig, { watchers: { chokidar: { ignoreInitial: true } } })
|
||||
const nuxtConfig = loadNuxtConfig(argv)
|
||||
defaultsDeep(nuxtConfig, { watchers: { chokidar: { ignoreInitial: true } } })
|
||||
|
||||
// Start dev
|
||||
let dev = startDev()
|
||||
let needToRestart = false
|
||||
|
||||
// Start watching for nuxt.config.js changes
|
||||
chokidar.watch(nuxtConfigFile, nuxtConfig.watchers.chokidar).on('all', () => {
|
||||
chokidar
|
||||
.watch(nuxtConfigFile(argv), nuxtConfig.watchers.chokidar)
|
||||
.on('all', () => {
|
||||
debug('[nuxt.config.js] changed')
|
||||
needToRestart = true
|
||||
|
||||
@ -79,16 +81,11 @@ chokidar.watch(nuxtConfigFile, nuxtConfig.watchers.chokidar).on('all', () => {
|
||||
debug('Rebuilding the app...')
|
||||
return startDev(instance)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
function startDev(oldInstance) {
|
||||
// Get latest environment variables
|
||||
const port =
|
||||
argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
|
||||
const host =
|
||||
argv.hostname ||
|
||||
process.env.HOST ||
|
||||
process.env.npm_package_config_nuxt_host
|
||||
const { port, host } = getLatestHost(argv)
|
||||
|
||||
// Error handler
|
||||
const onError = (err, instance) => {
|
||||
@ -99,7 +96,7 @@ function startDev(oldInstance) {
|
||||
// Load options
|
||||
let options = {}
|
||||
try {
|
||||
options = loadNuxtConfig()
|
||||
options = loadAndAugmentNuxtConfig()
|
||||
} catch (err) {
|
||||
return onError(err, oldInstance)
|
||||
}
|
||||
@ -111,9 +108,9 @@ function startDev(oldInstance) {
|
||||
try {
|
||||
nuxt = new Nuxt(options)
|
||||
builder = new Builder(nuxt)
|
||||
instance = { nuxt: nuxt, builder: builder }
|
||||
instance = { nuxt, builder }
|
||||
} catch (err) {
|
||||
return onError(err, instance || oldInstance)
|
||||
return onError(err, oldInstance)
|
||||
}
|
||||
|
||||
return (
|
||||
@ -142,26 +139,11 @@ function startDev(oldInstance) {
|
||||
)
|
||||
}
|
||||
|
||||
function loadNuxtConfig() {
|
||||
let options = {}
|
||||
|
||||
if (fs.existsSync(nuxtConfigFile)) {
|
||||
delete require.cache[nuxtConfigFile]
|
||||
options = require(nuxtConfigFile)
|
||||
} else if (argv['config-file'] !== 'nuxt.config.js') {
|
||||
Utils.fatalError('Could not load config file: ' + argv['config-file'])
|
||||
}
|
||||
|
||||
if (typeof options.rootDir !== 'string') {
|
||||
options.rootDir = rootDir
|
||||
}
|
||||
function loadAndAugmentNuxtConfig() {
|
||||
const options = loadNuxtConfig(argv)
|
||||
|
||||
// Force development mode for add hot reloading and watching changes
|
||||
options.dev = true
|
||||
|
||||
// Nuxt Mode
|
||||
options.mode =
|
||||
(argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
|
||||
|
||||
return options
|
||||
}
|
||||
|
@ -1,12 +1,11 @@
|
||||
#!/usr/bin/env node
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const fs = require('fs')
|
||||
const parseArgs = require('minimist')
|
||||
const debug = require('debug')('nuxt:generate')
|
||||
|
||||
const { Nuxt, Builder, Generator, Utils } = require('..')
|
||||
const resolve = require('path').resolve
|
||||
const { loadNuxtConfig } = require('./common/utils')
|
||||
|
||||
const argv = parseArgs(process.argv.slice(2), {
|
||||
alias: {
|
||||
@ -39,24 +38,10 @@ if (argv.help) {
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
const rootDir = resolve(argv._[0] || '.')
|
||||
const nuxtConfigFile = resolve(rootDir, argv['config-file'])
|
||||
const options = loadNuxtConfig(argv)
|
||||
|
||||
var options = {}
|
||||
if (fs.existsSync(nuxtConfigFile)) {
|
||||
options = require(nuxtConfigFile)
|
||||
} else if (argv['config-file'] !== 'nuxt.config.js') {
|
||||
Utils.fatalError('Could not load config file: ' + argv['config-file'])
|
||||
}
|
||||
if (typeof options.rootDir !== 'string') {
|
||||
options.rootDir = rootDir
|
||||
}
|
||||
options.dev = false // Force production mode (no webpack middleware called)
|
||||
|
||||
// Nuxt Mode
|
||||
options.mode =
|
||||
(argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
|
||||
|
||||
debug('Generating...')
|
||||
const nuxt = new Nuxt(options)
|
||||
const builder = new Builder(nuxt)
|
||||
@ -97,6 +82,4 @@ generator
|
||||
debug('Generate done')
|
||||
process.exit(0)
|
||||
})
|
||||
.catch(err => {
|
||||
Utils.fatalError(err)
|
||||
})
|
||||
.catch(Utils.fatalError)
|
||||
|
@ -3,9 +3,11 @@
|
||||
|
||||
const fs = require('fs')
|
||||
const parseArgs = require('minimist')
|
||||
const { Nuxt, Utils } = require('..')
|
||||
const { resolve } = require('path')
|
||||
|
||||
const { Nuxt, Utils } = require('..')
|
||||
const { loadNuxtConfig, getLatestHost } = require('./common/utils')
|
||||
|
||||
const argv = parseArgs(process.argv.slice(2), {
|
||||
alias: {
|
||||
h: 'help',
|
||||
@ -44,28 +46,11 @@ if (argv.help) {
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
const rootDir = resolve(argv._[0] || '.')
|
||||
const nuxtConfigFile = resolve(rootDir, argv['config-file'])
|
||||
|
||||
let options = {}
|
||||
|
||||
if (fs.existsSync(nuxtConfigFile)) {
|
||||
options = require(nuxtConfigFile)
|
||||
} else if (argv['config-file'] !== 'nuxt.config.js') {
|
||||
Utils.fatalError('Could not load config file: ' + argv['config-file'])
|
||||
}
|
||||
|
||||
if (typeof options.rootDir !== 'string') {
|
||||
options.rootDir = rootDir
|
||||
}
|
||||
const options = loadNuxtConfig(argv)
|
||||
|
||||
// Force production mode (no webpack middleware called)
|
||||
options.dev = false
|
||||
|
||||
// Nuxt Mode
|
||||
options.mode =
|
||||
(argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
|
||||
|
||||
const nuxt = new Nuxt(options)
|
||||
|
||||
// Setup hooks
|
||||
@ -93,9 +78,6 @@ if (nuxt.options.render.ssr === true) {
|
||||
}
|
||||
}
|
||||
|
||||
const port =
|
||||
argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
|
||||
const host =
|
||||
argv.hostname || process.env.HOST || process.env.npm_package_config_nuxt_host
|
||||
const { port, host } = getLatestHost(argv)
|
||||
|
||||
nuxt.listen(port, host)
|
||||
|
Loading…
Reference in New Issue
Block a user