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 ca9d5f8 commit 986e516
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 86 deletions.
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
5 changes: 2 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const fs = require('fs');
const ansiEscapes = require('.');
import fs from 'fs';
import ansiEscapes from './index.js';

console.log(ansiEscapes.image(fs.readFileSync('fixture.jpg'), {width: 15}));

Expand Down
97 changes: 46 additions & 51 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,62 @@
/// <reference types="node"/>
/* eslint-disable @typescript-eslint/member-ordering */
import {LiteralUnion} from 'type-fest';

declare namespace ansiEscapes {
interface ImageOptions {
/**
The width is given as a number followed by a unit, or the word `'auto'`.
export interface ImageOptions {
/**
The width is given as a number followed by a unit, or the word `'auto'`.
- `N`: N character cells.
- `Npx`: N pixels.
- `N%`: N percent of the session's width or height.
- `auto`: The image's inherent size will be used to determine an appropriate dimension.
*/
readonly width?: LiteralUnion<'auto', number | string>;
- `N`: N character cells.
- `Npx`: N pixels.
- `N%`: N percent of the session's width or height.
- `auto`: The image's inherent size will be used to determine an appropriate dimension.
*/
readonly width?: LiteralUnion<'auto', number | string>;

/**
The height is given as a number followed by a unit, or the word `'auto'`.
/**
The height is given as a number followed by a unit, or the word `'auto'`.
- `N`: N character cells.
- `Npx`: N pixels.
- `N%`: N percent of the session's width or height.
- `auto`: The image's inherent size will be used to determine an appropriate dimension.
*/
readonly height?: LiteralUnion<'auto', number | string>;
- `N`: N character cells.
- `Npx`: N pixels.
- `N%`: N percent of the session's width or height.
- `auto`: The image's inherent size will be used to determine an appropriate dimension.
*/
readonly height?: LiteralUnion<'auto', number | string>;

readonly preserveAspectRatio?: boolean;
}
readonly preserveAspectRatio?: boolean;
}

interface AnnotationOptions {
/**
Nonzero number of columns to annotate.
export interface AnnotationOptions {
/**
Nonzero number of columns to annotate.
Default: The remainder of the line.
*/
readonly length?: number;
Default: The remainder of the line.
*/
readonly length?: number;

/**
Starting X coordinate.
/**
Starting X coordinate.
Must be used with `y` and `length`.
Must be used with `y` and `length`.
Default: The cursor position
*/
readonly x?: number;
Default: The cursor position
*/
readonly x?: number;

/**
Starting Y coordinate.
/**
Starting Y coordinate.
Must be used with `x` and `length`.
Must be used with `x` and `length`.
Default: Cursor position.
*/
readonly y?: number;
Default: Cursor position.
*/
readonly y?: number;

/**
Create a "hidden" annotation.
/**
Create a "hidden" annotation.
Annotations created this way can be shown using the "Show Annotations" iTerm command.
*/
readonly isHidden?: boolean;
}
Annotations created this way can be shown using the "Show Annotations" iTerm command.
*/
readonly isHidden?: boolean;
}

