sonarqube/server/sonar-web/scripts/start.js

135 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-08-22 15:29:35 +02:00
/*
* SonarQube
2020-01-06 15:01:53 +01:00
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
2016-08-22 15:29:35 +02:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
2017-06-14 11:10:48 +02:00
/* eslint-disable no-console */
2016-08-22 15:29:35 +02:00
process.env.NODE_ENV = 'development';
const fs = require('fs');
2017-06-14 11:10:48 +02:00
const chalk = require('chalk');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const clearConsole = require('react-dev-utils/clearConsole');
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
2017-06-19 14:41:19 +02:00
const errorOverlayMiddleware = require('react-error-overlay/middleware');
const getMessages = require('./utils/getMessages');
const getConfigs = require('../config/webpack.config');
2017-06-14 11:10:48 +02:00
const paths = require('../config/paths');
2016-08-22 15:29:35 +02:00
const configs = getConfigs({ production: false });
const config = configs.find(config => config.name === 'modern');
2016-08-22 15:29:35 +02:00
2017-06-19 14:41:19 +02:00
const port = process.env.PORT || 3000;
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
const host = process.env.HOST || 'localhost';
const proxy = process.env.PROXY || 'http://localhost:9000';
2016-08-22 15:29:35 +02:00
// Force start script to proxy l10n request to the server (can be useful when working with plugins/extensions)
const l10nCompiledFlag = process.argv.findIndex(val => val === 'l10nCompiled') >= 0;
const l10nExtensions = process.argv.findIndex(val => val === 'l10nExtensions') >= 0;
2017-06-19 14:41:19 +02:00
const compiler = setupCompiler(host, port, protocol);
2017-06-19 14:41:19 +02:00
runDevServer(compiler, host, port, protocol);
2016-08-22 15:29:35 +02:00
2017-06-14 11:10:48 +02:00
function setupCompiler(host, port, protocol) {
2017-06-19 14:41:19 +02:00
const compiler = webpack(config);
2016-08-22 15:29:35 +02:00
2017-06-14 11:10:48 +02:00
compiler.plugin('invalid', () => {
2016-10-27 13:37:55 +02:00
clearConsole();
console.log('Compiling...');
2016-08-22 15:29:35 +02:00
});
2017-06-14 11:10:48 +02:00
compiler.plugin('done', stats => {
2016-10-27 13:37:55 +02:00
clearConsole();
2016-08-22 15:29:35 +02:00
2017-06-14 11:10:48 +02:00
const jsonStats = stats.toJson({}, true);
const messages = formatWebpackMessages(jsonStats);
const seconds = jsonStats.time / 1000;
2016-10-27 13:37:55 +02:00
if (!messages.errors.length && !messages.warnings.length) {
console.log(chalk.green('Compiled successfully!'));
2016-11-04 09:09:42 +01:00
console.log('Duration: ' + seconds.toFixed(2) + 's');
2016-10-27 13:37:55 +02:00
console.log();
console.log('The app is running at:');
console.log();
console.log(' ' + chalk.cyan(protocol + '://' + host + ':' + port + '/'));
console.log();
}
2016-08-22 15:29:35 +02:00
2016-10-27 13:37:55 +02:00
if (messages.errors.length) {
console.log(chalk.red('Failed to compile.'));
2016-08-22 15:29:35 +02:00
console.log();
2016-10-27 13:37:55 +02:00
messages.errors.forEach(message => {
2016-08-22 15:29:35 +02:00
console.log(message);
console.log();
});
}
});
2017-06-19 14:41:19 +02:00
return compiler;
2016-10-27 13:37:55 +02:00
}
2017-06-19 14:41:19 +02:00
function runDevServer(compiler, host, port, protocol) {
2017-06-14 11:10:48 +02:00
const devServer = new WebpackDevServer(compiler, {
2017-10-25 16:53:22 +02:00
before(app) {
app.use(errorOverlayMiddleware());
if (!l10nCompiledFlag) {
app.get('/api/l10n/index', (req, res) => {
getMessages(l10nExtensions)
.then(messages => res.json({ effectiveLocale: 'en', messages }))
.catch(() => res.status(500));
});
}
2017-10-25 16:53:22 +02:00
},
compress: true,
2016-10-27 13:37:55 +02:00
clientLogLevel: 'none',
contentBase: [paths.appPublic, paths.docRoot],
disableHostCheck: true,
2016-10-27 13:37:55 +02:00
hot: true,
publicPath: config.output.publicPath,
quiet: true,
watchOptions: {
ignored: /node_modules/
},
2017-06-14 11:10:48 +02:00
https: protocol === 'https',
2017-06-19 14:41:19 +02:00
host,
overlay: false,
historyApiFallback: {
disableDotRule: true
},
proxy: {
2018-06-11 17:16:04 +02:00
'/api': { target: proxy, changeOrigin: true },
'/static': { target: proxy, changeOrigin: true },
'/integration': { target: proxy, changeOrigin: true },
'/sessions/init': { target: proxy, changeOrigin: true },
'/oauth2': { target: proxy, changeOrigin: true },
'/batch': { target: proxy, changeOrigin: true }
2017-06-19 14:41:19 +02:00
}
2016-10-27 13:37:55 +02:00
});
2017-06-14 11:10:48 +02:00
devServer.listen(port, err => {
2016-08-22 15:29:35 +02:00
if (err) {
2017-10-25 16:53:22 +02:00
console.log(err);
return;
2016-08-22 15:29:35 +02:00
}
2016-10-27 13:37:55 +02:00
clearConsole();
console.log(chalk.cyan('Starting the development server...'));
2016-08-22 15:29:35 +02:00
console.log();
});
}