Skip to content

Commit 9fb86bc

Browse files
outsleptJounQinautofix-ci[bot]
authored
chore: replace get-stdin with node api, removed camelcase-keys (#465)
Co-authored-by: JounQin <[email protected]> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 6fc9e18 commit 9fb86bc

File tree

7 files changed

+87
-52
lines changed

7 files changed

+87
-52
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,15 @@
288288
"contributions": [
289289
"code"
290290
]
291+
},
292+
{
293+
"login": "outslept",
294+
"name": "outslept",
295+
"avatar_url": "https://avatars.githubusercontent.com/u/135520429?v=4",
296+
"profile": "https://github.com/outslept",
297+
"contributions": [
298+
"code"
299+
]
291300
}
292301
],
293302
"repoType": "github",

.changeset/sour-starfishes-rhyme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"prettier-eslint-cli": patch
3+
---
4+
5+
chore: replace `get-stdin` with node api, removed unused `camelcase-keys`

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ Thanks goes to these people ([emoji key][emojis]):
287287
<td align="center" valign="top" width="14.28%"><a href="https://github.com/dorser"><img src="https://avatars2.githubusercontent.com/u/20969462?v=4?s=100" width="100px;" alt="dorser"/><br /><sub><b>dorser</b></sub></a><br /><a href="https://github.com/prettier/prettier-eslint-cli/commits?author=dorser" title="Code">💻</a> <a href="#maintenance-dorser" title="Maintenance">🚧</a></td>
288288
<td align="center" valign="top" width="14.28%"><a href="https://qwq.cat"><img src="https://avatars2.githubusercontent.com/u/20062482?v=4?s=100" width="100px;" alt="さくら"/><br /><sub><b>さくら</b></sub></a><br /><a href="https://github.com/prettier/prettier-eslint-cli/commits?author=u3u" title="Code">💻</a></td>
289289
</tr>
290+
<tr>
291+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/outslept"><img src="https://avatars.githubusercontent.com/u/135520429?v=4?s=100" width="100px;" alt="outslept"/><br /><sub><b>outslept</b></sub></a><br /><a href="https://github.com/prettier/prettier-eslint-cli/commits?author=outslept" title="Code">💻</a></td>
292+
</tr>
290293
</tbody>
291294
</table>
292295

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,11 @@
5252
"@esm2cjs/indent-string": "^5.0.0",
5353
"@messageformat/core": "^3.4.0",
5454
"@prettier/eslint": "npm:prettier-eslint@^17.0.0-alpha.1",
55-
"camelcase-keys": "^9.1.3",
5655
"chalk-cjs": "^5.2.0",
5756
"common-tags": "^1.8.2",
5857
"core-js": "^3.42.0",
5958
"eslint": "^9.26.0",
6059
"find-up": "^5.0.0",
61-
"get-stdin": "^8.0.0",
6260
"glob": "^10.4.5",
6361
"ignore": "^7.0.4",
6462
"lodash.memoize": "^4.1.2",

src/format-files.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import { text } from 'node:stream/consumers';
34

