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 changing #511

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Docusaurus is a project for easily building, deploying, and maintaining open source project websites.

* **Simple to Start** Docusaurus is built to be easy to [get up and running](https://docusaurus.io/docs/en/installation.html) in as little time possible. We've built Docusaurus to handle the website build process so you can focus on your project.
* **Simple to Start** Docusaurus is built to be easy to [get up and running](https://docusaurus.io/docs/en/installation.html) in as little time possible. We've built Docusaurus to handle the website build process so you can focus on your project. Port scanning is now implemented so an available port is now automatically found!
* **Localizable** Docusaurus ships with [localization support](https://docusaurus.io/docs/en/translation.html) via CrowdIn. Empower and grow your international community by translating your documentation.
* **Customizable** While Docusaurus ships with the key pages and sections you need to get started, including a home page, a docs section, a [blog](https://docusaurus.io/docs/en/blog.html), and additional support pages, it is also [customizable](https://docusaurus.io/docs/en/custom-pages.html) as well to ensure you have a site that is [uniquely yours](https://docusaurus.io/docs/en/api-pages.html).

Expand Down
44 changes: 38 additions & 6 deletions docs/getting-started-publishing.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,54 @@ Most of the work to publish to GitHub pages is done for you automatically throug

Two of the required parameters are set in the [`siteConfig.js`](api-site-config.md):

- `organizationName`: The GitHub user or organization that owns the repository. In the case of Docusaurus, that would be the "facebook" GitHub organization.
- `projectName`: The name of the GitHub repository for your project. For example, Docusaurus is hosted at https://github.com/facebook/docusaurus, so our project name in this case would be "docusaurus".
<table style="width: 100%">
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<tr>
<td style="width:140px"><code>organizationName</code></td>
<td>The GitHub user or organization that owns the repository. In the case of Docusaurus, that would be the "facebook" GitHub organization.</td>
</tr>
<tr>
<td style="width:140px"><code>projectName</code></td>
<td>The name of the GitHub repository for your project. For example, Docusaurus is hosted at https://github.com/facebook/docusaurus, so our project name in this case would be "docusaurus".</td>
</tr>
</table>

> Docusaurus also supports deploying [user or organization sites](https://help.github.com/articles/user-organization-and-project-pages/#user--organization-pages). These sites will be served from the `master` branch of the repo. So, you will want to have the Docusaurus infra, your docs, etc. in another branch (e.g., maybe call it `source`). To do this, just set `projectName` to "_username_.github.io" (where _username_ is your username or organization name on GitHub) and `organizationName` to "_username_". The publish script will automatically deploy your site to the root of the `master` branch to be served.

> While we recommend setting the `projectName` and `organizationName` in `siteConfig.js`, you can also use environment variables `ORGANIZATION_NAME` and `PROJECT_NAME`.

One of the required parameters is set as a environment variable:

- `GIT_USER`: The username for a GitHub account that has commit access to this repo. For your own repositories, this will usually be your own GitHub username.
<table style="width: 100%">
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<tr>
<td style="width:140px"><code>GIT_USER</code></td>
<td>The username for a GitHub account that has commit access to this repo. For your own repositories, this will usually be your own GitHub username.</td>
</tr>
</table>

There are also two optional parameters that are set as environment variables:

- `USE_SSH`: If this is set to `true`, then SSH is used instead of HTTPS for the connection to the GitHub repo. HTTPS is the default if this variable is not set.

- `CURRENT_BRANCH`: The branch that contains the latest docs changes that will be deployed. Usually, the branch will be `master`, but it could be any branch (default or otherwise) except for `gh-pages`. If nothing is set for this variable, then the current branch will be used.
<table style="width: 100%">
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<tr>
<td style="width:140px"><code>USE_SSH</code></td>
<td>If this is set to <code>true</code>, then SSH is used instead of HTTPS for the connection to the GitHub repo. HTTPS is the default if this variable is not set.</td>
</tr>
<tr>
<td style="width:140px"><code>CURRENT_BRANCH</code></td>
<td> The branch that contains the latest docs changes that will be deployed. Usually, the branch will be <code>master</code>, but it could be any branch (default or otherwise) except for <code>gh-pages</code>. If nothing is set for this variable, then the current branch will be used.</td>
</tr>
</table>

Once you have the parameter value information, you can go ahead and run the publish script, ensuring you have inserted your own values inside the various parameter placeholders:

Expand Down
56 changes: 43 additions & 13 deletions lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ function execute(port) {
function reloadSiteConfig() {
removeModuleAndChildrenFromCache(join(CWD, 'siteConfig.js'));
siteConfig = require(join(CWD, 'siteConfig.js'));

if (siteConfig.highlight && siteConfig.highlight.hljs) {
siteConfig.highlight.hljs(require('highlight.js'));
}
Expand Down Expand Up @@ -133,7 +132,7 @@ function execute(port) {

app.get(/docs\/.*html$/, (req, res, next) => {
let url = req.path.toString().replace(siteConfig.baseUrl, '');

// let url = 'docs/en/installation.html'
// links is a map from a permalink to an id for each document
let links = {};
Object.keys(Metadata).forEach(id => {
Expand Down Expand Up @@ -524,23 +523,54 @@ function execute(port) {

// "redirect" requests to pages ending with "/" or no extension so that,
// for example, request to "blog" returns same result as "blog/index.html"
app.get(/\/[^\.]*\/?$/, (req, res) => {
app.get(/\/[^\.]*\/?$/, redirect_page);

function redirect_page(req, res) {
let slash = req.path.toString().endsWith('/') ? '' : '/';
request.get(
'http://localhost:' + port + req.path + slash + 'index.html',
(err, response, body) => {
if (!err) {
if (response) {
res.status(response.statusCode).send(body);
let requestURL =
'http://localhost:' + port + req.path + slash + 'index.html';
request.get(requestURL, (err, response, body) => {
if (!err) {
if (response) {
if (response.statusCode === 404) {
// For some reason, http://localhost:3000/blog/.../introducing-docusaurus/index.html
// gives a page with a response code of 200 and a blank page is rendered.
redirect_page_noHTML(req.path, res);
} else {
console.error('No response');
res.status(response.statusCode).send(body);
}
} else {
console.error('request failed:', err);
console.log('No response');
}
} else {
console.log('request failed:', err);
}
);
});
});
}

/*
This function is called when a user wants to visit a page w/o using the .html extension.

e.g.

now, both: http://localhost:3001/docs/en/installation.html
and
http://localhost:3001/docs/en/installation render the same page.
*/
function redirect_page_noHTML(path, res) {
let requestURL = 'http://localhost:' + port + path + '.html';
request.get(requestURL, (err, response, body) => {
if (!err) {
if (response) {
res.status(response.statusCode).send(body);
} else {
console.log('No response');
}
} else {
console.log('request failed:', err);
}
});
}

app.listen(port);
console.log('Open http://localhost:' + port + '/');
Expand Down
46 changes: 26 additions & 20 deletions lib/start-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,29 @@ 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;
checkPort();

function checkPort() {
tcpPortUsed
.check(port, 'localhost')
.then(function(inUse) {
if (inUse) {
console.error(chalk.red('Port ' + port + ' is in use'));
// Try again but with port + 1
port += 1;
checkPort();
// 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);
});
}
Loading