1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | process.on('unhandledRejection', err => {
|
17 | throw err;
|
18 | });
|
19 |
|
20 | process.env.NODE_ENV = 'development';
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | require('dotenv').config({ silent: true });
|
27 |
|
28 | const fs = require('fs');
|
29 | const chalk = require('chalk');
|
30 | const detect = require('detect-port');
|
31 | const WebpackDevServer = require('webpack-dev-server');
|
32 | const clearConsole = require('react-dev-utils/clearConsole');
|
33 | const { exec, execSync } = require('child_process');
|
34 | const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
|
35 | const getProcessForPort = require('react-dev-utils/getProcessForPort');
|
36 | const spawn = require('cross-spawn');
|
37 | const prompt = require('react-dev-utils/prompt');
|
38 | const paths = require('../config/paths');
|
39 | const config = require('../config/webpack.config.dev');
|
40 | const devServerConfig = require('../config/webpackDevServer.config');
|
41 | const createWebpackCompiler = require('./utils/createWebpackCompiler');
|
42 | const addWebpackMiddleware = require('./utils/addWebpackMiddleware');
|
43 |
|
44 | const useYarn = fs.existsSync(paths.yarnLockFile);
|
45 |
|
46 | const elec = commandExists('electron');
|
47 | const cli = useYarn ? 'yarn' : 'npm';
|
48 | const isInteractive = process.stdout.isTTY;
|
49 |
|
50 |
|
51 | if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) {
|
52 | process.exit(1);
|
53 | }
|
54 |
|
55 |
|
56 | const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000;
|
57 |
|
58 | function 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 |
|
89 | function 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 |
|
111 | const compiler = createWebpackCompiler(config, onReady);
|
112 |
|
113 |
|
114 | const devServer = new WebpackDevServer(compiler, devServerConfig);
|
115 |
|
116 |
|
117 | addWebpackMiddleware(devServer);
|
118 |
|
119 |
|
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 |
|
134 |
|
135 | detect(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 |
|
153 | function commandExists(command) {
|
154 | try {
|
155 | execSync('which ' + command);
|
156 | } catch (err) {
|
157 | return false;
|
158 | }
|
159 | return true;
|
160 | }
|