Skip to content

Commit 5c33dea

Browse files
committed
Simply vendor get-stdin
The current state of affairs in prettierd are a bit too tricky when it comes to modules: it deeply depends on commonjs to dynamically resolve which prettier to import, and that isn't supported out of the box with esmodules. It turns out that get-stdin doesn't support cjs, and typescript is being annoying here, compiling `import` into `require`. To be honest, migrating to esmodules wouldn't be super tricky (famous last words): we'd need to implement some of the filesystem navigation to find the files and then import those with `import()`. The thing is, I'm not 100% sure that that's trivial, and I allotted some time to work on prettierd today and that time is running out, and I want to make sure I fix at least one bug without getting nerdsnipped. So, rather than trying to migrate everything to esmodules, let's keep it simple and just vendor `get-stdin`. Another option I considered was bypassing TS to still use `import()` with commonjs (some options here: TypeStrong/ts-node#1290), but that sounds too painful.
1 parent 4d4de19 commit 5c33dea

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
},
3131
"dependencies": {
3232
"core_d": "^6.1.0",
33-
"get-stdin": "^9.0.0",
3433
"prettier": "^3.2.5"
3534
},
3635
"files": [

src/get-stdin.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// This is a vendored copy of get-stdin, modified to include type annotations
2+
// and adhere to formatting style in the prettierd repo. We should get rid of
3+
// this file once we migrate prettierd to esmodules.
4+
//
5+
// get-stdin is licensed under the MIT license. The full license is available
6+
// below:
7+
//
8+
//
9+
//
10+
// MIT License
11+
//
12+
// Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
13+
//
14+
// Permission is hereby granted, free of charge, to any person obtaining a copy
15+
// of this software and associated documentation files (the "Software"), to
16+
// deal in the Software without restriction, including without limitation the
17+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18+
// sell copies of the Software, and to permit persons to whom the Software is
19+
// furnished to do so, subject to the following conditions:
20+
//
21+
// The above copyright notice and this permission notice shall be included in
22+
// all copies or substantial portions of the Software.
23+
//
24+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30+
// IN THE SOFTWARE.
31+
32+
const { stdin } = process;
33+
34+
async function getStdinBuffer(): Promise<Buffer> {
35+
if (stdin.isTTY) {
36+
return Buffer.alloc(0);
37+
}
38+
39+
const result = [];
40+
let length = 0;
41+
42+
for await (const chunk of stdin) {
43+
result.push(chunk);
44+
length += chunk.length;
45+
}
46+
47+
return Buffer.concat(result, length);
48+
}
49+
50+
export default async function getStdin(): Promise<string> {
51+
const buffer = await getStdinBuffer();
52+
return buffer.toString();
53+
}

src/prettierd.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import path from "node:path";
77
import { version } from "../package.json";
88
import { displayHelp } from "./args";
99
import { DebugInfo, getDebugInfo, stopAll } from "./service";
10-
import getStdin from "get-stdin";
10+
import getStdin from "./get-stdin";
1111

1212
const coredCommands = ["restart", "start", "status"];
1313

@@ -108,9 +108,6 @@ async function main(args: string[]): Promise<void> {
108108
return;
109109
}
110110

111-
if (cmdOrFilename === "stop") {
112-
}
113-
114111
core_d.invoke(
115112
{
116113
args,

yarn.lock

-5
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,6 @@ fill-range@^7.0.1:
144144
dependencies:
145145
to-regex-range "^5.0.1"
146146

147-
get-stdin@^9.0.0:
148-
version "9.0.0"
149-
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-9.0.0.tgz#3983ff82e03d56f1b2ea0d3e60325f39d703a575"
150-
integrity sha512-dVKBjfWisLAicarI2Sf+JuBE/DghV4UzNAVe9yhEJuzeREd3JhOTE9cUaJTeSa77fsbQUK3pcOpJfM59+VKZaA==
151-
152147
glob-parent@^5.1.2:
153148
version "5.1.2"
154149
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"

0 commit comments

Comments
 (0)