// @ts-check

/**
 * Set server port
 */
module.exports = function setServerPort(callback) {
  const { isProduction } = require("../config");
  const getPort = require("get-port");
  const log = require("fancy-log");
  const chalk = require("chalk");

  if (isProduction) {
    return callback();
  }

  return new Promise((resolve, reject) => {
    getPort({ port: [8080, 3000, 3002, 3004, 3006, 3008] })
      .then((port) => {
        // @ts-ignore
        global.__FREEPORT = port;
        log.info(
          chalk.yellow("Local server will use free port " + chalk.bold(port))
        );
        resolve();
      })
      .catch(reject);
  });
};