45
import indentString from '@esm2cjs/indent-string';
56
import chalk from 'chalk-cjs';
67
import findUp from 'find-up';
7-
import getStdin from 'get-stdin';
88
import { glob } from 'glob';
99
import nodeIgnore from 'ignore';
1010
import memoize from 'lodash.memoize';
@@ -89,8 +89,13 @@ function formatFilesFromArgv({
8989
}
9090

9191
async function formatStdin(prettierESLintOptions) {
92-
const stdin = await getStdin();
93-
const stdinValue = stdin.trim();
92+
let stdinValue = '';
93+
94+
if (!process.stdin.isTTY) {
95+
const stdin = await text(process.stdin);
96+
stdinValue = stdin.trim();
97+
}
98+
9499
try {
95100
const formatted = await format({
96101
text: stdinValue,

src/format-files.spec.js

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// eslint-disable-next-line unicorn/prefer-node-protocol -- mocked
22
import mockFs from 'fs';
3+
import { text as mockText } from 'node:stream/consumers';
34

45
import findUpMock from 'find-up';
5-
import mockGetStdin from 'get-stdin';
66
import { glob as globMock } from 'glob';
77
import getLogger from 'loglevel-colored-level-prefix';
88

@@ -14,13 +14,20 @@ jest.mock('fs');
1414
// !NOTE: this is a workaround to also mock `node:fs`
1515
jest.mock('node:fs', () => mockFs);
1616

17+
// Mock stream consumers
18+
jest.mock('node:stream/consumers', () => ({
19+
text: jest.fn(),
20+
}));
21+
1722
beforeEach(() => {
1823
process.stdout.write = jest.fn();
24+
process.stdin.isTTY = undefined;
1925
console.error = jest.fn();
2026
console.log = jest.fn();
2127
formatMock.mockClear();
2228
mockFs.writeFile.mockClear();
2329
mockFs.readFile.mockClear();
30+
mockText.mockClear();
2431
});
2532

2633
afterEach(() => {
@@ -66,11 +73,13 @@ test('glob call excludes an ignore of node_modules', async () => {
6673
});
6774

6875
test('should accept stdin', async () => {
69-
mockGetStdin.stdin = ' var [ foo, { bar } ] = window.APP ;';
76+
const stdinContent = ' var [ foo, { bar } ] = window.APP ;';
77+
mockText.mockResolvedValue(stdinContent);
78+
7079
await formatFiles({ stdin: true });
7180
expect(formatMock).toHaveBeenCalledTimes(1);
7281
// the trim is part of the test
73-
const text = mockGetStdin.stdin.trim();
82+
const text = stdinContent.trim();
7483
expect(formatMock).toHaveBeenCalledWith(expect.objectContaining({ text }));
7584
expect(process.stdout.write).toHaveBeenCalledTimes(1);
7685
expect(process.stdout.write).toHaveBeenCalledWith('MOCK_OUTPUT for stdin');
@@ -83,7 +92,7 @@ test('will write to files if that is specified', async () => {
8392
});
8493

8594
test('handles stdin errors gracefully', async () => {
86-
mockGetStdin.stdin = 'MOCK_SYNTAX_ERROR';
95+
mockText.mockResolvedValue('MOCK_SYNTAX_ERROR');
8796
await formatFiles({ stdin: true });
8897
expect(console.error).toHaveBeenCalledTimes(1);
8998
});
@@ -288,6 +297,54 @@ test('will not blow up if an .eslintignore or .prettierignore cannot be found',
288297
}
289298
});
290299

300+
test('should handle TTY stdin', async () => {
301+
const originalIsTTY = process.stdin.isTTY;
302+
process.stdin.isTTY = true;
303+
304+
try {
305+
await formatFiles({ stdin: true });
306+
307+
expect(mockText).not.toHaveBeenCalled();
308+
309+
expect(formatMock).toHaveBeenCalledTimes(1);
310+
expect(formatMock).toHaveBeenCalledWith(
311+
expect.objectContaining({ text: '' }),
312+
);
313+
314+
expect(process.stdout.write).toHaveBeenCalledTimes(1);
315+
expect(process.stdout.write).toHaveBeenCalledWith('MOCK_OUTPUT for stdin');
316+
} finally {
317+
process.stdin.isTTY = originalIsTTY;
318+
}
319+
});
320+
321+
test('should handle non-TTY stdin', async () => {
322+
const originalIsTTY = process.stdin.isTTY;
323+
process.stdin.isTTY = false;
324+
325+
const stdinContent = ' var [ foo, { bar } ] = window.APP ;';
326+
mockText.mockResolvedValue(stdinContent);
327+
328+
try {
329+
await formatFiles({ stdin: true });
330+
331+
expect(mockText).toHaveBeenCalledTimes(1);
332+
expect(mockText).toHaveBeenCalledWith(process.stdin);
333+
334+
expect(formatMock).toHaveBeenCalledTimes(1);
335+
expect(formatMock).toHaveBeenCalledWith(
336+
expect.objectContaining({
337+
text: stdinContent.trim(),
338+
}),
339+
);
340+
341+
expect(process.stdout.write).toHaveBeenCalledTimes(1);
342+
expect(process.stdout.write).toHaveBeenCalledWith('MOCK_OUTPUT for stdin');
343+
} finally {
344+
process.stdin.isTTY = originalIsTTY;
345+
}
346+
});
347+
291348
describe('listDifferent', () => {
292349
test('will list different files', async () => {
293350
await formatFiles({

yarn.lock

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4215,18 +4215,6 @@ __metadata:
42154215
languageName: node
42164216
linkType: hard
42174217

4218-
"camelcase-keys@npm:^9.1.3":
4219-
version: 9.1.3
4220-
resolution: "camelcase-keys@npm:9.1.3"
4221-
dependencies:
4222-
camelcase: "npm:^8.0.0"
4223-
map-obj: "npm:5.0.0"
4224-
quick-lru: "npm:^6.1.1"
4225-
type-fest: "npm:^4.3.2"
4226-
checksum: 10c0/ed8cb24f4b69f9be67b4a104250d9e025c5968a9a15e7e162b1ba3ecac5117abb106efd9484c93c497d0b0f6f0e5bc7bd4cedbb4e6f64ee115b3a49340886d60
4227-
languageName: node
4228-
linkType: hard
4229-
42304218
"camelcase@npm:^2.0.0":
42314219
version: 2.1.1
42324220
resolution: "camelcase@npm:2.1.1"
@@ -4248,13 +4236,6 @@ __metadata:
42484236
languageName: node
42494237
linkType: hard
42504238

4251-
"camelcase@npm:^8.0.0":
4252-
version: 8.0.0
4253-
resolution: "camelcase@npm:8.0.0"
4254-
checksum: 10c0/56c5fe072f0523c9908cdaac21d4a3b3fb0f608fb2e9ba90a60e792b95dd3bb3d1f3523873ab17d86d146e94171305f73ef619e2f538bd759675bc4a14b4bff3
4255-
languageName: node
4256-
linkType: hard
4257-
42584239
"caniuse-lite@npm:^1.0.30001716":
42594240
version: 1.0.30001718
42604241
resolution: "caniuse-lite@npm:1.0.30001718"
@@ -6712,13 +6693,6 @@ __metadata:
67126693
languageName: node
67136694
linkType: hard
67146695

6715-
"get-stdin@npm:^8.0.0":
6716-
version: 8.0.0
6717-
resolution: "get-stdin@npm:8.0.0"
6718-
checksum: 10c0/b71b72b83928221052f713b3b6247ebf1ceaeb4ef76937778557537fd51ad3f586c9e6a7476865022d9394b39b74eed1dc7514052fa74d80625276253571b76f
6719-
languageName: node
6720-
linkType: hard
6721-
67226696
"get-stream@npm:^5.1.0":
67236697
version: 5.2.0
67246698
resolution: "get-stream@npm:5.2.0"
@@ -8873,13 +8847,6 @@ __metadata:
88738847
languageName: node
88748848
linkType: hard
88758849

8876-
"map-obj@npm:5.0.0":
8877-
version: 5.0.0
8878-
resolution: "map-obj@npm:5.0.0"
8879-
checksum: 10c0/8ae0d8a3ce085e3e9eb46d7a4eba03681ffc644920046f7ba8edff37f11d30b0de4dd10e83f492ea6d3ba3b9c51de66d7ca6580c24b7e15d61b845d2f9f688ca
8880-
languageName: node
8881-
linkType: hard
8882-
88838850
"map-obj@npm:^1.0.0, map-obj@npm:^1.0.1":
88848851
version: 1.0.1
88858852
resolution: "map-obj@npm:1.0.1"
@@ -10673,7 +10640,6 @@ __metadata:
1067310640
"@types/jest": "npm:^29.5.14"
1067410641
"@unts/patch-package": "npm:^8.1.1"
1067510642
all-contributors-cli: "npm:^6.26.1"
10676-
camelcase-keys: "npm:^9.1.3"
1067710643
chalk-cjs: "npm:^5.2.0"
1067810644
clean-pkg-json: "npm:^1.3.0"
1067910645
common-tags: "npm:^1.8.2"
@@ -10682,7 +10648,6 @@ __metadata:
1068210648
eslint-plugin-jest: "npm:^28.11.0"
1068310649
eslint-plugin-node-dependencies: "npm:^1.0.1"
1068410650
find-up: "npm:^5.0.0"
10685-
get-stdin: "npm:^8.0.0"
1068610651
glob: "npm:^10.4.5"
1068710652
ignore: "npm:^7.0.4"
1068810653
jest: "npm:^29.7.0"
@@ -11002,13 +10967,6 @@ __metadata:
1100210967
languageName: node
1100310968
linkType: hard
1100410969

11005-
"quick-lru@npm:^6.1.1":
11006-
version: 6.1.2
11007-
resolution: "quick-lru@npm:6.1.2"
11008-
checksum: 10c0/f499f07bd276eec460c4d7d2ee286c519f3bd189cbbb5ddf3eb929e2182e4997f66b951ea8d24b3f3cee8ed5ac9f0006bf40636f082acd1b38c050a4cbf07ed3
11009-
languageName: node
11010-
linkType: hard
11011-
1101210970
"range-parser@npm:^1.2.1":
1101310971
version: 1.2.1
1101410972
resolution: "range-parser@npm:1.2.1"
@@ -12678,7 +12636,7 @@ __metadata:
1267812636
languageName: node
1267912637
linkType: hard
1268012638

12681-
"type-fest@npm:4.39.1, type-fest@npm:^4.3.2, type-fest@npm:^4.39.1, type-fest@npm:^4.6.0":
12639+
"type-fest@npm:4.39.1, type-fest@npm:^4.39.1, type-fest@npm:^4.6.0":
1268212640
version: 4.39.1
1268312641
resolution: "type-fest@npm:4.39.1"
1268412642
checksum: 10c0/f5bf302eb2e2f70658be1757aa578f4a09da3f65699b0b12b7ae5502ccea76e5124521a6e6b69540f442c3dc924c394202a2ab58718d0582725c7ac23c072594

0 commit comments

Comments
 (0)