Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 18, 2021
1 parent cf007a7 commit 151b2c1
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 99 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
84 changes: 41 additions & 43 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,21 @@
declare namespace terminalLink {
interface Options {
/**
Override the default fallback. If false, the fallback will be disabled.
@default `${text} (${url})`
*/
fallback?: ((text: string, url: string) => string) | boolean;
}
}

declare const terminalLink: {
export interface Options {
/**
Create a clickable link in the terminal's stdout.
[Supported terminals.](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda)
For unsupported terminals, the link will be printed in parens after the text: `My website (https://sindresorhus.com)`,
unless the fallback is disabled by setting the `fallback` option to `false`.
@param text - Text to linkify.
@param url - URL to link to.
Override the default fallback. If false, the fallback will be disabled.
@example
```
import terminalLink = require('terminal-link');
const link = terminalLink('My Website', 'https://sindresorhus.com');
console.log(link);
```
@default `${text} (${url})`
*/
(text: string, url: string, options?: terminalLink.Options): string;
readonly fallback?: ((text: string, url: string) => string) | boolean;
}

/**
Check whether the terminal supports links.
declare const terminalLink: {
readonly stderr: {
/**
Check whether the terminal's stderr supports links.
Prefer just using the default fallback or the `fallback` option whenever possible.
*/
readonly isSupported: boolean;
Prefer just using the default fallback or the `fallback` option whenever possible.
*/
readonly isSupported: boolean;

readonly stderr: {
/**
Create a clickable link in the terminal's stderr.
Expand All @@ -49,21 +27,41 @@ declare const terminalLink: {
@example
```
import terminalLink = require('terminal-link');
import terminalLink from 'terminal-link';
const link = terminalLink.stderr('My Website', 'https://sindresorhus.com');
console.error(link);
```
*/
(text: string, url: string, options?: terminalLink.Options): string;
(text: string, url: string, options?: Options): string;
};

/**
Check whether the terminal's stderr supports links.
/**
Check whether the terminal supports links.
Prefer just using the default fallback or the `fallback` option whenever possible.
*/
readonly isSupported: boolean;
}
Prefer just using the default fallback or the `fallback` option whenever possible.
*/
readonly isSupported: boolean;

/**
Create a clickable link in the terminal's stdout.
[Supported terminals.](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda)
For unsupported terminals, the link will be printed in parens after the text: `My website (https://sindresorhus.com)`,
unless the fallback is disabled by setting the `fallback` option to `false`.
@param text - Text to linkify.
@param url - URL to link to.
@example
```
import terminalLink from 'terminal-link';
const link = terminalLink('My Website', 'https://sindresorhus.com');
console.log(link);
```
*/
(text: string, url: string, options?: Options): string;
};

export = terminalLink;
export default terminalLink;
18 changes: 7 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
const ansiEscapes = require('ansi-escapes');
const supportsHyperlinks = require('supports-hyperlinks');
import ansiEscapes from 'ansi-escapes';
import supportsHyperlinks from 'supports-hyperlinks';

