Skip to content

Commit

Permalink
feat: Add support for port scanning in the development server to use …
Browse files Browse the repository at this point in the history
…the first available port from 9999 to 10999. (#145)
  • Loading branch information
misteroneill authored Dec 9, 2017
1 parent 338779f commit 7de482d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
1 change: 1 addition & 0 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ module.exports = yeoman.generators.Base.extend({
'_.gitignore',
'_.npmignore',
'scripts/_banner.ejs',
'scripts/_server.js',
'scripts/_version.js'
];

Expand Down
7 changes: 2 additions & 5 deletions generators/app/package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
16 changes: 0 additions & 16 deletions generators/app/templates/_bower.json

This file was deleted.

32 changes: 32 additions & 0 deletions generators/app/templates/scripts/_server.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 7de482d

Please sign in to comment.