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 Nov 4, 2023
1 parent fe7631b commit 04981cb
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 45 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 20
- 18
os:
- ubuntu-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface TerminalSize {
export type TerminalSize = {
columns: number;
rows: number;
}
};

/**
Reliably get the terminal window size.
Expand Down
49 changes: 31 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import {execFileSync} from 'node:child_process';
import path from 'node:path';
import {fileURLToPath} from 'node:url';

const exec = (command, arguments_, shell) =>
execFileSync(command, arguments_, {encoding: 'utf8', shell, stdio: ['ignore', 'pipe', 'ignore']}).trim();
const exec = (command, arguments_, {shell, env} = {}) =>
execFileSync(command, arguments_, {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
timeout: 500,
shell,
env,
}).trim();

function execNative(command, shell) {
function execNative(command, {shell} = {}) {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
return exec(path.join(__dirname, command), [], shell).split(/\r?\n/);
return exec(path.join(__dirname, command), [], {shell}).split(/\r?\n/);
}

const create = (columns, rows) => ({
Expand All @@ -19,23 +25,23 @@ const create = (columns, rows) => ({
export default function terminalSize() {
const {env, stdout, stderr} = process;

if (stdout && stdout.columns && stdout.rows) {
if (stdout?.columns && stdout?.rows) {
return create(stdout.columns, stdout.rows);
}

if (stderr && stderr.columns && stderr.rows) {
if (stderr?.columns && stderr?.rows) {
return create(stderr.columns, stderr.rows);
}

// These values are static, so not the first choice
// These values are static, so not the first choice.
if (env.COLUMNS && env.LINES) {
return create(env.COLUMNS, env.LINES);
}

if (process.platform === 'win32') {
try {
// Binary: https://github.com/sindresorhus/win-term-size
const size = execNative('vendor/windows/term-size.exe', false);
const size = execNative('vendor/windows/term-size.exe', {shell: false});

if (size.length === 2) {
return create(size[0], size[1]);
Expand All @@ -45,7 +51,7 @@ export default function terminalSize() {
if (process.platform === 'darwin') {
try {
// Binary: https://github.com/sindresorhus/macos-term-size
const size = execNative('vendor/macos/term-size', true);
const size = execNative('vendor/macos/term-size', {shell: true});

if (size.length === 2) {
return create(size[0], size[1]);
Expand All @@ -63,17 +69,24 @@ export default function terminalSize() {
}
} catch {}

if (process.env.TERM) {
try {
const columns = exec('tput', ['cols']);
const rows = exec('tput', ['lines']);

if (columns && rows) {
return create(columns, rows);
}
} catch {}
const tputResult = tput();
if (tputResult) {
return tputResult;
}
}

return create(80, 24);
}

// On macOS, this only returns correct values when stdout is not redirected.
const tput = () => {
try {
// `tput` requires the `TERM` environment variable to be set.
const columns = exec('tput', ['cols'], {env: {TERM: 'dumb', ...process.env}});
const rows = exec('tput', ['lines'], {env: {TERM: 'dumb', ...process.env}});

if (columns && rows) {
return create(columns, rows);
}
} catch {}
};
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 terminalSize, {TerminalSize} from './index.js';
import terminalSize, {type TerminalSize} from './index.js';

const size: TerminalSize = terminalSize();
expectType<number>(size.columns);
Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -37,9 +40,9 @@
"redirected"
],
"devDependencies": {
"ava": "^3.15.0",
"execa": "^5.1.1",
"tsd": "^0.17.0",
"xo": "^0.44.0"
"ava": "^5.3.1",
"execa": "^8.0.1",
"tsd": "^0.29.0",
"xo": "^0.56.0"
}
}
16 changes: 2 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Confirmed working on macOS, Linux, and Windows.

## Install

```
$ npm install term-size
```sh
npm install term-size
```

## Usage
Expand All @@ -34,15 +34,3 @@ The bundled macOS binary is signed and hardened.
## Related

- [term-size-cli](https://github.com/sindresorhus/term-size-cli) - CLI for this module

---

<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-term-size?utm_source=npm-term-size&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import process from 'node:process';
import test from 'ava';
import execa from 'execa';
import {execa} from 'execa';
import terminalSize from './index.js';

test('main', t => {
Expand Down

0 comments on commit 04981cb

Please sign in to comment.