IT_DOC

Untitled

Aug 20th, 2020 (edited)
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Utils.js
  2.  
  3. const chalk = require('chalk');
  4. const detect = require('detect-port');
  5. const inquirer = require('inquirer');
  6.  
  7. exports.choosePort = async (defaultPort) => {
  8.   try {
  9.     const port = await detect(defaultPort);
  10.     if (port === defaultPort) {
  11.       return defaultPort;
  12.     }
  13.  
  14.     const message = `Port ${defaultPort} is already in use.`;
  15.  
  16.     if (process.stdout.isTTY) {
  17.       const questionName = 'changePort';
  18.       const question = {
  19.         type: 'confirm',
  20.         name: questionName,
  21.         message: chalk.yellowBright(`
  22.           ${message}
  23.           Do you want to run the app on another port?
  24.         `),
  25.         default: true,
  26.       };
  27.       const result = await inquirer.prompt(question);
  28.  
  29.       return result[questionName] ? port : null;
  30.     }
  31.  
  32.     console.log(chalk.redBright(`${message}`));
  33.   } catch (err) {
  34.     console.log(chalk.redBright('❌❌❌ ERROR ❌❌❌'));
  35.     throw new Error(err.message || err);
  36.   }
  37.  
  38.   return null;
  39. };
  40.  
  41. // Start.js
  42.  
  43. // Core
  44. const webpack = require('webpack');
  45. const DevServer = require('webpack-dev-server');
  46. // const hot = require('webpack-hot-middleware');
  47. const {choosePort} = require('./utils');
  48. const chalk = require('chalk'); // Раскрашивает консоль
  49.  
  50. // Config
  51. const {HOST, PORT} = require('./constants');
  52. const getConfig = require('./webpack.config');
  53. const compiler = webpack(getConfig());
  54.  
  55. (async () => {
  56.   try {
  57.     const choosenPort = await choosePort(PORT);
  58.  
  59.     if (!choosenPort) {
  60.       console.log(chalk.yellowBright('It\'s impossible to run the app :('));
  61.       return null;
  62.     }
  63.  
  64.     const server = new DevServer(compiler, {
  65.       host: HOST,
  66.       port: choosenPort,
  67.       historyApiFallback: true,
  68.       overlay: true,
  69.       quiet: true,
  70.       hot: true,
  71.       clientLogLevel: 'none',
  72.       noInfo: true,
  73.       // // after: (app) => {
  74.       // //   app.use(
  75.       // //     hot(compiler, {
  76.       // //       log: false,
  77.       // //     }),
  78.       // //   );
  79.       // },
  80.     });
  81.  
  82.  
  83.     server.listen(PORT, HOST, () => {
  84.       console.log(`${chalk.greenBright(`Server listening on http://${HOST}:${choosenPort}`)}`);
  85.     });
  86.  
  87.   } catch (err) {
  88.     console.log(chalk.redBright('❌❌❌ ERROR ❌❌❌'));
  89.     throw new Error(err.message || err);
  90.   }
  91. })();
Add Comment
Please, Sign In to add comment