Skip to content

Commit

Permalink
Require Node.js 18
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 2, 2024
1 parent d383ae1 commit 2c603eb
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 47 deletions.
35 changes: 15 additions & 20 deletions base.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* eslint-disable @typescript-eslint/member-ordering */
import type { Buffer } from 'node:buffer';

// From https://github.com/sindresorhus/type-fest
type Primitive =
| null // eslint-disable-line @typescript-eslint/ban-types
Expand Down Expand Up @@ -237,31 +234,29 @@ export function link(text: string, url: string): string;
/**
Display an image.
_Currently only supported on iTerm2 >=3_
See [term-img](https://github.com/sindresorhus/term-img) for a higher-level module.
@param buffer - Buffer of an image. Usually read in with `fs.readFile()`.
@param data - Image data. Usually read in with `fs.readFile()`.
*/
export function image(buffer: Buffer, options?: ImageOptions): string;
export function image(data: Uint8Array, options?: ImageOptions): string;

export namespace iTerm {
export const iTerm: {
/**
[Inform iTerm2](https://www.iterm2.com/documentation-escape-codes.html) of the current directory to help semantic history and enable [Cmd-clicking relative paths](https://coderwall.com/p/b7e82q/quickly-open-files-in-iterm-with-cmd-click).
[Inform iTerm2](https://www.iterm2.com/documentation-escape-codes.html) of the current directory to help semantic history and enable [Cmd-clicking relative paths](https://coderwall.com/p/b7e82q/quickly-open-files-in-iterm-with-cmd-click).
@param cwd - Current directory. Default: `process.cwd()`.
*/
function setCwd(cwd?: string): string;
@param cwd - Current directory. Default: `process.cwd()`.
*/
setCwd(cwd?: string): string,

/**
An annotation looks like this when shown:
An annotation looks like this when shown:
![screenshot of iTerm annotation](https://user-images.githubusercontent.com/924465/64382136-b60ac700-cfe9-11e9-8a35-9682e8dc4b72.png)
![screenshot of iTerm annotation](https://user-images.githubusercontent.com/924465/64382136-b60ac700-cfe9-11e9-8a35-9682e8dc4b72.png)
See the [iTerm Proprietary Escape Codes documentation](https://iterm2.com/documentation-escape-codes.html) for more information.
See the [iTerm Proprietary Escape Codes documentation](https://iterm2.com/documentation-escape-codes.html) for more information.
@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.
*/
function annotation(message: string, options?: AnnotationOptions): string;
}
@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?: AnnotationOptions): string
};
17 changes: 8 additions & 9 deletions base.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import process from 'node:process';
import {isBrowser} from 'environment';

const ESC = '\u001B[';
const OSC = '\u001B]';
const BEL = '\u0007';
const SEP = ';';

/* global window */
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';

const isTerminalApp = !isBrowser && process.env.TERM_PROGRAM === 'Apple_Terminal';
const isWindows = !isBrowser && process.platform === 'win32';

const cwdFunction = isBrowser ? () => {
throw new Error('`process.cwd()` only works in Node.js, not the browser.');
} : process.cwd;
Expand Down Expand Up @@ -115,7 +114,7 @@ export const link = (text, url) => [
BEL,
].join('');

export const image = (buffer, options = {}) => {
export const image = (data, options = {}) => {
let returnValue = `${OSC}1337;File=inline=1`;

if (options.width) {
Expand All @@ -130,7 +129,7 @@ export const image = (buffer, options = {}) => {
returnValue += ';preserveAspectRatio=0';
}

return returnValue + ':' + buffer.toString('base64') + BEL;
return returnValue + ':' + Buffer.from(data).toString('base64') + BEL;
};

export const iTerm = {
Expand All @@ -139,13 +138,13 @@ export const iTerm = {
annotation(message, options = {}) {
let returnValue = `${OSC}1337;`;

const hasX = typeof options.x !== 'undefined';
const hasY = typeof options.y !== 'undefined';
if ((hasX || hasY) && !(hasX && hasY && typeof options.length !== 'undefined')) {
const hasX = options.x !== undefined;
const hasY = options.y !== undefined;
if ((hasX || hasY) && !(hasX && hasY && options.length !== undefined)) {
throw new Error('`x`, `y` and `length` must be defined when `x` or `y` is defined');
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './base.d.js';
export * as default from './base.d.js';
export * from './base.js';
export * as default from './base.js';
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import * as ansiEscapes from './base.js';

export default ansiEscapes;
export * from './base.js';
export * from './base.js';
export * as default from './base.js';
27 changes: 19 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"types": "./index.d.ts",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": ">=14.16"
"node": ">=18"
},
"scripts": {
"test": "ava && tsd",
"//test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
"index.d.ts",
"base.js",
"base.d.ts"
],
"keywords": [
"ansi",
Expand All @@ -48,12 +52,19 @@
"codes",
"cursor",
"iterm",
"iterm2"
"iterm2",
"clear",
"screen",
"erase",
"scrollback"
],
"dependencies": {
"environment": "^1.0.0"
},
"devDependencies": {
"@types/node": "20.12.7",
"ava": "^4.3.3",
"@types/node": "20.12.8",
"ava": "^6.1.2",
"tsd": "0.31.0",
"xo": "^0.52.3"
"xo": "^0.58.0"
}
}
4 changes: 1 addition & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ process.stdout.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft);
Or use named exports...

```js
import {cursorUp, cursorLeft} from `ansi-escapes`;
import {cursorUp, cursorLeft} from 'ansi-escapes';

// etc, as above...
```
Expand Down Expand Up @@ -164,8 +164,6 @@ Create a clickable link.

Display an image.

*Currently only supported on iTerm2 >=3*

See [term-img](https://github.com/sindresorhus/term-img) for a higher-level module.

#### input
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, { cursorTo } from './index.js';
import ansiEscapes, {cursorTo} from './index.js';

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

0 comments on commit 2c603eb

Please # to comment.