Last active
August 27, 2025 13:14
-
-
Save gabssnake/e4f8cce2981b39d21a0a099c3f86af10 to your computer and use it in GitHub Desktop.
Node.js get input stream from stdin or globed file list
This file contains hidden or 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
| import { readFile } from "node:fs/promises" | |
| let { debug } = console | |
| export async function* getInputStream(sources = process.argv.slice(2)) { | |
| if (sources.length === 0) { | |
| debug('stdin') | |
| let data = "" | |
| for await (let chunk of process.stdin) { | |
| data += chunk | |
| } | |
| yield data | |
| } else { | |
| debug('files', sources) | |
| for (let file of sources) { | |
| try { | |
| yield readFile(file, "utf8") | |
| } catch (e) { | |
| debug(e) | |
| } | |
| } | |
| } | |
| } | |
| export async function getInputString(separator = "\n\n") { | |
| let parts = []; | |
| for await (let chunk of getInputStream()) { | |
| parts.push(chunk); | |
| } | |
| return parts.join(separator); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment