Skip to content

Commit ff4fb02

Browse files
committed
feature: @putout/cli-keypress: migrate to ESM
1 parent b465fb4 commit ff4fb02

File tree

9 files changed

+56
-65
lines changed

9 files changed

+56
-65
lines changed

packages/cli-keypress/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ npm i @putout/cli-keypress
1414
## Examples
1515

1616
```js
17-
const keyPress = require('keypress');
18-
const {isStop} = keyPress();
17+
import {keypress} from 'keypress';
18+
const {isStop} = keypress();
1919

2020
// do some stuff until ctrl+c pressed
2121
async function again() {

packages/cli-keypress/lib/keypress.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
2-
3-
const process = require('node:process');
4-
const readline = require('node:readline');
5-
const fullstore = require('fullstore');
6-
const {isCI} = require('ci-info');
1+
import process from 'node:process';
2+
import readline from 'node:readline';
3+
import fullstore from 'fullstore';
4+
import {isCI as _isCI} from 'ci-info';
75

86
const isStop = fullstore(false);
97

@@ -18,8 +16,13 @@ const isSet = (a, b) => a
1816
.filter(isKeypress)
1917
.length;
2018

21-
module.exports = (stream = process.stdin) => {
22-
if (!stream.isTTY || isCI && KEYPRESS !== '1')
19+
export const keypress = (stream = process.stdin, overrides = {}) => {
20+
const {
21+
isCI = _isCI,
22+
keypress = KEYPRESS,
23+
} = overrides;
24+
25+
if (!stream.isTTY || isCI && keypress !== '1')
2326
return {
2427
isHandlerSet: IS_NOT_SET,
2528
isStop,
@@ -44,4 +47,4 @@ const onKeyPress = (isStop) => function __keypress(str, key) {
4447
isStop(true);
4548
};
4649

47-
module.exports._onKeyPress = onKeyPress;
50+
export const _onKeyPress = onKeyPress;

packages/cli-keypress/lib/keypress.spec.js

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
'use strict';
1+
import process from 'node:process';
2+
import {Readable} from 'node:stream';
3+
import {test, stub} from 'supertape';
4+
import {keypress, _onKeyPress} from './keypress.js';
25

3-
const process = require('node:process');
4-
const {Readable} = require('node:stream');
5-
const {test, stub} = require('supertape');
6-
const mockRequire = require('mock-require');
7-
8-
const {reRequire, stopAll} = mockRequire;
96
const {assign} = Object;
107

118
test('putout: cli: on halt: no isTTY', (t) => {
129
const {isTTY} = process.stdin;
1310

1411
process.stdin.isTTY = false;
1512

16-
const onHalt = reRequire('./keypress');
17-
const {isHandlerSet} = onHalt();
13+
const {isHandlerSet} = keypress();
1814

1915
process.stdin.isTTY = isTTY;
2016

@@ -27,19 +23,14 @@ test('putout: cli: on halt: isTTY', (t) => {
2723

2824
process.env.CI = true;
2925

30-
mockRequire('ci-info', {
26+
const stream = createStream();
27+
28+
const {isHandlerSet} = keypress(stream, {
3129
isCI: false,
3230
});
3331

34-
const onHalt = reRequire('./keypress');
35-
36-
const stream = createStream();
37-
const {isHandlerSet} = onHalt(stream);
38-
3932
process.env.CI = CI;
4033

41-
stopAll();
42-
4334
t.ok(isHandlerSet(), 'should set handler');
4435
t.end();
4536
});
@@ -49,52 +40,34 @@ test('putout: cli: on halt: isTTY: double call', (t) => {
4940

5041
process.env.CI = true;
5142

52-
mockRequire('ci-info', {
43+
const stream = createStream();
44+
const {isHandlerSet} = keypress(stream, {
5345
isCI: false,
5446
});
5547

56-
const onHalt = reRequire('./keypress');
57-
58-
const stream = createStream();
59-
const {isHandlerSet} = onHalt(stream);
60-
61-
onHalt(stream);
48+
keypress(stream);
6249
process.env.CI = CI;
6350

64-
stopAll();
65-
6651
t.ok(isHandlerSet(), 'should set handler');
6752
t.end();
6853
});
6954

7055
test('putout: cli: on halt: CI, KEYPRESS', (t) => {
71-
const {KEYPRESS} = process.env;
72-
73-
process.env.KEYPRESS = '1';
74-
75-
mockRequire('ci-info', {
56+
const stream = createStream();
57+
const {isHandlerSet} = keypress(stream, {
7658
isCI: true,
59+
keypress: '1',
7760
});
7861

79-
const onHalt = reRequire('./keypress');
80-
const stream = createStream();
81-
const {isHandlerSet} = onHalt(stream);
82-
83-
onHalt(stream);
84-
process.env.KEYPRESS = KEYPRESS;
85-
86-
stopAll();
62+
keypress(stream);
8763

8864
t.ok(isHandlerSet(), 'should set handler');
8965
t.end();
9066
});
9167

9268
test('putout: cli: on halt: onKeyPress: Ctrl+C', (t) => {
93-
const {_onKeyPress} = reRequire('./keypress');
9469
const isStop = stub();
95-
9670
const fn = _onKeyPress(isStop);
97-
9871
const str = '^c';
9972

10073
const key = {
@@ -109,7 +82,6 @@ test('putout: cli: on halt: onKeyPress: Ctrl+C', (t) => {
10982
});
11083

11184
test('putout: cli: on halt: onKeyPress: not Ctrl+C', (t) => {
112-
const {_onKeyPress} = reRequire('./keypress');
11385
const isStop = stub();
11486

11587
const fn = _onKeyPress(isStop);

packages/cli-keypress/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@putout/cli-keypress",
33
"version": "2.0.0",
4-
"type": "commonjs",
4+
"type": "module",
55
"author": "coderaiser <[email protected]> (https://github.com/coderaiser)",
66
"description": "handle keypress",
77
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/cli-keypress#readme",
@@ -34,7 +34,6 @@
3434
"gitignore"
3535
],
3636
"devDependencies": {
37-
"@putout/eslint-flat": "^3.0.0",
3837
"c8": "^10.0.0",
3938
"eslint": "^9.0.0",
4039
"eslint-plugin-n": "^17.0.0",
@@ -47,7 +46,7 @@
4746
},
4847
"license": "MIT",
4948
"engines": {
50-
"node": ">=16"
49+
"node": ">=20"
5150
},
5251
"publishConfig": {
5352
"access": "public"

packages/engine-reporter/lib/subscribe.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import keyPress from '@putout/cli-keypress';
1+
import {keypress} from '@putout/cli-keypress';
22
import {createReport} from './index.js';
33

44
export const subscribe = async ({write, cwd, args, worker, exit}) => {
5-
const {isStop} = keyPress();
5+
const {isStop} = keypress();
66
const report = await createReport({
77
args,
88
cwd,

packages/putout/lib/cli/index.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515
defaultProcessors,
1616
} = require('@putout/engine-processor');
1717

18-
const keyPress = require('@putout/cli-keypress');
18+
const {keypress: _keypress} = require('@putout/cli-keypress');
1919

2020
const supportedFiles = require('./supported-files');
2121
const getOptions = require('./get-options');
@@ -56,17 +56,32 @@ const getExitCode = (wasStop) => wasStop() ? WAS_STOP : OK;
5656
const isStr = (a) => typeof a === 'string';
5757
const isNoop = (a) => String(a) === String(noop);
5858

59-
const parseIsStop = (passedIsStop) => {
59+
const parseIsStop = (passedIsStop, {keypress}) => {
6060
if (!isNoop(passedIsStop))
6161
return passedIsStop;
6262

63-
const {isStop} = keyPress();
63+
const {isStop} = keypress();
6464

6565
return isStop;
6666
};
6767

68-
module.exports = async ({argv, halt, log, write, logError, readFile, writeFile, trace = noop, isStop = noop}) => {
69-
isStop = parseIsStop(isStop);
68+
module.exports = async (overrides = {}) => {
69+
const {
70+
argv,
71+
halt,
72+
log,
73+
write,
74+
logError,
75+
readFile,
76+
writeFile,
77+
trace = noop,
78+
keypress = _keypress,
79+
} = overrides;
80+
81+
const isStop = parseIsStop(overrides.isStop || noop, {
82+
keypress,
83+
});
84+
7085
trace('start');
7186

7287
const wasStop = fullstore();

packages/putout/lib/cli/index.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,6 @@ test('putout: cli: get files: was stop', async (t) => {
19351935

19361936
mockRequire('./get-options', getOptions);
19371937
mockRequire('./get-files', getFiles);
1938-
mockRequire('@putout/cli-keypress', keypress);
19391938

19401939
reRequire('./runner/writer.js');
19411940
reRequire('./runner/runner.js');
@@ -1945,6 +1944,7 @@ test('putout: cli: get files: was stop', async (t) => {
19451944
cli,
19461945
argv,
19471946
halt,
1947+
keypress,
19481948
});
19491949

19501950
stopAll();
@@ -2407,6 +2407,7 @@ async function runCli(options) {
24072407
cli = _cli,
24082408
readFile = stub().returns(''),
24092409
writeFile = stub(),
2410+
keypress,
24102411
} = options;
24112412

24122413
await cli({
@@ -2418,5 +2419,6 @@ async function runCli(options) {
24182419
readFile,
24192420
writeFile,
24202421
isStop,
2422+
keypress,
24212423
});
24222424
}

0 commit comments

Comments
 (0)