Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Utils.js
- const chalk = require('chalk');
- const detect = require('detect-port');
- const inquirer = require('inquirer');
- exports.choosePort = async (defaultPort) => {
- try {
- const port = await detect(defaultPort);
- if (port === defaultPort) {
- return defaultPort;
- }
- const message = `Port ${defaultPort} is already in use.`;
- if (process.stdout.isTTY) {
- const questionName = 'changePort';
- const question = {
- type: 'confirm',
- name: questionName,
- message: chalk.yellowBright(`
- ${message}
- Do you want to run the app on another port?
- `),
- default: true,
- };
- const result = await inquirer.prompt(question);
- return result[questionName] ? port : null;
- }
- console.log(chalk.redBright(`${message}`));
- } catch (err) {
- console.log(chalk.redBright('❌❌❌ ERROR ❌❌❌'));
- throw new Error(err.message || err);
- }
- return null;
- };
- // Start.js
- // Core
- const webpack = require('webpack');
- const DevServer = require('webpack-dev-server');
- // const hot = require('webpack-hot-middleware');
- const {choosePort} = require('./utils');
- const chalk = require('chalk'); // Раскрашивает консоль
- // Config
- const {HOST, PORT} = require('./constants');
- const getConfig = require('./webpack.config');
- const compiler = webpack(getConfig());
- (async () => {
- try {
- const choosenPort = await choosePort(PORT);
- if (!choosenPort) {
- console.log(chalk.yellowBright('It\'s impossible to run the app :('));
- return null;
- }
- const server = new DevServer(compiler, {
- host: HOST,
- port: choosenPort,
- historyApiFallback: true,
- overlay: true,
- quiet: true,
- hot: true,
- clientLogLevel: 'none',
- noInfo: true,
- // // after: (app) => {
- // // app.use(
- // // hot(compiler, {
- // // log: false,
- // // }),
- // // );
- // },
- });
- server.listen(PORT, HOST, () => {
- console.log(`${chalk.greenBright(`Server listening on http://${HOST}:${choosenPort}`)}`);
- });
- } catch (err) {
- console.log(chalk.redBright('❌❌❌ ERROR ❌❌❌'));
- throw new Error(err.message || err);
- }
- })();
Add Comment
Please, Sign In to add comment