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 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

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 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

View File

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

View File

@ -1,12 +1,18 @@
'use strict'
const http = require('http')
const https = require('https')
class Server {
constructor (nuxt) {
constructor (nuxt, server) {
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
}
@ -16,11 +22,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.isUserServer !== 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
}

View File

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