|
| 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 | +} |
0 commit comments