Merge branch '21224-project-details'
[arvados.git] / services / workbench2 / scripts / start.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 'use strict';
6
7 // Do this as the first thing so that any code reading it knows the right env.
8 process.env.BABEL_ENV = 'development';
9 process.env.NODE_ENV = 'development';
10
11 // Makes the script crash on unhandled rejections instead of silently
12 // ignoring them. In the future, promise rejections that are not handled will
13 // terminate the Node.js process with a non-zero exit code.
14 process.on('unhandledRejection', err => {
15   throw err;
16 });
17
18 // Ensure environment variables are read.
19 require('../config/env');
20
21 const fs = require('fs');
22 const chalk = require('react-dev-utils/chalk');
23 const webpack = require('webpack');
24 const WebpackDevServer = require('webpack-dev-server');
25 const clearConsole = require('react-dev-utils/clearConsole');
26 const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
27 const {
28   choosePort,
29   createCompiler,
30   prepareProxy,
31   prepareUrls,
32 } = require('react-dev-utils/WebpackDevServerUtils');
33 const openBrowser = require('react-dev-utils/openBrowser');
34 const semver = require('semver');
35 const paths = require('../config/paths');
36 const configFactory = require('../config/webpack.config');
37 const createDevServerConfig = require('../config/webpackDevServer.config');
38 const getClientEnvironment = require('../config/env');
39 const react = require(require.resolve('react', { paths: [paths.appPath] }));
40
41 const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1));
42 const useYarn = fs.existsSync(paths.yarnLockFile);
43 const isInteractive = process.stdout.isTTY;
44
45 // Warn and crash if required files are missing
46 if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
47   process.exit(1);
48 }
49
50 // Tools like Cloud9 rely on this.
51 const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
52 const HOST = process.env.HOST || '0.0.0.0';
53
54 if (process.env.HOST) {
55   console.log(
56     chalk.cyan(
57       `Attempting to bind to HOST environment variable: ${chalk.yellow(
58         chalk.bold(process.env.HOST)
59       )}`
60     )
61   );
62   console.log(
63     `If this was unintentional, check that you haven't mistakenly set it in your shell.`
64   );
65   console.log(
66     `Learn more here: ${chalk.yellow('https://cra.link/advanced-config')}`
67   );
68   console.log();
69 }
70
71 // We require that you explicitly set browsers and do not fall back to
72 // browserslist defaults.
73 const { checkBrowsers } = require('react-dev-utils/browsersHelper');
74 checkBrowsers(paths.appPath, isInteractive)
75   .then(() => {
76     // We attempt to use the default port but if it is busy, we offer the user to
77     // run on a different port. `choosePort()` Promise resolves to the next free port.
78     return choosePort(HOST, DEFAULT_PORT);
79   })
80   .then(port => {
81     if (port == null) {
82       // We have not found a port.
83       return;
84     }
85
86     const config = configFactory('development');
87     const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
88     const appName = require(paths.appPackageJson).name;
89
90     const useTypeScript = fs.existsSync(paths.appTsConfig);
91     const urls = prepareUrls(
92       protocol,
93       HOST,
94       port,
95       paths.publicUrlOrPath.slice(0, -1)
96     );
97     // Create a webpack compiler that is configured with custom messages.
98     const compiler = createCompiler({
99       appName,
100       config,
101       urls,
102       useYarn,
103       useTypeScript,
104       webpack,
105     });
106     // Load proxy config
107     const proxySetting = require(paths.appPackageJson).proxy;
108     const proxyConfig = prepareProxy(
109       proxySetting,
110       paths.appPublic,
111       paths.publicUrlOrPath
112     );
113     // Serve webpack assets generated by the compiler over a web server.
114     const serverConfig = {
115       ...createDevServerConfig(proxyConfig, urls.lanUrlForConfig),
116       host: HOST,
117       port,
118     };
119     const devServer = new WebpackDevServer(serverConfig, compiler);
120     // Launch WebpackDevServer.
121     devServer.startCallback(() => {
122       if (isInteractive) {
123         clearConsole();
124       }
125
126       if (env.raw.FAST_REFRESH && semver.lt(react.version, '16.10.0')) {
127         console.log(
128           chalk.yellow(
129             `Fast Refresh requires React 16.10 or higher. You are using React ${react.version}.`
130           )
131         );
132       }
133
134       console.log(chalk.cyan('Starting the development server...\n'));
135       openBrowser(urls.localUrlForBrowser);
136     });
137
138     ['SIGINT', 'SIGTERM'].forEach(function (sig) {
139       process.on(sig, function () {
140         devServer.close();
141         process.exit();
142       });
143     });
144
145     if (process.env.CI !== 'true') {
146       // Gracefully exit when stdin ends
147       process.stdin.on('end', function () {
148         devServer.close();
149         process.exit();
150       });
151     }
152   })
153   .catch(err => {
154     if (err && err.message) {
155       console.log(err.message);
156     }
157     process.exit(1);
158   });