Refactor nuxt commands using minimist

This commit is contained in:
Sébastien Chopin 2017-06-11 15:48:20 +02:00 committed by Pooya Parsa
parent 8fe9380df9
commit 602adf0464
6 changed files with 479 additions and 339 deletions

View File

@ -4,60 +4,66 @@
process.env.DEBUG = 'nuxt:*'
var fs = require('fs')
var parseArgs = require('minimist')
var without = require('lodash').without
var Nuxt = require('../')
var resolve = require('path').resolve
// --analyze option
var analyzeBuild = false
if (process.argv.indexOf('--analyze') !== -1 || process.argv.indexOf('-a') !== -1) {
analyzeBuild = true
process.argv = without(process.argv, '--analyze', '-a')
const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
c: 'config-file',
a: 'analyze'
},
boolean: ['h', 'a'],
string: ['c'],
default: {
c: 'nuxt.config.js'
}
})
if (argv.help) {
console.log(`
Description
Compiles the application for production deployment
Usage
$ nuxt build <dir>
Options
--analyze, -a Launch webpack-bundle-analyzer to optimize your bundles.
--config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
--help, -h Displays this message
`)
process.exit(0)
}
var nuxtConfigFileName = 'nuxt.config.js'
// --config-file option
var indexOfConfig = false
if (process.argv.indexOf('--config-file') !== -1) {
indexOfConfig = process.argv.indexOf('--config-file')
} else if (process.argv.indexOf('-c') !== -1) {
indexOfConfig = process.argv.indexOf('-c')
}
if (indexOfConfig !== false) {
nuxtConfigFileName = process.argv.slice(indexOfConfig)[1]
process.argv = without(process.argv, '--config-file', '-c', nuxtConfigFileName)
}
// Root directory parameter
var rootDir = resolve(process.argv.slice(2)[0] || '.')
var nuxtConfigFilePath = resolve(rootDir, nuxtConfigFileName)
var rootDir = resolve(argv._[0] || '.')
var nuxtConfigFile = resolve(rootDir, argv['config-file'])
var options = {}
if (fs.existsSync(nuxtConfigFilePath)) {
options = require(nuxtConfigFilePath)
} else {
console.log(`Could not locate ${nuxtConfigFilePath}`) // eslint-disable-line no-console
if (fs.existsSync(nuxtConfigFile)) {
options = require(nuxtConfigFile)
} else if (argv['config-file'] !== 'nuxt.config.js') {
console.error(`> Could not load config file ${argv['config-file']}`)
process.exit(1)
}
if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
}
options.dev = false // Create production build when calling `nuxt build`
// Create production build when calling `nuxt build`
options.dev = false
// Analyze option
options.build = options.build || {}
if (analyzeBuild) {
options.build.analyze = analyzeBuild
if (argv.analyze) {
options.build.analyze = true
}
console.log('[nuxt] Building...') // eslint-disable-line no-console
var nuxt = module.exports = new Nuxt(options)
nuxt.build()
.then(() => {
.then(() => {
console.log('[nuxt] Building done') // eslint-disable-line no-console
})
.catch((err) => {
})
.catch((err) => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})
})

View File

@ -7,41 +7,66 @@ var _ = require('lodash')
var debug = require('debug')('nuxt:build')
debug.color = 2 // force green color
var fs = require('fs')
var parseArgs = require('minimist')
var Nuxt = require('../')
var chokidar = require('chokidar')
var resolve = require('path').resolve
var without = require('lodash').without
var nuxtConfigFileName = 'nuxt.config.js'
var argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
H: 'hostname',
p: 'port',
c: 'config-file'
},
boolean: ['h'],
string: ['H', 'c'],
default: {
c: 'nuxt.config.js'
}
})
// --config-file option
var indexOfConfig = false
if (process.argv.indexOf('--config-file') !== -1) {
indexOfConfig = process.argv.indexOf('--config-file')
} else if (process.argv.indexOf('-c') !== -1) {
indexOfConfig = process.argv.indexOf('-c')
if (argv.hostname === '') {
console.error(`> Provided hostname argument has no value`)
process.exit(1)
}
if (indexOfConfig !== false) {
nuxtConfigFileName = process.argv.slice(indexOfConfig)[1]
process.argv = without(process.argv, '--config-file', '-c', nuxtConfigFileName)
if (argv.help) {
console.log(`
Description
Starts the application in development mode (hot-code reloading, error
reporting, etc)
Usage
$ nuxt dev <dir> -p <port number> -H <hostname>
Options
--port, -p A port number on which to start the application
--hostname, -H Hostname on which to start the application
--config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
--help, -h Displays this message
`)
process.exit(0)
}
var rootDir = resolve(process.argv.slice(2)[0] || '.')
var nuxtConfigFile = resolve(rootDir, nuxtConfigFileName)
var rootDir = resolve(argv._[0] || '.')
var nuxtConfigFile = resolve(rootDir, argv['config-file'])
var options = {}
if (fs.existsSync(nuxtConfigFile)) {
options = require(nuxtConfigFile)
} else if (argv['config-file'] !== 'nuxt.config.js') {
console.error(`> Could not load config file ${argv['config-file']}`)
process.exit(1)
}
if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
}
options.dev = true // Add hot reloading and watching changes
// Force development mode: add hot reloading and watching changes
options.dev = true
var nuxt = module.exports = new Nuxt(options)
var port = process.env.PORT || process.env.npm_package_config_nuxt_port
var host = process.env.HOST || process.env.npm_package_config_nuxt_host
var port = argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
var host = argv.hostname || process.env.HOST || process.env.npm_package_config_nuxt_host
var server = nuxt.server = new nuxt.Server(nuxt).listen(port, host)
listenOnConfigChanges(nuxt, server)
@ -74,7 +99,6 @@ function listenOnConfigChanges(nuxt, server) {
process.exit(1)
})
}, 200)
var nuxtConfigFile = resolve(rootDir, nuxtConfigFileName)
chokidar.watch(nuxtConfigFile, Object.assign({}, nuxt.options.watchers.chokidar, {ignoreInitial: true}))
chokidar.watch(nuxtConfigFile, Object.assign({}, nuxt.options.watchers.chokidar, { ignoreInitial: true }))
.on('all', build)
}

