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 May 3, 2021
1 parent 42bc561 commit 091aabd
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 67 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
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
5 changes: 2 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const termImg = require('.');
import terminalImage from './index.js';

console.log(termImg('fixture.jpg', {
console.log(terminalImage('fixture.jpg', {
width: 50,
fallback: () => 'Not supported here, sorry...'
}));
57 changes: 23 additions & 34 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,37 @@
/// <reference types="node"/>
import {ImageOptions} from 'ansi-escapes';

declare class UnsupportedTerminalErrorClass extends Error {
export class UnsupportedTerminalError extends Error {
readonly name: 'UnsupportedTerminalError';

constructor();
}

declare namespace termImg {
interface Options<FallbackType = unknown> extends ImageOptions {
/**
Enables you to do something else when the terminal doesn't support images.
@default () => throw new UnsupportedTerminalError()
*/
readonly fallback?: () => FallbackType;
}
export interface Options<FallbackType = unknown> extends ImageOptions {
/**
Enables you to do something else when the terminal doesn't support images.
type UnsupportedTerminalError = UnsupportedTerminalErrorClass;
@default () => throw new UnsupportedTerminalError()
*/
readonly fallback?: () => FallbackType;
}

declare const termImg: {
UnsupportedTerminalError: typeof UnsupportedTerminalErrorClass;

/**
Get the image as a `string` that you can log manually.
@param image - Filepath to an image or an image as a buffer.
/**
Get the image as a `string` that you can log manually.
@example
```
import termImg = require('term-img');
@param image - File path to an image or an image as a buffer.
function fallback() {
// Do something else when not supported
}
@example
```
import terminalImage from 'term-img';
termImg('unicorn.jpg', {fallback});
```
*/
<FallbackType>(
image: string | Buffer,
options?: termImg.Options<FallbackType>
): string | FallbackType;
};
function fallback() {
// Do something else when not supported
}
export = termImg;
terminalImage('unicorn.jpg', {fallback});
```
*/
export default function terminalImage<FallbackType>(
image: string | Buffer,
options?: Options<FallbackType>
): string | FallbackType;
15 changes: 6 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';
const fs = require('fs');
const iterm2Version = require('iterm2-version');
const ansiEscapes = require('ansi-escapes');
import fs from 'node:fs';
import iterm2Version from 'iterm2-version';
import ansiEscapes from 'ansi-escapes';

class UnsupportedTerminalError extends Error {
export class UnsupportedTerminalError extends Error {
constructor() {
super('iTerm >=3 required');
this.name = 'UnsupportedTerminalError';
Expand All @@ -14,7 +13,7 @@ function unsupported() {
throw new UnsupportedTerminalError();
}

module.exports = (image, options = {}) => {
export default function terminalImage(image, options = {}) {
const fallback = typeof options.fallback === 'function' ? options.fallback : unsupported;

if (!(image && image.length > 0)) {
Expand All @@ -36,6 +35,4 @@ module.exports = (image, options = {}) => {
}

return ansiEscapes.image(image, options);
};

module.exports.UnsupportedTerminalError = UnsupportedTerminalError;
}
11 changes: 5 additions & 6 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {expectType} from 'tsd';
import termImg = require('.');
import {UnsupportedTerminalError} from '.';
import terminalImage, {UnsupportedTerminalError} from './index.js';

expectType<string>(termImg('/foo/bar.jpg'));
expectType<string>(termImg(Buffer.alloc(1)));
expectType<string>(termImg('/foo/bar.jpg', {width: 1}));
expectType<string>(terminalImage('/foo/bar.jpg'));
expectType<string>(terminalImage(Buffer.alloc(1)));
expectType<string>(terminalImage('/foo/bar.jpg', {width: 1}));
expectType<string | false>(
termImg('/foo/bar.jpg', {fallback: () => false})
terminalImage('/foo/bar.jpg', {fallback: () => false})
);

const unsupportedTerminalError = new UnsupportedTerminalError();
Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=10"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -43,13 +45,13 @@
"jpeg"
],
"dependencies": {
"ansi-escapes": "^4.1.0",
"iterm2-version": "^4.1.0"
"ansi-escapes": "^5.0.0",
"iterm2-version": "^5.0.0"
},
"devDependencies": {
"@types/node": "^14.0.4",
"ava": "^2.0.0",
"tsd": "^0.11.0",
"xo": "^0.30.0"
"@types/node": "^15.0.1",
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.39.1"
}
}
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ $ npm install term-img
## Usage

```js
const termImg = require('term-img');
import terminalImage from 'term-img';

function fallback() {
// Return something else when not supported
}

console.log(termImg('unicorn.jpg', {fallback}));
console.log(terminalImage('unicorn.jpg', {fallback}));
```

## API

### termImg(image, options?)
### terminalImage(image, options?)

Get the image as a `string` that you can log manually.

#### image

Type: `string | Buffer`

Filepath to an image or an image as a buffer.
File path to an image or an image as a buffer.

#### options

Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import test from 'ava';
import termImg from '.';
import terminalImage from './index.js';

test('main', t => {
process.env.TERM_PROGRAM = 'iTerm.app';
process.env.TERM_PROGRAM_VERSION = '3.3.7';
t.snapshot(termImg('fixture.jpg'));
t.snapshot(terminalImage('fixture.jpg'));
});

0 comments on commit 091aabd

Please sign in to comment.