declare const ansiEscapes: {
Expand Down Expand Up @@ -218,7 +216,7 @@ declare const ansiEscapes: {
@param buffer - Buffer of an image. Usually read in with `fs.readFile()`.
*/
image(buffer: Buffer, options?: ansiEscapes.ImageOptions): string;
image(buffer: Buffer, options?: ImageOptions): string;

iTerm: {
/**
Expand All @@ -238,11 +236,8 @@ declare const ansiEscapes: {
@param message - The message to display within the annotation. The `|` character is disallowed and will be stripped.
@returns An escape code which will create an annotation when printed in iTerm2.
*/
annotation(message: string, options?: ansiEscapes.AnnotationOptions): string;
annotation(message: string, options?: AnnotationOptions): string;
};

// TODO: remove this in the next major version
default: typeof ansiEscapes;
};

export = ansiEscapes;
export default ansiEscapes;
41 changes: 20 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
'use strict';
const ansiEscapes = module.exports;
// TODO: remove this in the next major version
module.exports.default = ansiEscapes;

const ESC = '\u001B[';
const OSC = '\u001B]';
const BEL = '\u0007';
const SEP = ';';
const isTerminalApp = process.env.TERM_PROGRAM === 'Apple_Terminal';

const ansiEscapes = {};

ansiEscapes.cursorTo = (x, y) => {
if (typeof x !== 'number') {
throw new TypeError('The `x` argument is required');
Expand All @@ -26,21 +23,21 @@ ansiEscapes.cursorMove = (x, y) => {
throw new TypeError('The `x` argument is required');
}

let ret = '';
let returnValue = '';

if (x < 0) {
ret += ESC + (-x) + 'D';
returnValue += ESC + (-x) + 'D';
} else if (x > 0) {
ret += ESC + x + 'C';
returnValue += ESC + x + 'C';
}

if (y < 0) {
ret += ESC + (-y) + 'A';
returnValue += ESC + (-y) + 'A';
} else if (y > 0) {
ret += ESC + y + 'B';
returnValue += ESC + y + 'B';
}

return ret;
return returnValue;
};

ansiEscapes.cursorUp = (count = 1) => ESC + count + 'A';
Expand Down Expand Up @@ -110,28 +107,28 @@ ansiEscapes.link = (text, url) => {
};

ansiEscapes.image = (buffer, options = {}) => {
let ret = `${OSC}1337;File=inline=1`;
let returnValue = `${OSC}1337;File=inline=1`;

if (options.width) {
ret += `;width=${options.width}`;
returnValue += `;width=${options.width}`;
}

if (options.height) {
ret += `;height=${options.height}`;
returnValue += `;height=${options.height}`;
}

if (options.preserveAspectRatio === false) {
ret += ';preserveAspectRatio=0';
returnValue += ';preserveAspectRatio=0';
}

return ret + ':' + buffer.toString('base64') + BEL;
return returnValue + ':' + buffer.toString('base64') + BEL;
};

ansiEscapes.iTerm = {
setCwd: (cwd = process.cwd()) => `${OSC}50;CurrentDir=${cwd}${BEL}`,

annotation: (message, options = {}) => {
let ret = `${OSC}1337;`;
let returnValue = `${OSC}1337;`;

const hasX = typeof options.x !== 'undefined';
const hasY = typeof options.y !== 'undefined';
Expand All @@ -141,17 +138,19 @@ ansiEscapes.iTerm = {

message = message.replace(/\|/g, '');

ret += options.isHidden ? 'AddHiddenAnnotation=' : 'AddAnnotation=';
returnValue += options.isHidden ? 'AddHiddenAnnotation=' : 'AddAnnotation=';

if (options.length > 0) {
ret +=
returnValue +=
(hasX ?
[message, options.length, options.x, options.y] :
[options.length, message]).join('|');
} else {
ret += message;
returnValue += message;
}

return ret + BEL;
return returnValue + BEL;
}
};

export default ansiEscapes;
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import ansiEscapes = require('.');
import ansiEscapes from './index.js';

expectType<string>(ansiEscapes.cursorTo(0));
expectType<string>(ansiEscapes.cursorTo(0, 1));
Expand Down
12 changes: 7 additions & 5 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": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -46,12 +48,12 @@
"iterm2"
],
"dependencies": {
"type-fest": "^0.21.3"
"type-fest": "^1.0.2"
},
"devDependencies": {
"@types/node": "^13.7.7",
"ava": "^2.1.0",
"@types/node": "^14.14.41",
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.25.3"
"xo": "^0.38.2"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $ npm install ansi-escapes
## Usage

```js
const ansiEscapes = require('ansi-escapes');
import ansiEscapes from 'ansi-escapes';

// Moves the cursor two rows up and to the left
process.stdout.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft);
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import ansiEscapes from '.';
import ansiEscapes from './index.js';

test('main', t => {
t.true(Object.keys(ansiEscapes).length > 0);
Expand Down

0 comments on commit 986e516

Please sign in to comment.