From 7de482de47d5fdc28e4a6a274561f16aa2508e6b Mon Sep 17 00:00:00 2001 From: Pat O'Neill Date: Sat, 9 Dec 2017 10:16:08 -0500 Subject: [PATCH] feat: Add support for port scanning in the development server to use the first available port from 9999 to 10999. (#145) --- generators/app/index.js | 1 + generators/app/package-json.js | 7 ++--- generators/app/templates/_bower.json | 16 ----------- generators/app/templates/scripts/_server.js | 32 +++++++++++++++++++++ 4 files changed, 35 insertions(+), 21 deletions(-) delete mode 100644 generators/app/templates/_bower.json create mode 100644 generators/app/templates/scripts/_server.js diff --git a/generators/app/index.js b/generators/app/index.js index 449d2a30..22917a50 100644 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -215,6 +215,7 @@ module.exports = yeoman.generators.Base.extend({ '_.gitignore', '_.npmignore', 'scripts/_banner.ejs', + 'scripts/_server.js', 'scripts/_version.js' ]; diff --git a/generators/app/package-json.js b/generators/app/package-json.js index 6820a53f..e7796f02 100644 --- a/generators/app/package-json.js +++ b/generators/app/package-json.js @@ -26,6 +26,7 @@ const DEFAULTS = { 'mkdirp': '^0.5.1', 'node-static': '^0.7.9', 'npm-run-all': '^4.0.2', + 'portscanner': '^2.1.1', 'qunitjs': '^2.3.2', 'rimraf': '^2.6.1', 'rollup': '^0.50.0', @@ -173,11 +174,7 @@ const packageJSON = (current, context) => { 'lint': 'vjsstandard', 'prepublish': 'not-in-install && npm run build || in-install', 'start': 'npm-run-all -p start:server watch', - 'start:server': scriptify([ - 'static -a 0.0.0.0 -p 9999', - '-H \'{"Cache-Control": "no-cache, must-revalidate"}\'', - '.' - ]), + 'start:server': 'node scripts/server.js', 'pretest': 'npm-run-all lint build', 'test': 'karma start test/karma.conf.js', 'preversion': 'npm test', diff --git a/generators/app/templates/_bower.json b/generators/app/templates/_bower.json deleted file mode 100644 index 31fdca17..00000000 --- a/generators/app/templates/_bower.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "<%= pluginName %>", - "author": "<%= author %>", - "license": "<%= licenseName %>", - "main": [ -<% if (sass) { -%> - "dist/<%= pluginName %>.css", -<% } -%> - "dist/<%= pluginName %>.min.js" - ], - "keywords": [ - "videojs", - "videojs-plugin" - ] -} - diff --git a/generators/app/templates/scripts/_server.js b/generators/app/templates/scripts/_server.js new file mode 100644 index 00000000..db2bb400 --- /dev/null +++ b/generators/app/templates/scripts/_server.js @@ -0,0 +1,32 @@ +const http = require('http'); +const portscanner = require('portscanner'); +const static = require('node-static'); + +const files = new static.Server(process.cwd(), {cache: false}); + +const server = http.createServer((request, response) => { + response.setHeader('Cache-Control', 'no-cache,must-revalidate'); + + request.addListener('end', () => { + files.serve(request, response, (err) => { + if (err) { + response.writeHead(err.status, err.headers); + response.end('Not Found'); + } + + console.log([ + (new Date()).toISOString(), + `[${response.statusCode}]`, + request.url + ].join(' ')); + }); + }).resume(); +}); + +portscanner.findAPortNotInUse(9999, 10999).then((port) => { + server.listen(port, '0.0.0.0'); + console.log(`serving "." at http://0.0.0.0:${port}`); +}).catch((err) => { + console.log('could not find an open port: ', err); + process.exit(1); +});