Skip to content

Instantly share code, notes, and snippets.

@gabssnake
Last active August 27, 2025 13:14
Show Gist options
  • Save gabssnake/e4f8cce2981b39d21a0a099c3f86af10 to your computer and use it in GitHub Desktop.
Save gabssnake/e4f8cce2981b39d21a0a099c3f86af10 to your computer and use it in GitHub Desktop.
Node.js get input stream from stdin or globed file list
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