From ad0649925d67b3ea456ac7694a3513823e567166 Mon Sep 17 00:00:00 2001 From: qm3ster Date: Wed, 7 Feb 2018 02:26:40 +0200 Subject: [PATCH] Refactor cli Extracted common logic Improved style consistency --- bin/common/utils.js | 43 +++++++++++++++++++++++++ bin/nuxt | 9 ++++-- bin/nuxt-build | 27 +++------------- bin/nuxt-dev | 76 +++++++++++++++++---------------------------- bin/nuxt-generate | 23 ++------------ bin/nuxt-start | 28 +++-------------- 6 files changed, 92 insertions(+), 114 deletions(-) create mode 100644 bin/common/utils.js diff --git a/bin/common/utils.js b/bin/common/utils.js new file mode 100644 index 0000000000..63aa89dd3d --- /dev/null +++ b/bin/common/utils.js @@ -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 } +} diff --git a/bin/nuxt b/bin/nuxt index a1ddd8b065..cd0ec2590a 100755 --- a/bin/nuxt +++ b/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' diff --git a/bin/nuxt-build b/bin/nuxt-build index c171ca1ee0..552d2a463b 100755 --- a/bin/nuxt-build +++ b/bin/nuxt-build @@ -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() diff --git a/bin/nuxt-dev b/bin/nuxt-dev index 74f365774e..e71811cbed 100755 --- a/bin/nuxt-dev +++ b/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,39 +59,33 @@ 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', () => { - debug('[nuxt.config.js] changed') - needToRestart = true +chokidar + .watch(nuxtConfigFile(argv), nuxtConfig.watchers.chokidar) + .on('all', () => { + debug('[nuxt.config.js] changed') + needToRestart = true - dev = dev.then(instance => { - if (needToRestart === false) return instance - needToRestart = false + dev = dev.then(instance => { + if (needToRestart === false) return instance + needToRestart = false - debug('Rebuilding the app...') - return startDev(instance) + 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 } diff --git a/bin/nuxt-generate b/bin/nuxt-generate index 6c21d1fa36..292731a4fa 100755 --- a/bin/nuxt-generate +++ b/bin/nuxt-generate @@ -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) diff --git a/bin/nuxt-start b/bin/nuxt-start index 50fd50bf46..7033e1711f 100755 --- a/bin/nuxt-start +++ b/bin/nuxt-start @@ -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)