json2lines
Version:
Takes a JSON file input and a JSONStream filter and produces a JSONL line-seperated file.
68 lines (61 loc) • 1.67 kB
text/typescript
import es from "event-stream";
import fs from "fs";
import { parse } from "JSONStream";
import { Readable, Writable } from "stream";
import { pipeline } from "stream/promises";
import options from "./options.js";
/**
* Processes a single Object from the upstream JSONStream, stringifying it.
* @param this
* @param data A single JSON object
* @param callback
*/
function processLine(
this: any,
data: any,
callback: (err: null, json: any) => void
) {
callback(null, JSON.stringify(data));
}
/**
* Takes a Readable that contains a valid JSON file, and outputs JSON Lines to
* the Writable.
* @param input Readable stream with JSON contents
* @param output Writable stream that will recieve JSON Lines
* @param filter The JSONStream filter to use
*/
export default async function read(
input: Readable,
output: Writable,
filter: string
) {
await pipeline(input, parse(filter), es.map(processLine), output);
}
/**
* Gets the input stream source
* @returns Either a fail stream
*/
function getInputStream() {
if (options.input === "-") {
return process.stdin;
} else {
return fs.createReadStream(options.input);
}
}
/**
* Gets the output stream source
* @returns
*/
function getOutputStream() {
if (options.output === "-") {
return process.stdout;
} else {
return fs.createWriteStream(options.output);
}
}
// Bootstrap code for CLI usage
const { input, output, filter } = options;
if (input && output && filter) {
read(getInputStream(), getOutputStream(), filter);
}