processOptions.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. const createConfig = require('./createConfig');
  3. const defaultPort = require('./defaultPort');
  4. const findPort = require('./findPort');
  5. function processOptions(config, argv, callback) {
  6. // processOptions {Promise}
  7. if (typeof config.then === 'function') {
  8. config
  9. .then((conf) => processOptions(conf, argv, callback))
  10. .catch((err) => {
  11. // eslint-disable-next-line no-console
  12. console.error(err.stack || err);
  13. // eslint-disable-next-line no-process-exit
  14. process.exit(1);
  15. });
  16. return;
  17. }
  18. // Taken out of yargs because we must know if
  19. // it wasn't given by the user, in which case
  20. // we should use portfinder.
  21. const options = createConfig(config, argv, { port: defaultPort });
  22. if (options.socket) {
  23. callback(config, options);
  24. } else {
  25. findPort(options.port)
  26. .then((port) => {
  27. options.port = port;
  28. callback(config, options);
  29. })
  30. .catch((err) => {
  31. // eslint-disable-next-line no-console
  32. console.error(err.stack || err);
  33. // eslint-disable-next-line no-process-exit
  34. process.exit(1);
  35. });
  36. }
  37. }
  38. module.exports = processOptions;