UNPKG

5.05 kBJavaScriptView Raw
1// @remove-on-eject-begin
2/**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 * All rights reserved.
5 *
6 * This source code is licensed under the BSD-style license found in the
7 * LICENSE file in the root directory of this source tree. An additional grant
8 * of patent rights can be found in the PATENTS file in the same directory.
9 */
10// @remove-on-eject-end
11'use strict';
12
13// Makes the script crash on unhandled rejections instead of silently
14// ignoring them. In the future, promise rejections that are not handled will
15// terminate the Node.js process with a non-zero exit code.
16process.on('unhandledRejection', err => {
17 throw err;
18});
19
20process.env.NODE_ENV = 'development';
21
22// Load environment variables from .env file. Suppress warnings using silent
23// if this file is missing. dotenv will never modify any environment variables
24// that have already been set.
25// https://github.com/motdotla/dotenv
26require('dotenv').config({ silent: true });
27
28const fs = require('fs');
29const chalk = require('chalk');
30const detect = require('detect-port');
31const WebpackDevServer = require('webpack-dev-server');
32const clearConsole = require('react-dev-utils/clearConsole');
33const { exec, execSync } = require('child_process');
34const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
35const getProcessForPort = require('react-dev-utils/getProcessForPort');
36const spawn = require('cross-spawn');
37const prompt = require('react-dev-utils/prompt');
38const paths = require('../config/paths');
39const config = require('../config/webpack.config.dev');
40const devServerConfig = require('../config/webpackDevServer.config');
41const createWebpackCompiler = require('./utils/createWebpackCompiler');
42const addWebpackMiddleware = require('./utils/addWebpackMiddleware');
43
44const useYarn = fs.existsSync(paths.yarnLockFile);
45
46const elec = commandExists('electron');
47const cli = useYarn ? 'yarn' : 'npm';
48const isInteractive = process.stdout.isTTY;
49
50// Warn and crash if required files are missing
51if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
52 process.exit(1);
53}
54
55// Tools like Cloud9 rely on this.
56const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
57
58function electronCheck() {
59 return new Promise((resolve, reject) => {
60 if (!elec) {
61 console.log(chalk.red('Electron must be installed globally'));
62 prompt(chalk.green('Install now?'), true).then(ans => {
63 if (ans) {
64 try {
65 if (useYarn) {
66 spawn.sync(cli, ['global', 'add', 'electron'], {
67 stdio: 'inherit'
68 });
69 } else {
70 spawn.sync(cli, ['install', '-g', 'electron'], {
71 stdio: 'inherit'
72 });
73 }
74 } catch (err) {
75 reject(err);
76 }
77 resolve();
78 } else {
79 console.log("Install on your own with 'npm i -g electron'");
80 reject(null);
81 }
82 });
83 } else {
84 resolve();
85 }
86 });
87}
88
89function run(port) {
90 const protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
91 const host = process.env.HOST || 'localhost';
92 const onReady = showInstructions => {
93 if (!showInstructions) {
94 return;
95 }
96 if (!process.env.ELECTRON_LAUNCH && !process.env.ELECTRON_RUNNING) {
97 process.env.ELECTRON_RUNNING = true;
98 spawn('electron', [paths.appPath]);
99 }
100 console.log();
101 console.log('The app is running at:');
102 console.log(` ${chalk.cyan(`${protocol}://${host}:${port}/`)}`);
103 console.log();
104 console.log('Note that the development build is not optimized.');
105 console.log(
106 `To create a production build, use ${chalk.cyan(`${cli} run build`)}.`
107 );
108 console.log();
109 };
110 // Create a webpack compiler that is configured with custom messages.
111 const compiler = createWebpackCompiler(config, onReady);
112
113 // Serve webpack assets generated by the compiler over a web sever.
114 const devServer = new WebpackDevServer(compiler, devServerConfig);
115
116 // Our custom middleware proxies requests to /index.html or a remote API.
117 addWebpackMiddleware(devServer);
118
119 // Launch WebpackDevServer.
120 devServer.listen(port, err => {
121 if (err) {
122 return console.log(err);
123 }
124
125 if (isInteractive) {
126 clearConsole();
127 }
128 console.log(chalk.cyan('Starting the development server...'));
129 console.log();
130 });
131}
132
133// We attempt to use the default port but if it is busy, we offer the user to
134// run on a different port. `detect()` Promise resolves to the next free port.
135detect(DEFAULT_PORT).then(port => {
136 if (port === DEFAULT_PORT) {
137 electronCheck().then(() => run(port)).catch(console.error);
138 return;
139 }
140
141 if (isInteractive) {
142 clearConsole();
143 console.log(
144 chalk.red(`Something is already running on port ${DEFAULT_PORT}.`)
145 );
146 } else {
147 console.log(
148 chalk.red(`Something is already running on port ${DEFAULT_PORT}.`)
149 );
150 }
151});
152
153function commandExists(command) {
154 try {
155 execSync('which ' + command);
156 } catch (err) {
157 return false;
158 }
159 return true;
160}