const terminalLink = (text, url, {target = 'stdout', ...options} = {}) => {
export default function terminalLink(text, url, {target = 'stdout', ...options} = {}) {
if (!supportsHyperlinks[target]) {
// If the fallback has been explicitly disabled, don't modify the text itself.
if (options.fallback === false) {
Expand All @@ -13,11 +12,8 @@ const terminalLink = (text, url, {target = 'stdout', ...options} = {}) => {
}

return ansiEscapes.link(text, url);
};
}

module.exports = (text, url, options = {}) => terminalLink(text, url, options);

module.exports.stderr = (text, url, options = {}) => terminalLink(text, url, {target: 'stderr', ...options});

module.exports.isSupported = supportsHyperlinks.stdout;
module.exports.stderr.isSupported = supportsHyperlinks.stderr;
terminalLink.isSupported = supportsHyperlinks.stdout;
terminalLink.stderr = (text, url, options = {}) => terminalLink(text, url, {target: 'stderr', ...options});
terminalLink.stderr.isSupported = supportsHyperlinks.stderr;
6 changes: 3 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import terminalLink = require('.');
import terminalLink from './index.js';

expectType<string>(terminalLink('text', 'url'));

Expand All @@ -17,7 +17,7 @@ expectType<string>(

expectType<boolean>(terminalLink.isSupported);

// stderr
// Stderr

expectType<string>(terminalLink.stderr('text', 'url'));

Expand All @@ -27,4 +27,4 @@ expectType<string>(
})
);

expectType<boolean>(terminalLink.stderr.isSupported)
expectType<boolean>(terminalLink.stderr.isSupported);
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
23 changes: 14 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
"//test": "xo && ava && tsd",
"test": "xo && tsd"
},
"files": [
"index.js",
Expand All @@ -32,13 +35,15 @@
"command-line"
],
"dependencies": {
"ansi-escapes": "^4.2.1",
"supports-hyperlinks": "^2.0.0"
"ansi-escapes": "^5.0.0",
"supports-hyperlinks": "^2.2.0"
},
"devDependencies": {
"ava": "^2.3.0",
"clear-module": "^4.0.0",
"tsd": "^0.11.0",
"xo": "^0.25.3"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
},
"ava": {
"serial": true
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $ npm install terminal-link
## Usage

```js
const terminalLink = require('terminal-link');
import terminalLink from 'terminal-link';

const link = terminalLink('My Website', 'https://sindresorhus.com');
console.log(link);
Expand Down
52 changes: 27 additions & 25 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
import {serial as test} from 'ava';
import clearModule from 'clear-module';
import test from 'ava';

test.beforeEach(() => {
clearModule.all();
});
// TODO: Tests don't currently work as we need to be able to clear `supports-color`, but our `importFresh` helper can only bypass the cache at top-level.

// eslint-disable-next-line node/no-unsupported-features/es-syntax
const importFresh = async modulePath => import(`${modulePath}?x=${new Date()}`);

const importModule = async () => (await importFresh('./index.js')).default;

test.afterEach(() => {
delete process.env.FORCE_HYPERLINK;
});

test('main', t => {
test('main', async t => {
process.env.FORCE_HYPERLINK = 1;
const terminalLink = require('.');
const terminalLink = await importModule();

const actual = terminalLink('My Website', 'https://sindresorhus.com');
console.log(actual);
t.is(actual, '\u001B]8;;https://sindresorhus.com\u0007My Website\u001B]8;;\u0007');
});

test('stderr', t => {
test('stderr', async t => {
process.env.FORCE_HYPERLINK = 1;
const terminalLink = require('.');
const terminalLink = await importModule();

const actual = terminalLink.stderr('My Website', 'https://sindresorhus.com');
console.log(actual);
t.is(actual, '\u001B]8;;https://sindresorhus.com\u0007My Website\u001B]8;;\u0007');
});

test('default fallback', t => {
test('default fallback', async t => {
process.env.FORCE_HYPERLINK = 0;
const terminalLink = require('.');
const terminalLink = await importModule();

const actual = terminalLink('My Website', 'https://sindresorhus.com');
console.log(actual);
t.is(actual, 'My Website (\u200Bhttps://sindresorhus.com\u200B)');
});

test('disabled fallback', t => {
test('disabled fallback', async t => {
process.env.FORCE_HYPERLINK = 0;
const terminalLink = require('.');
const terminalLink = await importModule();

const actual = terminalLink('My Website', 'https://sindresorhus.com', {
fallback: false
Expand All @@ -47,9 +49,9 @@ test('disabled fallback', t => {
t.is(actual, 'My Website');
});

test('explicitly enabled fallback', t => {
test('explicitly enabled fallback', async t => {
process.env.FORCE_HYPERLINK = 0;
const terminalLink = require('.');
const terminalLink = await importModule();

const actual = terminalLink('My Website', 'https://sindresorhus.com', {
fallback: true
Expand All @@ -58,18 +60,18 @@ test('explicitly enabled fallback', t => {
t.is(actual, 'My Website (\u200Bhttps://sindresorhus.com\u200B)');
});

test('stderr default fallback', t => {
test('stderr default fallback', async t => {
process.env.FORCE_HYPERLINK = 0;
const terminalLink = require('.');
const terminalLink = await importModule();

const actual = terminalLink.stderr('My Website', 'https://sindresorhus.com');
console.log(actual);
t.is(actual, 'My Website (\u200Bhttps://sindresorhus.com\u200B)');
});

test('custom fallback', t => {
test('custom fallback', async t => {
process.env.FORCE_HYPERLINK = 0;
const terminalLink = require('.');
const terminalLink = await importModule();

const actual = terminalLink('My Website', 'https://sindresorhus.com', {
fallback: (text, url) => `${text}: ${url}`
Expand All @@ -78,9 +80,9 @@ test('custom fallback', t => {
t.is(actual, 'My Website: https://sindresorhus.com');
});

test('custom fallback stderr', t => {
test('custom fallback stderr', async t => {
process.env.FORCE_HYPERLINK = 0;
const terminalLink = require('.');
const terminalLink = await importModule();

const actual = terminalLink.stderr('My Website', 'https://sindresorhus.com', {
fallback: (text, url) => `${text}: ${url}`
Expand All @@ -89,12 +91,12 @@ test('custom fallback stderr', t => {
t.is(actual, 'My Website: https://sindresorhus.com');
});

test('isSupported', t => {
const terminalLink = require('.');
test('isSupported', async t => {
const terminalLink = await importModule();
t.is(typeof terminalLink.isSupported, 'boolean');
});

test('isSupported stderr', t => {
const terminalLink = require('.');
test('isSupported stderr', async t => {
const terminalLink = await importModule();
t.is(typeof terminalLink.stderr.isSupported, 'boolean');
});

0 comments on commit 151b2c1

Please sign in to comment.