Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Publish types (+ validate and use types + generate type maps) #21

Merged
merged 13 commits into from Mar 8, 2023
8 changes: 8 additions & 0 deletions declaration.tsconfig.json
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"declaration": true,
"noEmit": false,
"emitDeclarationOnly": true
}
}
44 changes: 30 additions & 14 deletions index.js
Expand Up @@ -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) || [];
voxpelli marked this conversation as resolved.
Show resolved Hide resolved
return {
major: 0,
minor: parseInt(m[1], 10),
Expand All @@ -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')) {
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -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;
}

Expand Down
11 changes: 7 additions & 4 deletions package.json
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
12 changes: 12 additions & 0 deletions tsconfig.json
@@ -0,0 +1,12 @@
{
"extends": "@tsconfig/node14/tsconfig.json",
"files": [
"index.js"
],
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"removeComments": true
}
}