diff --git a/declaration.tsconfig.json b/declaration.tsconfig.json new file mode 100644 index 0000000..c8d2a71 --- /dev/null +++ b/declaration.tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig", + "compilerOptions": { + "declaration": true, + "noEmit": false, + "emitDeclarationOnly": true + } +} diff --git a/index.js b/index.js index 89f1b2c..aab476a 100644 --- a/index.js +++ b/index.js @@ -2,10 +2,14 @@ const supportsColor = require('supports-color'); const hasFlag = require('has-flag'); +/** +@param {string} versionString +@returns {{ major: number, minor: number, patch: number }} +*/ function parseVersion(versionString) { if (/^\d{3,4}$/.test(versionString)) { // Env var doesn't always use dots. example: 4601 => 46.1.0 - const m = /(\d{1,2})(\d{2})/.exec(versionString); + const m = /(\d{1,2})(\d{2})/.exec(versionString) || []; return { major: 0, minor: parseInt(m[1], 10), @@ -21,11 +25,23 @@ function parseVersion(versionString) { }; } +/** +@param {{ isTTY?: boolean | undefined }} stream +@returns {boolean} +*/ function supportsHyperlink(stream) { - const {env} = process; - - if ('FORCE_HYPERLINK' in env) { - return !(env.FORCE_HYPERLINK.length > 0 && parseInt(env.FORCE_HYPERLINK, 10) === 0); + const { + CI, + FORCE_HYPERLINK, + NETLIFY, + TEAMCITY_VERSION, + TERM_PROGRAM, + TERM_PROGRAM_VERSION, + VTE_VERSION + } = process.env; + + if (FORCE_HYPERLINK) { + return !(FORCE_HYPERLINK.length > 0 && parseInt(FORCE_HYPERLINK, 10) === 0); } if (hasFlag('no-hyperlink') || hasFlag('no-hyperlinks') || hasFlag('hyperlink=false') || hasFlag('hyperlink=never')) { @@ -37,7 +53,7 @@ function supportsHyperlink(stream) { } // Netlify does not run a TTY, it does not need `supportsColor` check - if ('NETLIFY' in env) { + if (NETLIFY) { return true; } @@ -54,18 +70,18 @@ function supportsHyperlink(stream) { return false; } - if ('CI' in env) { + if (CI) { return false; } - if ('TEAMCITY_VERSION' in env) { + if (TEAMCITY_VERSION) { return false; } - if ('TERM_PROGRAM' in env) { - const version = parseVersion(env.TERM_PROGRAM_VERSION); + if (TERM_PROGRAM) { + const version = parseVersion(TERM_PROGRAM_VERSION || ''); - switch (env.TERM_PROGRAM) { + switch (TERM_PROGRAM) { case 'iTerm.app': if (version.major === 3) { return version.minor >= 1; @@ -80,13 +96,13 @@ function supportsHyperlink(stream) { } } - if ('VTE_VERSION' in env) { + if (VTE_VERSION) { // 0.50.0 was supposed to support hyperlinks, but throws a segfault - if (env.VTE_VERSION === '0.50.0') { + if (VTE_VERSION === '0.50.0') { return false; } - const version = parseVersion(env.VTE_VERSION); + const version = parseVersion(VTE_VERSION); return version.major > 0 || version.minor >= 50; } diff --git a/package.json b/package.json index 40a45ff..76786c1 100644 --- a/package.json +++ b/package.json @@ -10,15 +10,16 @@ "url": "github.com/jamestalmage" }, "engines": { - "node": ">=8" + "node": ">=14.18" }, "scripts": { "prepublishOnly": "npm run create-types", - "test": "xo && nyc ava", - "create-types": "tsc index.js --allowJs --declaration --emitDeclarationOnly" + "test": "xo && nyc ava && tsc", + "create-types": "tsc --project declaration.tsconfig.json" }, "files": [ "index.js", + "index.d.ts", "browser.js" ], "browser": "browser.js", @@ -33,10 +34,12 @@ "supports-color": "^7.0.0" }, "devDependencies": { + "@tsconfig/node14": "^1.0.3", + "@types/supports-color": "^8.1.1", "ava": "^2.2.0", "codecov": "^3.5.0", "nyc": "^14.1.1", - "typescript": "^3.7.2", + "typescript": "^4.9.5", "xo": "^0.24.0" }, "nyc": { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..1aa521a --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "@tsconfig/node14/tsconfig.json", + "files": [ + "index.js" + ], + "compilerOptions": { + "allowJs": true, + "checkJs": true, + "noEmit": true, + "removeComments": true + } +}