utils.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import chalk from 'chalk';
  2. import detect from 'detect-port-alt';
  3. import prompts from 'prompts';
  4. import fs from 'fs-extra';
  5. import {outputPath} from '../config/paths.ts';
  6. export function clearConsole() {
  7. process.stdout.write(
  8. process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H',
  9. );
  10. }
  11. export function startedServerLogger(port: number, host?: string) {
  12. console.log();
  13. console.log(chalk.green('Server started! 🥰'));
  14. console.log();
  15. console.log(
  16. `Network: ${chalk.hex('#db5a6b').bold(`http://${host}:${port}`)}`,
  17. );
  18. console.log();
  19. }
  20. export function errorLogger(error: string) {
  21. console.error(chalk.red(error + ' 😭'));
  22. }
  23. export function junglePort(host: string, defaultPort: number) {
  24. const isInteractive = process.stdout.isTTY;
  25. return detect(defaultPort, host).then(
  26. function(port) {
  27. return new Promise(function(resolve) {
  28. if (port === defaultPort) {
  29. return resolve(port);
  30. }
  31. const message = `Something is already running on port ${defaultPort}.`;
  32. if (isInteractive) {
  33. clearConsole();
  34. const question: prompts.PromptObject = {
  35. type: 'confirm',
  36. name: 'changePort',
  37. message:
  38. chalk.yellow(message)
  39. + '\nWould you like to run the app on another port instead?',
  40. initial: true,
  41. };
  42. prompts(question).then(function({changePort}) {
  43. changePort ? resolve(port) : resolve(null);
  44. });
  45. } else {
  46. console.log(chalk.red(message));
  47. resolve(null);
  48. }
  49. });
  50. },
  51. function(err: Error) {
  52. throw new Error(
  53. chalk.red(`Could not find an open port at ${chalk.bold(host)}.`)
  54. + '\n'
  55. + ('Network error message: ' + err.message || err)
  56. + '\n',
  57. );
  58. },
  59. );
  60. }
  61. export function clearBuildFolder() {
  62. fs.emptyDirSync(outputPath);
  63. }
  64. export function printBuildError(err: Error) {
  65. const message = err != null && err.message;
  66. const stack = err != null && err.stack;
  67. if (
  68. stack
  69. && typeof message === 'string'
  70. && message.indexOf('from Terser') !== -1
  71. ) {
  72. try {
  73. const matched = /(.+)\[(.+):(.+),(.+)\]\[.+\]/.exec(stack);
  74. if (!matched) {
  75. throw new Error('Using errors for control flow is bad.');
  76. }
  77. const [, , problemPath, line, column] = matched;
  78. console.log(
  79. 'Failed to minify the code from this file: \n\n',
  80. chalk.yellow(
  81. `\t${problemPath}:${line}${column !== '0' ? ':' + column : ''}`,
  82. ),
  83. '\n',
  84. );
  85. } catch (ignored) {
  86. console.log('Failed to minify the bundle.', err);
  87. }
  88. } else {
  89. console.log((message || err) + '\n');
  90. }
  91. console.log();
  92. }