diff --git a/bin/nuxt b/bin/nuxt index 4e9019757d..e0b15c6096 100755 --- a/bin/nuxt +++ b/bin/nuxt @@ -3,17 +3,18 @@ var join = require('path').join var defaultCommand = 'dev' -var commands = new Set([ - defaultCommand, - 'init', - 'build', - 'start', - 'generate' -]) +var commands = [] +try { + commands = require('fs').readdirSync(__dirname) +} catch (e) { + commands = [] +} +commands.push(defaultCommand, 'init') +commands = new Set(commands) var cmd = process.argv[2] -if (commands.has(cmd)) { +if (commands.has('nuxt-' + cmd)) { process.argv.splice(2, 1) } else { cmd = defaultCommand diff --git a/bin/nuxt-ddvdev b/bin/nuxt-ddvdev new file mode 100644 index 0000000000..c1adcfe028 --- /dev/null +++ b/bin/nuxt-ddvdev @@ -0,0 +1,28 @@ +#!/usr/bin/env node + +process.env.NOTLISTEN = true +const fs = require('fs') +const worker = require('ddv-worker') +const nuxtDev = require('./nuxt-dev') + +worker.server = nuxtDev.server + +var options = {} +if (fs.existsSync(nuxtDev.nuxtConfigFile)) { + options = require(nuxtDev.nuxtConfigFile) +} + +worker.DEBUG = true // Force production mode (no webpack middlewares called) + +worker.updateServerConf({ + defaultListen: options.defaultListen, + listen: options.listen, + cpuLen: options.cpuLen +}, (e) => { + if (e) { + console.error('service config update fail') // eslint-disable-line no-console + console.error(e) // eslint-disable-line no-console + } else { + console.log('service config update successful') // eslint-disable-line no-console + } +}) diff --git a/bin/nuxt-ddvstart b/bin/nuxt-ddvstart new file mode 100644 index 0000000000..5146ae0f30 --- /dev/null +++ b/bin/nuxt-ddvstart @@ -0,0 +1,28 @@ +#!/usr/bin/env node + +process.env.NOTLISTEN = true +const fs = require('fs') +const worker = require('ddv-worker') +const nuxtStart = require('./nuxt-start') + +worker.server = nuxtStart.server + +var options = {} +if (fs.existsSync(nuxtStart.nuxtConfigFile)) { + options = require(nuxtStart.nuxtConfigFile) +} + +worker.DEBUG = false // Force production mode (no webpack middlewares called) + +worker.updateServerConf({ + defaultListen: options.defaultListen, + listen: options.listen, + cpuLen: options.cpuLen +}, (e) => { + if (e) { + console.error('service config update fail') // eslint-disable-line no-console + console.error(e) // eslint-disable-line no-console + } else { + console.log('service config update successful') // eslint-disable-line no-console + } +}) diff --git a/bin/nuxt-dev b/bin/nuxt-dev index a54b99f9ba..757139a832 100755 --- a/bin/nuxt-dev +++ b/bin/nuxt-dev @@ -10,13 +10,14 @@ var fs = require('fs') var Nuxt = require('../') var chokidar = require('chokidar') var resolve = require('path').resolve - +var server = module.exports.server = require('http').createServer() var rootDir = resolve(process.argv.slice(2)[0] || '.') var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js') var options = {} if (fs.existsSync(nuxtConfigFile)) { options = require(nuxtConfigFile) + module.exports.nuxtConfigFile = nuxtConfigFile } if (typeof options.rootDir !== 'string') { options.rootDir = rootDir @@ -26,16 +27,18 @@ options.dev = true // Add hot reloading and watching changes var nuxt = new Nuxt(options) nuxt.build() .then(() => { - var server = new nuxt.Server(nuxt) - .listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host) - listenOnConfigChanges(nuxt, server) + var nuxtServer = new nuxt.Server(nuxt, server) + if (!process.env.NOTLISTEN) { + nuxtServer.listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host) + } + listenOnConfigChanges(nuxt, nuxtServer) }) .catch((err) => { console.error(err) // eslint-disable-line no-console process.exit(1) }) -function listenOnConfigChanges (nuxt, server) { +function listenOnConfigChanges (nuxt, nuxtServer) { // Listen on nuxt.config.js changes var build = _.debounce(() => { debug('[nuxt.config.js] changed') @@ -56,7 +59,7 @@ function listenOnConfigChanges (nuxt, server) { return new Nuxt(options).build() }) .then((nuxt) => { - server.nuxt = nuxt + nuxtServer.nuxt = nuxt }) .catch((error) => { console.error('Error while rebuild the app:', error) // eslint-disable-line no-console diff --git a/bin/nuxt-start b/bin/nuxt-start index 0f9aef3577..1fd2d95c55 100755 --- a/bin/nuxt-start +++ b/bin/nuxt-start @@ -18,8 +18,13 @@ options.dev = false // Force production mode (no webpack middlewares called) var nuxt = new Nuxt(options) -new nuxt.Server(nuxt) -.listen( - process.env.PORT || process.env.npm_package_config_nuxt_port, - process.env.HOST || process.env.npm_package_config_nuxt_host -) +var nuxtServer = new nuxt.Server(nuxt) +module.exports.server = nuxtServer.server +module.exports.nuxtConfigFile = nuxtConfigFile + +if (!process.env.NOTLISTEN) { + nuxtServer.listen( + process.env.PORT || process.env.npm_package_config_nuxt_port, + process.env.HOST || process.env.npm_package_config_nuxt_host + ) +} diff --git a/lib/server.js b/lib/server.js index 1b9efae6d7..9f1210c599 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,12 +1,19 @@ 'use strict' const http = require('http') +const https = require('https') class Server { - - constructor (nuxt) { + constructor (nuxt, server, isUserListen) { this.nuxt = nuxt - this.server = http.createServer(this.render.bind(this)) + if (server instanceof http.Server && server instanceof https.Server) { + this.isUserListen = isUserListen === void 0 ? true : isUserListen + this.server = server + } else { + this.isUserListen = false + this.server = http.createServer() + } + this.server.on('request', this.render.bind(this)) return this } @@ -16,11 +23,16 @@ class Server { } listen (port, host) { - host = host || 'localhost' - port = port || 3000 - this.server.listen(port, host, () => { - console.log('Ready on http://%s:%s', host, port) // eslint-disable-line no-console - }) + if (this.isUserListen !== true) { + host = host || 'localhost' + port = port || 3000 + this.server.listen(port, host, () => { + console.log('Ready on http://%s:%s', host, port) // eslint-disable-line no-console + }) + } else { + console.log('Please use the incoming server to establish listen') // eslint-disable-line no-console + } + return this } diff --git a/package.json b/package.json index 60ea6875bb..b36a329fae 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,8 @@ "webpack": "^2.2.0", "webpack-bundle-analyzer": "^2.2.1", "webpack-dev-middleware": "^1.9.0", - "webpack-hot-middleware": "^2.15.0" + "webpack-hot-middleware": "^2.15.0", + "ddv-worker": "latest" }, "devDependencies": { "ava": "^0.17.0",