Last active
August 29, 2015 14:24
-
-
Save geta6/bbce982d9a47e882c359 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const _ = require('lodash'); | |
| const cp = require('child_process'); | |
| const util = require('gulp-util'); | |
| class DevServer { | |
| constructor(options) { | |
| process.env.PORT = process.env.PORT || 3000; | |
| options = options || {}; | |
| this.script = options.script; | |
| this.browser = !!options.browser; | |
| this.browserSync = _.defaults((options.browserSync || {}), { | |
| port: process.env.PORT, | |
| notify: false, | |
| ghostMode: false, | |
| logPrefix: this.script, | |
| logConnections: true | |
| }); | |
| this.server = false; | |
| this.client = false; | |
| this.info = {}; | |
| } | |
| restart() { | |
| return Promise.resolve() | |
| .then(() => this.stop()) | |
| .then(() => this.start()) | |
| .then(() => this.proxy()); | |
| } | |
| start() { | |
| return new Promise((resolve, reject) => { | |
| this.server = cp.fork(this.script, {env: _.extend({}, process.env, {PORT: this.info.port || ''})}); | |
| this.server.on('close', () => { | |
| this.server = null; | |
| reject(util.PluginError(this.script, 'Fail to startup server.')); | |
| }); | |
| this.server.on('message', res => { | |
| if (res.message && res.message === 'online') { | |
| this.server.removeAllListeners(); | |
| this.info = res; | |
| util.log(`${this.script} listening on '${util.colors.green(`${this.info.address}:${this.info.port}`)}'`); | |
| resolve(); | |
| } | |
| }); | |
| }); | |
| } | |
| stop() { | |
| return new Promise(resolve => { | |
| if (this.server) { | |
| this.server.removeAllListeners(); | |
| this.server.once('close', () => resolve()); | |
| this.server.kill(); | |
| } else { | |
| resolve(); | |
| } | |
| }); | |
| } | |
| proxy() { | |
| return new Promise(resolve => { | |
| if (this.browser) { | |
| if (this.client) { | |
| this.client.reload(); | |
| resolve(); | |
| } else { | |
| this.client = require('browser-sync'); | |
| this.client(_.extend(this.browserSync, {proxy: `${this.info.address}:${this.info.port}`}), () => resolve()); | |
| } | |
| } else { | |
| resolve(); | |
| } | |
| }); | |
| } | |
| } | |
| module.exports = DevServer; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const DevServer = require('./DevServer'); | |
| const server = new DevServer({script: './server.js', browser: true}); | |
| gulp.task('server:restart', function() { | |
| return server.restart(); | |
| }); | |
| gulp.task('default', function() { | |
| gulp.watch('./**/*.js', ['server:restart']); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| 'use strict'; | |
| const app = require('./app.js'); | |
| const server = app.listen(process.env.PORT, process.env.HOST, ~~process.env.BACKLOG, function() { | |
| process.send && process.send({message: 'online', address: server.address().address, port: server.address().port}); | |
| }); | |
| server.timeout = ~~process.env.TIMEOUT; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment