docusaurus/lib/start-server.js

85 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-07-07 13:28:29 -04:00
#!/usr/bin/env node
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
2017-07-07 13:28:29 -04:00
*/
require('babel-register')({
babelrc: false,
only: [__dirname, `${process.cwd()}/core`],
plugins: [
require('./server/translate-plugin.js'),
'transform-class-properties',
'transform-object-rest-spread',
],
presets: ['react', 'env'],
2017-07-07 13:28:29 -04:00
});
const chalk = require('chalk');
const fs = require('fs');
const program = require('commander');
const openBrowser = require('react-dev-utils/openBrowser');
const tcpPortUsed = require('tcp-port-used');
2017-08-09 18:43:30 -04:00
const CWD = process.cwd();
const env = require('./server/env.js');
2017-08-09 18:43:30 -04:00
if (!fs.existsSync(`${CWD}/siteConfig.js`)) {
console.error(
chalk.red('Error: No siteConfig.js file found in website folder!')
);
process.exit(1);
2017-08-09 18:43:30 -04:00
}
if (env.versioning.enabled && env.versioning.missingVersionsPage) {
env.versioning.printMissingVersionsPageError();
process.exit(1);
}
program
.option('--port <number>', 'Specify port number')
.option('--no-watch', 'Toggle live reload file watching')
.parse(process.argv);
2017-07-13 16:40:24 -04:00
let port = parseInt(program.port, 10) || process.env.PORT || 3000;
let numAttempts = 0;
const MAX_ATTEMPTS = 10;
2018-04-10 11:48:44 -04:00
function checkPort() {
tcpPortUsed
.check(port, 'localhost')
.then(inUse => {
if (inUse && numAttempts >= MAX_ATTEMPTS) {
console.log(
'Reached max attempts, exiting. Please open up some ports or ' +
'increase the number of attempts and try again.'
);
process.exit(1);
2018-04-10 11:48:44 -04:00
} else if (inUse) {
console.error(chalk.red(`Port ${port} is in use`));
2018-04-10 11:48:44 -04:00
// Try again but with port + 1
port += 1;
numAttempts += 1;
checkPort();
} else {
// start local server on specified port
const server = require('./server/server.js');
server(port, program.opts());
const {baseUrl} = require(`${CWD}/siteConfig.js`);
const host = `http://localhost:${port}${baseUrl}`;
2018-05-17 19:17:56 -04:00
console.log('Docusaurus server started on port %d', port);
openBrowser(host);
2018-04-10 11:48:44 -04:00
}
})
.catch(ex => {
setTimeout(() => {
2018-04-10 11:48:44 -04:00
throw ex;
}, 0);
});
}
checkPort();