Add ddv virtual host management worker support

Add [ddv](https://github.com/ddvjs/ddv) virtual host management worker
support

update pageage.json
This commit is contained in:
yuchonghua 2017-01-21 02:35:41 +08:00
parent e016b5d184
commit bbed4dbd02
7 changed files with 110 additions and 32 deletions

View File

@ -3,17 +3,18 @@
var join = require('path').join var join = require('path').join
var defaultCommand = 'dev' var defaultCommand = 'dev'
var commands = new Set([ var commands = []
defaultCommand, try {
'init', commands = require('fs').readdirSync(__dirname)
'build', } catch (e) {
'start', commands = []
'generate' }
]) commands.push(defaultCommand, 'init')
commands = new Set(commands)
var cmd = process.argv[2] var cmd = process.argv[2]
if (commands.has(cmd)) { if (commands.has('nuxt-' + cmd)) {
process.argv.splice(2, 1) process.argv.splice(2, 1)
} else { } else {
cmd = defaultCommand cmd = defaultCommand

28
bin/nuxt-ddvdev Executable file
View File

@ -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
}
})

28
bin/nuxt-ddvstart Executable file
View File

@ -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
}
})

View File

@ -10,13 +10,14 @@ var fs = require('fs')
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 server = module.exports.server = require('http').createServer()
var rootDir = resolve(process.argv.slice(2)[0] || '.') var rootDir = resolve(process.argv.slice(2)[0] || '.')
var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js') var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
var options = {} var options = {}
if (fs.existsSync(nuxtConfigFile)) { if (fs.existsSync(nuxtConfigFile)) {
options = require(nuxtConfigFile) options = require(nuxtConfigFile)
module.exports.nuxtConfigFile = nuxtConfigFile
} }
if (typeof options.rootDir !== 'string') { if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir options.rootDir = rootDir
@ -26,16 +27,18 @@ options.dev = true // Add hot reloading and watching changes
var nuxt = new Nuxt(options) var nuxt = new Nuxt(options)
nuxt.build() nuxt.build()
.then(() => { .then(() => {
var server = new nuxt.Server(nuxt) var nuxtServer = new nuxt.Server(nuxt, server)
.listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host) if (!process.env.NOTLISTEN) {
listenOnConfigChanges(nuxt, server) 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) => { .catch((err) => {
console.error(err) // eslint-disable-line no-console console.error(err) // eslint-disable-line no-console
process.exit(1) process.exit(1)
}) })
function listenOnConfigChanges (nuxt, server) { function listenOnConfigChanges (nuxt, nuxtServer) {
// Listen on nuxt.config.js changes // Listen on nuxt.config.js changes
var build = _.debounce(() => { var build = _.debounce(() => {
debug('[nuxt.config.js] changed') debug('[nuxt.config.js] changed')
@ -56,7 +59,7 @@ function listenOnConfigChanges (nuxt, server) {
return new Nuxt(options).build() return new Nuxt(options).build()
}) })
.then((nuxt) => { .then((nuxt) => {
server.nuxt = nuxt nuxtServer.nuxt = nuxt
}) })
.catch((error) => { .catch((error) => {
console.error('Error while rebuild the app:', error) // eslint-disable-line no-console console.error('Error while rebuild the app:', error) // eslint-disable-line no-console

View File

@ -18,8 +18,13 @@ options.dev = false // Force production mode (no webpack middlewares called)
var nuxt = new Nuxt(options) var nuxt = new Nuxt(options)
new nuxt.Server(nuxt) var nuxtServer = new nuxt.Server(nuxt)
.listen( module.exports.server = nuxtServer.server
process.env.PORT || process.env.npm_package_config_nuxt_port, module.exports.nuxtConfigFile = nuxtConfigFile
process.env.HOST || process.env.npm_package_config_nuxt_host
) 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
)
}

View File

@ -1,12 +1,18 @@
'use strict' 'use strict'
const http = require('http') const http = require('http')
const https = require('https')
class Server { class Server {
constructor (nuxt, server) {
constructor (nuxt) {
this.nuxt = nuxt this.nuxt = nuxt
this.server = http.createServer(this.render.bind(this)) if (server instanceof http.Server && server instanceof https.Server) {
this.isUserServer = true
} else {
this.isUserServer = false
this.server = http.createServer()
}
this.server.on('request', this.render.bind(this))
return this return this
} }
@ -16,11 +22,16 @@ class Server {
} }
listen (port, host) { listen (port, host) {
host = host || 'localhost' if (this.isUserServer !== true) {
port = port || 3000 host = host || 'localhost'
this.server.listen(port, host, () => { port = port || 3000
console.log('Ready on http://%s:%s', host, port) // eslint-disable-line no-console 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 return this
} }

View File

@ -46,7 +46,7 @@
}, },
"dependencies": { "dependencies": {
"ansi-html": "^0.0.7", "ansi-html": "^0.0.7",
"autoprefixer": "^6.6.0", "autoprefixer": "^6.7.0",
"babel-core": "^6.22.1", "babel-core": "^6.22.1",
"babel-loader": "^6.2.10", "babel-loader": "^6.2.10",
"babel-plugin-array-includes": "^2.0.3", "babel-plugin-array-includes": "^2.0.3",
@ -80,25 +80,27 @@
"vue-template-compiler": "^2.1.10", "vue-template-compiler": "^2.1.10",
"vuex": "^2.1.1", "vuex": "^2.1.1",
"webpack": "^2.2.0", "webpack": "^2.2.0",
"webpack-bundle-analyzer": "^2.2.1",
"webpack-dev-middleware": "^1.9.0", "webpack-dev-middleware": "^1.9.0",
"webpack-hot-middleware": "^2.14.0" "webpack-hot-middleware": "^2.15.0",
"ddv-worker": "latest"
}, },
"devDependencies": { "devDependencies": {
"ava": "^0.17.0", "ava": "^0.17.0",
"babel-eslint": "^7.1.1", "babel-eslint": "^7.1.1",
"codecov": "^1.0.1", "codecov": "^1.0.1",
"copy-webpack-plugin": "^4.0.1", "copy-webpack-plugin": "^4.0.1",
"eslint": "^3.10.2", "eslint": "^3.14.0",
"eslint-config-standard": "^6.2.1", "eslint-config-standard": "^6.2.1",
"eslint-plugin-html": "^1.7.0", "eslint-plugin-html": "^1.7.0",
"eslint-plugin-promise": "^3.4.0", "eslint-plugin-promise": "^3.4.0",
"eslint-plugin-standard": "^2.0.1", "eslint-plugin-standard": "^2.0.1",
"finalhandler": "^0.5.1", "finalhandler": "^0.5.1",
"jsdom": "^9.8.3", "jsdom": "^9.9.1",
"json-loader": "^0.5.4", "json-loader": "^0.5.4",
"nyc": "^10.1.2", "nyc": "^10.1.2",
"request": "^2.79.0", "request": "^2.79.0",
"request-promise-native": "^1.0.3", "request-promise-native": "^1.0.3",
"webpack-node-externals": "^1.5.4" "webpack-node-externals": "^1.5.4"
} }
} }