mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-13 09:33:54 +00:00
Refactor nuxt commands using minimist
This commit is contained in:
parent
c08801cf53
commit
c9313572bd
@ -4,60 +4,66 @@
|
|||||||
process.env.DEBUG = 'nuxt:*'
|
process.env.DEBUG = 'nuxt:*'
|
||||||
|
|
||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
|
var parseArgs = require('minimist')
|
||||||
var without = require('lodash').without
|
var without = require('lodash').without
|
||||||
var Nuxt = require('../')
|
var Nuxt = require('../')
|
||||||
var resolve = require('path').resolve
|
var resolve = require('path').resolve
|
||||||
|
|
||||||
// --analyze option
|
const argv = parseArgs(process.argv.slice(2), {
|
||||||
var analyzeBuild = false
|
alias: {
|
||||||
if (process.argv.indexOf('--analyze') !== -1 || process.argv.indexOf('-a') !== -1) {
|
h: 'help',
|
||||||
analyzeBuild = true
|
c: 'config-file',
|
||||||
process.argv = without(process.argv, '--analyze', '-a')
|
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'
|
var rootDir = resolve(argv._[0] || '.')
|
||||||
|
var nuxtConfigFile = resolve(rootDir, argv['config-file'])
|
||||||
// --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 options = {}
|
var options = {}
|
||||||
if (fs.existsSync(nuxtConfigFilePath)) {
|
if (fs.existsSync(nuxtConfigFile)) {
|
||||||
options = require(nuxtConfigFilePath)
|
options = require(nuxtConfigFile)
|
||||||
} else {
|
} else if (argv['config-file'] !== 'nuxt.config.js') {
|
||||||
console.log(`Could not locate ${nuxtConfigFilePath}`) // eslint-disable-line no-console
|
console.error(`> Could not load config file ${argv['config-file']}`)
|
||||||
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof options.rootDir !== 'string') {
|
if (typeof options.rootDir !== 'string') {
|
||||||
options.rootDir = rootDir
|
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 || {}
|
options.build = options.build || {}
|
||||||
if (analyzeBuild) {
|
if (argv.analyze) {
|
||||||
options.build.analyze = analyzeBuild
|
options.build.analyze = true
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('[nuxt] Building...') // eslint-disable-line no-console
|
console.log('[nuxt] Building...') // eslint-disable-line no-console
|
||||||
var nuxt = module.exports = new Nuxt(options)
|
var nuxt = module.exports = new Nuxt(options)
|
||||||
nuxt.build()
|
nuxt.build()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log('[nuxt] Building done') // eslint-disable-line no-console
|
console.log('[nuxt] Building done') // eslint-disable-line no-console
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err) // eslint-disable-line no-console
|
console.error(err) // eslint-disable-line no-console
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
|
58
bin/nuxt-dev
58
bin/nuxt-dev
@ -7,41 +7,66 @@ var _ = require('lodash')
|
|||||||
var debug = require('debug')('nuxt:build')
|
var debug = require('debug')('nuxt:build')
|
||||||
debug.color = 2 // force green color
|
debug.color = 2 // force green color
|
||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
|
var parseArgs = require('minimist')
|
||||||
var Nuxt = require('../')
|
var Nuxt = require('../')
|
||||||
var chokidar = require('chokidar')
|
var chokidar = require('chokidar')
|
||||||
var resolve = require('path').resolve
|
var resolve = require('path').resolve
|
||||||
var without = require('lodash').without
|
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
|
if (argv.hostname === '') {
|
||||||
var indexOfConfig = false
|
console.error(`> Provided hostname argument has no value`)
|
||||||
if (process.argv.indexOf('--config-file') !== -1) {
|
process.exit(1)
|
||||||
indexOfConfig = process.argv.indexOf('--config-file')
|
|
||||||
} else if (process.argv.indexOf('-c') !== -1) {
|
|
||||||
indexOfConfig = process.argv.indexOf('-c')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (indexOfConfig !== false) {
|
if (argv.help) {
|
||||||
nuxtConfigFileName = process.argv.slice(indexOfConfig)[1]
|
console.log(`
|
||||||
process.argv = without(process.argv, '--config-file', '-c', nuxtConfigFileName)
|
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 rootDir = resolve(argv._[0] || '.')
|
||||||
var nuxtConfigFile = resolve(rootDir, nuxtConfigFileName)
|
var nuxtConfigFile = resolve(rootDir, argv['config-file'])
|
||||||
|
|
||||||
var options = {}
|
var options = {}
|
||||||
if (fs.existsSync(nuxtConfigFile)) {
|
if (fs.existsSync(nuxtConfigFile)) {
|
||||||
options = require(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') {
|
if (typeof options.rootDir !== 'string') {
|
||||||
options.rootDir = rootDir
|
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 nuxt = module.exports = new Nuxt(options)
|
||||||
var port = process.env.PORT || process.env.npm_package_config_nuxt_port
|
var port = argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
|
||||||
var host = process.env.HOST || process.env.npm_package_config_nuxt_host
|
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)
|
var server = nuxt.server = new nuxt.Server(nuxt).listen(port, host)
|
||||||
|
|
||||||
listenOnConfigChanges(nuxt, server)
|
listenOnConfigChanges(nuxt, server)
|
||||||
@ -74,7 +99,6 @@ function listenOnConfigChanges(nuxt, server) {
|
|||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
}, 200)
|
}, 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)
|
.on('all', build)
|
||||||
}
|
}
|
||||||
|
@ -4,15 +4,44 @@
|
|||||||
process.env.DEBUG = 'nuxt:*'
|
process.env.DEBUG = 'nuxt:*'
|
||||||
|
|
||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
|
var parseArgs = require('minimist')
|
||||||
var Nuxt = require('../')
|
var Nuxt = require('../')
|
||||||
var resolve = require('path').resolve
|
var resolve = require('path').resolve
|
||||||
|
|
||||||
var rootDir = resolve(process.argv.slice(2)[0] || '.')
|
var argv = parseArgs(process.argv.slice(2), {
|
||||||
var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
|
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 = {}
|
var options = {}
|
||||||
if (fs.existsSync(nuxtConfigFile)) {
|
if (fs.existsSync(nuxtConfigFile)) {
|
||||||
options = require(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') {
|
if (typeof options.rootDir !== 'string') {
|
||||||
options.rootDir = rootDir
|
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
|
console.log('[nuxt] Generating...') // eslint-disable-line no-console
|
||||||
var nuxt = module.exports = new Nuxt(options)
|
var nuxt = module.exports = new Nuxt(options)
|
||||||
nuxt.generate()
|
nuxt.generate()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log('[nuxt] Generate done') // eslint-disable-line no-console
|
console.log('[nuxt] Generate done') // eslint-disable-line no-console
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err) // eslint-disable-line no-console
|
console.error(err) // eslint-disable-line no-console
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
|
@ -1,22 +1,69 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
|
var parseArgs = require('minimist')
|
||||||
var Nuxt = require('../')
|
var Nuxt = require('../')
|
||||||
var resolve = require('path').resolve
|
var resolve = require('path').resolve
|
||||||
|
var join = require('path').join
|
||||||
|
|
||||||
var rootDir = resolve(process.argv.slice(2)[0] || '.')
|
var argv = parseArgs(process.argv.slice(2), {
|
||||||
var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
|
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 = {}
|
var options = {}
|
||||||
if (fs.existsSync(nuxtConfigFile)) {
|
if (fs.existsSync(nuxtConfigFile)) {
|
||||||
options = require(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') {
|
if (typeof options.rootDir !== 'string') {
|
||||||
options.rootDir = rootDir
|
options.rootDir = rootDir
|
||||||
}
|
}
|
||||||
options.dev = false // Force production mode (no webpack middleware called)
|
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 nuxt = module.exports = new Nuxt(options)
|
||||||
var port = process.env.PORT || process.env.npm_package_config_nuxt_port
|
var port = argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
|
||||||
var host = process.env.HOST || process.env.npm_package_config_nuxt_host
|
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)
|
var server = nuxt.server = new nuxt.Server(nuxt).listen(port, host)
|
||||||
|
@ -80,6 +80,7 @@
|
|||||||
"html-webpack-plugin": "^2.28.0",
|
"html-webpack-plugin": "^2.28.0",
|
||||||
"lodash": "^4.17.4",
|
"lodash": "^4.17.4",
|
||||||
"memory-fs": "^0.4.1",
|
"memory-fs": "^0.4.1",
|
||||||
|
"minimist": "^1.2.0",
|
||||||
"offline-plugin": "^4.8.1",
|
"offline-plugin": "^4.8.1",
|
||||||
"opencollective": "^1.0.3",
|
"opencollective": "^1.0.3",
|
||||||
"pify": "^3.0.0",
|
"pify": "^3.0.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user