-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
54 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,6 @@ | |
}, | ||
"dependencies": { | ||
"core_d": "^6.1.0", | ||
"get-stdin": "^9.0.0", | ||
"prettier": "^3.2.5" | ||
}, | ||
"files": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters