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’ll 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
yarn.lock
.nyc_output
coverage
index.d.ts
*.d.ts*
10 changes: 10 additions & 0 deletions declaration.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

{
"extends": "./tsconfig",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"noEmit": false,
"emitDeclarationOnly": true
}
}
44 changes: 30 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
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
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
"url": "github.com/jamestalmage"
},
"engines": {
"node": ">=8"
"node": "^14.18.0 || >=16.0.0"
voxpelli marked this conversation as resolved.
Show resolved Hide resolved
},
"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 -p declaration.tsconfig.json"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the long-flag rather than -p.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

},
"files": [
"index.js",
"index.d.ts",
"index.d.ts.map",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it should publish a Source Map

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? It's just a way to ensure that "go to source" in eg. VSCode actually still goes to the source and not to the type file?

And its only this big:

{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.js"],"names":[],"mappings":"AA+BA,0CAHW;IAAE,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CAAE,GAC7B,OAAO,CAgFnB"}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed ✔️

"browser.js"
],
"browser": "browser.js",
Expand All @@ -33,10 +35,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.8.4",
"xo": "^0.24.0"
},
"nyc": {
Expand Down
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@tsconfig/node14/tsconfig.json",
"files": [
"index.js"
],
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
voxpelli marked this conversation as resolved.
Show resolved Hide resolved
voxpelli marked this conversation as resolved.
Show resolved Hide resolved
"removeComments": true
}
}