View File

@ -4,15 +4,44 @@
process.env.DEBUG = 'nuxt:*'
var fs = require('fs')
var parseArgs = require('minimist')
var Nuxt = require('../')
var resolve = require('path').resolve
var rootDir = resolve(process.argv.slice(2)[0] || '.')
var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
var argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
c: 'config-file'
},
boolean: ['h'],
string: ['c'],
default: {
c: 'nuxt.config.js'
}
})
if (argv.help) {
console.log(`
Description
Generate a static web application (server-rendered)
Usage
$ nuxt generate <dir>
Options
--config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
--help, -h Displays this message
`)
process.exit(0)
}
var rootDir = resolve(argv._[0] || '.')
var nuxtConfigFile = resolve(rootDir, argv['config-file'])
var options = {}
if (fs.existsSync(nuxtConfigFile)) {
options = require(nuxtConfigFile)
} else if (argv['config-file'] !== 'nuxt.config.js') {
console.error(`> Could not load config file ${argv['config-file']}`)
process.exit(1)
}
if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
@ -22,10 +51,10 @@ options.dev = false // Force production mode (no webpack middleware called)
console.log('[nuxt] Generating...') // eslint-disable-line no-console
var nuxt = module.exports = new Nuxt(options)
nuxt.generate()
.then(() => {
.then(() => {
console.log('[nuxt] Generate done') // eslint-disable-line no-console
})
.catch((err) => {
})
.catch((err) => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})
})

View File

@ -1,22 +1,69 @@
#!/usr/bin/env node
var fs = require('fs')
var parseArgs = require('minimist')
var Nuxt = require('../')
var resolve = require('path').resolve
var join = require('path').join
var rootDir = resolve(process.argv.slice(2)[0] || '.')
var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
var argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
H: 'hostname',
p: 'port',
c: 'config-file'
},
boolean: ['h'],
string: ['H', 'c'],
default: {
c: 'nuxt.config.js'
}
})
if (argv.hostname === '') {
console.error(`> Provided hostname argument has no value`)
process.exit(1)
}
if (argv.help) {
console.log(`
Description
Starts the application in production mode.
The application should be compiled with \`nuxt build\` first.
Usage
$ nuxt start <dir> -p <port number> -H <hostname>
Options
--port, -p A port number on which to start the application
--hostname, -H Hostname on which to start the application
--config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
--help, -h Displays this message
`)
process.exit(0)
}
var rootDir = resolve(argv._[0] || '.')
var nuxtConfigFile = resolve(rootDir, argv['config-file'])
var options = {}
if (fs.existsSync(nuxtConfigFile)) {
options = require(nuxtConfigFile)
} else if (argv['config-file'] !== 'nuxt.config.js') {
console.error(`> Could not load config file ${argv['config-file']}`)
process.exit(1)
}
if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
}
options.dev = false // Force production mode (no webpack middleware called)
var buildDir = join(options.rootDir, (options.buildDir || '.nuxt'), 'dist', 'server-bundle.json')
// Check if project is built for production
if (!fs.existsSync(buildDir)) {
console.error('> No build files found, please run `nuxt build` before launching `nuxt start`') // eslint-disable-line no-console
process.exit(1)
}
var nuxt = module.exports = new Nuxt(options)
var port = process.env.PORT || process.env.npm_package_config_nuxt_port
var host = process.env.HOST || process.env.npm_package_config_nuxt_host
var port = argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
var host = argv.hostname || process.env.HOST || process.env.npm_package_config_nuxt_host
var server = nuxt.server = new nuxt.Server(nuxt).listen(port, host)

View File

@ -80,6 +80,7 @@
"html-webpack-plugin": "^2.28.0",
"lodash": "^4.17.4",
"memory-fs": "^0.4.1",
"minimist": "^1.2.0",
"offline-plugin": "^4.8.1",
"opencollective": "^1.0.3",
"pify": "^3.0.0",

573
yarn.lock

File diff suppressed because it is too large Load Diff