index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. const supportsColor = require('supports-color');
  3. const hasFlag = require('has-flag');
  4. function parseVersion(versionString) {
  5. if (/^\d{3,4}$/.test(versionString)) {
  6. // Env var doesn't always use dots. example: 4601 => 46.1.0
  7. const m = /(\d{1,2})(\d{2})/.exec(versionString);
  8. return {
  9. major: 0,
  10. minor: parseInt(m[1], 10),
  11. patch: parseInt(m[2], 10)
  12. };
  13. }
  14. const versions = (versionString || '').split('.').map(n => parseInt(n, 10));
  15. return {
  16. major: versions[0],
  17. minor: versions[1],
  18. patch: versions[2]
  19. };
  20. }
  21. function supportsHyperlink(stream) {
  22. const {env} = process;
  23. if ('FORCE_HYPERLINK' in env) {
  24. return !(env.FORCE_HYPERLINK.length > 0 && parseInt(env.FORCE_HYPERLINK, 10) === 0);
  25. }
  26. if (hasFlag('no-hyperlink') || hasFlag('no-hyperlinks') || hasFlag('hyperlink=false') || hasFlag('hyperlink=never')) {
  27. return false;
  28. }
  29. if (hasFlag('hyperlink=true') || hasFlag('hyperlink=always')) {
  30. return true;
  31. }
  32. // Netlify does not run a TTY, it does not need `supportsColor` check
  33. if ('NETLIFY' in env) {
  34. return true;
  35. }
  36. // If they specify no colors, they probably don't want hyperlinks.
  37. if (!supportsColor.supportsColor(stream)) {
  38. return false;
  39. }
  40. if (stream && !stream.isTTY) {
  41. return false;
  42. }
  43. if (process.platform === 'win32') {
  44. return false;
  45. }
  46. if ('CI' in env) {
  47. return false;
  48. }
  49. if ('TEAMCITY_VERSION' in env) {
  50. return false;
  51. }
  52. if ('TERM_PROGRAM' in env) {
  53. const version = parseVersion(env.TERM_PROGRAM_VERSION);
  54. switch (env.TERM_PROGRAM) {
  55. case 'iTerm.app':
  56. if (version.major === 3) {
  57. return version.minor >= 1;
  58. }
  59. return version.major > 3;
  60. case 'WezTerm':
  61. return version.major >= 20200620;
  62. case 'vscode':
  63. return version.major > 1 || version.major === 1 && version.minor >= 72;
  64. // No default
  65. }
  66. }
  67. if ('VTE_VERSION' in env) {
  68. // 0.50.0 was supposed to support hyperlinks, but throws a segfault
  69. if (env.VTE_VERSION === '0.50.0') {
  70. return false;
  71. }
  72. const version = parseVersion(env.VTE_VERSION);
  73. return version.major > 0 || version.minor >= 50;
  74. }
  75. return false;
  76. }
  77. module.exports = {
  78. supportsHyperlink,
  79. stdout: supportsHyperlink(process.stdout),
  80. stderr: supportsHyperlink(process.stderr)
  81. };