Skip to content

Commit

Permalink
Simply vendor get-stdin
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fsouza committed Apr 12, 2024
1 parent 4d4de19 commit 5c33dea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
},
"dependencies": {
"core_d": "^6.1.0",
"get-stdin": "^9.0.0",
"prettier": "^3.2.5"
},
"files": [
Expand Down
53 changes: 53 additions & 0 deletions src/get-stdin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This is a vendored copy of get-stdin, modified to include type annotations
// and adhere to formatting style in the prettierd repo. We should get rid of
// this file once we migrate prettierd to esmodules.
//
// get-stdin is licensed under the MIT license. The full license is available
// below:
//
//
//
// MIT License
//
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

const { stdin } = process;

async function getStdinBuffer(): Promise<Buffer> {
if (stdin.isTTY) {
return Buffer.alloc(0);
}

const result = [];
let length = 0;

for await (const chunk of stdin) {
result.push(chunk);
length += chunk.length;
}

return Buffer.concat(result, length);
}

export default async function getStdin(): Promise<string> {
const buffer = await getStdinBuffer();
return buffer.toString();
}
5 changes: 1 addition & 4 deletions src/prettierd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import path from "node:path";
import { version } from "../package.json";
import { displayHelp } from "./args";
import { DebugInfo, getDebugInfo, stopAll } from "./service";
import getStdin from "get-stdin";
import getStdin from "./get-stdin";

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

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

if (cmdOrFilename === "stop") {
}

core_d.invoke(
{
args,
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ fill-range@^7.0.1:
dependencies:
to-regex-range "^5.0.1"

get-stdin@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-9.0.0.tgz#3983ff82e03d56f1b2ea0d3e60325f39d703a575"
integrity sha512-dVKBjfWisLAicarI2Sf+JuBE/DghV4UzNAVe9yhEJuzeREd3JhOTE9cUaJTeSa77fsbQUK3pcOpJfM59+VKZaA==

glob-parent@^5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"
Expand Down

0 comments on commit 5c33dea

Please sign in to comment.