Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic port switching #516

Merged
merged 8 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions lib/start-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,34 @@ const program = require('commander');

program.option('--port <number>', 'Specify port number').parse(process.argv);

const port = parseInt(program.port, 10) || 3000;

tcpPortUsed
.check(port, 'localhost')
.then(function(inUse) {
if (inUse) {
console.error(chalk.red('Port ' + port + ' is in use'));
process.exit(1);
} else {
console.log('Starting Docusaurus server on port ' + port + '...');
// start local server on specified port
const server = require('./server/server.js');
server(port);
}
})
.catch(function(ex) {
setTimeout(function() {
throw ex;
}, 0);
});
var port = process.env.PORT || 3000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the behavior from listening to the --port argument on the command line to use the PORT environment variable.

Copy link
Contributor

@yangshun yangshun Apr 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this line.

EDIT: Just saw #588

var numAttempts = 0;
var maxAttempts = 10;
checkPort();

function checkPort() {
tcpPortUsed
.check(port, 'localhost')
.then(function(inUse) {
if (inUse && numAttempts >= maxAttempts) {
console.log("Reached max attempts, exiting. Please open up some ports or increase the number of attempts and try again.")
process.exit(1)
} else if (inUse) {
console.error(chalk.red('Port ' + port + ' is in use'));
// Try again but with port + 1
port += 1;
numAttempts += 1;
checkPort();
} else {
console.log('Starting Docusaurus server on port ' + port + '...');
// start local server on specified port
const server = require('./server/server.js');
server(port);
}
})
.catch(function(ex) {
setTimeout(function() {
throw ex;
}, 0);
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"url": "https://github.com/facebook/Docusaurus.git"
},
"scripts": {
"ci-check": "yarn prettier:diff",
"ci-check": "yarn prettier && yarn prettier:diff",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@InternetExplorer7 This should not be added here. yarn ci-check is run in Circle this change makes the code prettified before diffing, so it is guaranteed to pass.

The check for whether the code has been prettified is essentially rendered ineffective by this change as even if submitted code has been prettified, this command will fix it and pass the CI but the changes are not committed to the branch.

I'll submit a PR to revert this line. cc @JoelMarcey

"format:source": "prettier --config .prettierrc --write \"lib/**/*.js\"",
"format:examples": "prettier --config .prettierrc --write \"examples/**/*.js\"",
"nit:source": "prettier --config .prettierrc --list-different \"lib/**/*.js\"",
Expand Down