From 091aabd8fa9878924a49e2d85b85b02a214a669c Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 4 May 2021 01:00:17 +0700 Subject: [PATCH] Require Node.js 12 and move to ESM --- .github/workflows/main.yml | 4 +-- example.js | 5 ++-- index.d.ts | 57 +++++++++++++++----------------------- index.js | 15 ++++------ index.test-d.ts | 11 ++++---- package.json | 16 ++++++----- readme.md | 8 +++--- test.js | 4 +-- 8 files changed, 53 insertions(+), 67 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1d4d275..268f715 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/example.js b/example.js index 8ff37bb..2da5d37 100644 --- a/example.js +++ b/example.js @@ -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...' })); diff --git a/index.d.ts b/index.d.ts index 6d53ba2..c16b607 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,48 +1,37 @@ -/// import {ImageOptions} from 'ansi-escapes'; -declare class UnsupportedTerminalErrorClass extends Error { +export class UnsupportedTerminalError extends Error { readonly name: 'UnsupportedTerminalError'; constructor(); } -declare namespace termImg { - interface Options 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 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}); - ``` - */ - ( - image: string | Buffer, - options?: termImg.Options - ): string | FallbackType; -}; +function fallback() { + // Do something else when not supported +} -export = termImg; +terminalImage('unicorn.jpg', {fallback}); +``` +*/ +export default function terminalImage( + image: string | Buffer, + options?: Options +): string | FallbackType; diff --git a/index.js b/index.js index 20b151d..0ddbe61 100644 --- a/index.js +++ b/index.js @@ -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'; @@ -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)) { @@ -36,6 +35,4 @@ module.exports = (image, options = {}) => { } return ansiEscapes.image(image, options); -}; - -module.exports.UnsupportedTerminalError = UnsupportedTerminalError; +} diff --git a/index.test-d.ts b/index.test-d.ts index 1468ed2..2e763af 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,12 +1,11 @@ import {expectType} from 'tsd'; -import termImg = require('.'); -import {UnsupportedTerminalError} from '.'; +import terminalImage, {UnsupportedTerminalError} from './index.js'; -expectType(termImg('/foo/bar.jpg')); -expectType(termImg(Buffer.alloc(1))); -expectType(termImg('/foo/bar.jpg', {width: 1})); +expectType(terminalImage('/foo/bar.jpg')); +expectType(terminalImage(Buffer.alloc(1))); +expectType(terminalImage('/foo/bar.jpg', {width: 1})); expectType( - termImg('/foo/bar.jpg', {fallback: () => false}) + terminalImage('/foo/bar.jpg', {fallback: () => false}) ); const unsupportedTerminalError = new UnsupportedTerminalError(); diff --git a/package.json b/package.json index cb23b45..eca690c 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" } } diff --git a/readme.md b/readme.md index 03c8875..60e2e00 100644 --- a/readme.md +++ b/readme.md @@ -19,18 +19,18 @@ $ 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. @@ -38,7 +38,7 @@ Get the image as a `string` that you can log manually. 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 diff --git a/test.js b/test.js index 9fa290e..e179a85 100644 --- a/test.js +++ b/test.js @@ -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')); });