Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@
"contributions": [
"code"
]
},
{
"login": "outslept",
"name": "outslept",
"avatar_url": "https://avatars.githubusercontent.com/u/135520429?v=4",
"profile": "https://github.com/outslept",
"contributions": [
"code"
]
}
],
"repoType": "github",
Expand Down
5 changes: 5 additions & 0 deletions .changeset/sour-starfishes-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prettier-eslint-cli": patch
---

chore: replace `get-stdin` with node api, removed unused `camelcase-keys`
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ Thanks goes to these people ([emoji key][emojis]):
<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>
<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>
</tr>
<tr>
<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>
</tr>
</tbody>
</table>

Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@
"@esm2cjs/indent-string": "^5.0.0",
"@messageformat/core": "^3.4.0",
"@prettier/eslint": "npm:prettier-eslint@^17.0.0-alpha.1",
"camelcase-keys": "^9.1.3",
"chalk-cjs": "^5.2.0",
"common-tags": "^1.8.2",
"core-js": "^3.42.0",
"eslint": "^9.26.0",
"find-up": "^5.0.0",
"get-stdin": "^8.0.0",
"glob": "^10.4.5",
"ignore": "^7.0.4",
"lodash.memoize": "^4.1.2",
Expand Down
11 changes: 8 additions & 3 deletions src/format-files.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fs from 'node:fs';
import path from 'node:path';
import { text } from 'node:stream/consumers';

import indentString from '@esm2cjs/indent-string';
import chalk from 'chalk-cjs';
import findUp from 'find-up';
import getStdin from 'get-stdin';
import { glob } from 'glob';
import nodeIgnore from 'ignore';
import memoize from 'lodash.memoize';
Expand Down Expand Up @@ -89,8 +89,13 @@ function formatFilesFromArgv({
}

async function formatStdin(prettierESLintOptions) {
const stdin = await getStdin();
const stdinValue = stdin.trim();
let stdinValue = '';

if (!process.stdin.isTTY) {
const stdin = await text(process.stdin);
stdinValue = stdin.trim();
}

try {
const formatted = await format({
text: stdinValue,
Expand Down
65 changes: 61 additions & 4 deletions src/format-files.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// eslint-disable-next-line unicorn/prefer-node-protocol -- mocked
import mockFs from 'fs';
import { text as mockText } from 'node:stream/consumers';

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

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

// Mock stream consumers
jest.mock('node:stream/consumers', () => ({
text: jest.fn(),
}));

beforeEach(() => {
process.stdout.write = jest.fn();
process.stdin.isTTY = undefined;
console.error = jest.fn();
console.log = jest.fn();
formatMock.mockClear();
mockFs.writeFile.mockClear();
mockFs.readFile.mockClear();
mockText.mockClear();
});

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

test('should accept stdin', async () => {
mockGetStdin.stdin = ' var [ foo, { bar } ] = window.APP ;';
const stdinContent = ' var [ foo, { bar } ] = window.APP ;';
mockText.mockResolvedValue(stdinContent);

await formatFiles({ stdin: true });
expect(formatMock).toHaveBeenCalledTimes(1);
// the trim is part of the test
const text = mockGetStdin.stdin.trim();
const text = stdinContent.trim();
expect(formatMock).toHaveBeenCalledWith(expect.objectContaining({ text }));
expect(process.stdout.write).toHaveBeenCalledTimes(1);
expect(process.stdout.write).toHaveBeenCalledWith('MOCK_OUTPUT for stdin');
Expand All @@ -83,7 +92,7 @@ test('will write to files if that is specified', async () => {
});

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

test('should handle TTY stdin', async () => {
const originalIsTTY = process.stdin.isTTY;
process.stdin.isTTY = true;

try {
await formatFiles({ stdin: true });

expect(mockText).not.toHaveBeenCalled();

expect(formatMock).toHaveBeenCalledTimes(1);
expect(formatMock).toHaveBeenCalledWith(
expect.objectContaining({ text: '' }),
);

expect(process.stdout.write).toHaveBeenCalledTimes(1);
expect(process.stdout.write).toHaveBeenCalledWith('MOCK_OUTPUT for stdin');
} finally {
process.stdin.isTTY = originalIsTTY;
}
});

test('should handle non-TTY stdin', async () => {
const originalIsTTY = process.stdin.isTTY;
process.stdin.isTTY = false;

const stdinContent = ' var [ foo, { bar } ] = window.APP ;';
mockText.mockResolvedValue(stdinContent);

try {
await formatFiles({ stdin: true });

expect(mockText).toHaveBeenCalledTimes(1);
expect(mockText).toHaveBeenCalledWith(process.stdin);

expect(formatMock).toHaveBeenCalledTimes(1);
expect(formatMock).toHaveBeenCalledWith(
expect.objectContaining({
text: stdinContent.trim(),
}),
);

expect(process.stdout.write).toHaveBeenCalledTimes(1);
expect(process.stdout.write).toHaveBeenCalledWith('MOCK_OUTPUT for stdin');
} finally {
process.stdin.isTTY = originalIsTTY;
}
});

describe('listDifferent', () => {
test('will list different files', async () => {
await formatFiles({
Expand Down
44 changes: 1 addition & 43 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4215,18 +4215,6 @@ __metadata:
languageName: node
linkType: hard

"camelcase-keys@npm:^9.1.3":
version: 9.1.3
resolution: "camelcase-keys@npm:9.1.3"
dependencies:
camelcase: "npm:^8.0.0"
map-obj: "npm:5.0.0"
quick-lru: "npm:^6.1.1"
type-fest: "npm:^4.3.2"
checksum: 10c0/ed8cb24f4b69f9be67b4a104250d9e025c5968a9a15e7e162b1ba3ecac5117abb106efd9484c93c497d0b0f6f0e5bc7bd4cedbb4e6f64ee115b3a49340886d60
languageName: node
linkType: hard

"camelcase@npm:^2.0.0":
version: 2.1.1
resolution: "camelcase@npm:2.1.1"
Expand All @@ -4248,13 +4236,6 @@ __metadata:
languageName: node
linkType: hard

"camelcase@npm:^8.0.0":
version: 8.0.0
resolution: "camelcase@npm:8.0.0"
checksum: 10c0/56c5fe072f0523c9908cdaac21d4a3b3fb0f608fb2e9ba90a60e792b95dd3bb3d1f3523873ab17d86d146e94171305f73ef619e2f538bd759675bc4a14b4bff3
languageName: node
linkType: hard

"caniuse-lite@npm:^1.0.30001716":
version: 1.0.30001718
resolution: "caniuse-lite@npm:1.0.30001718"
Expand Down Expand Up @@ -6712,13 +6693,6 @@ __metadata:
languageName: node
linkType: hard

"get-stdin@npm:^8.0.0":
version: 8.0.0
resolution: "get-stdin@npm:8.0.0"
checksum: 10c0/b71b72b83928221052f713b3b6247ebf1ceaeb4ef76937778557537fd51ad3f586c9e6a7476865022d9394b39b74eed1dc7514052fa74d80625276253571b76f
languageName: node
linkType: hard

"get-stream@npm:^5.1.0":
version: 5.2.0
resolution: "get-stream@npm:5.2.0"
Expand Down Expand Up @@ -8873,13 +8847,6 @@ __metadata:
languageName: node
linkType: hard

"map-obj@npm:5.0.0":
version: 5.0.0
resolution: "map-obj@npm:5.0.0"
checksum: 10c0/8ae0d8a3ce085e3e9eb46d7a4eba03681ffc644920046f7ba8edff37f11d30b0de4dd10e83f492ea6d3ba3b9c51de66d7ca6580c24b7e15d61b845d2f9f688ca
languageName: node
linkType: hard

"map-obj@npm:^1.0.0, map-obj@npm:^1.0.1":
version: 1.0.1
resolution: "map-obj@npm:1.0.1"
Expand Down Expand Up @@ -10673,7 +10640,6 @@ __metadata:
"@types/jest": "npm:^29.5.14"
"@unts/patch-package": "npm:^8.1.1"
all-contributors-cli: "npm:^6.26.1"
camelcase-keys: "npm:^9.1.3"
chalk-cjs: "npm:^5.2.0"
clean-pkg-json: "npm:^1.3.0"
common-tags: "npm:^1.8.2"
Expand All @@ -10682,7 +10648,6 @@ __metadata:
eslint-plugin-jest: "npm:^28.11.0"
eslint-plugin-node-dependencies: "npm:^1.0.1"
find-up: "npm:^5.0.0"
get-stdin: "npm:^8.0.0"
glob: "npm:^10.4.5"
ignore: "npm:^7.0.4"
jest: "npm:^29.7.0"
Expand Down Expand Up @@ -11002,13 +10967,6 @@ __metadata:
languageName: node
linkType: hard

"quick-lru@npm:^6.1.1":
version: 6.1.2
resolution: "quick-lru@npm:6.1.2"
checksum: 10c0/f499f07bd276eec460c4d7d2ee286c519f3bd189cbbb5ddf3eb929e2182e4997f66b951ea8d24b3f3cee8ed5ac9f0006bf40636f082acd1b38c050a4cbf07ed3
languageName: node
linkType: hard

"range-parser@npm:^1.2.1":
version: 1.2.1
resolution: "range-parser@npm:1.2.1"
Expand Down Expand Up @@ -12678,7 +12636,7 @@ __metadata:
languageName: node
linkType: hard

"type-fest@npm:4.39.1, type-fest@npm:^4.3.2, type-fest@npm:^4.39.1, type-fest@npm:^4.6.0":
"type-fest@npm:4.39.1, type-fest@npm:^4.39.1, type-fest@npm:^4.6.0":
version: 4.39.1
resolution: "type-fest@npm:4.39.1"
checksum: 10c0/f5bf302eb2e2f70658be1757aa578f4a09da3f65699b0b12b7ae5502ccea76e5124521a6e6b69540f442c3dc924c394202a2ab58718d0582725c7ac23c072594
Expand Down