auron
Version:
Interact with your ATProto labeler from your terminal
30 lines (29 loc) • 1.36 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeCsvFile = exports.writeJsonFile = void 0;
const promises_1 = __importDefault(require("node:fs/promises"));
const json2csv_1 = require("json2csv");
const common_1 = require("@atproto/common");
const writeJsonFile = async (filename, content) => {
// When writing json that's too big, JSON.stringify can throw a RangeError: "Invalid string length"
// To avoid this, we can write the content in chunks of 5K items at a time
// Since we are appending to a file, let's make sure we empty the file first
await promises_1.default.writeFile(filename, "[");
let i = 0;
for (const chunk of (0, common_1.chunkArray)(content, 5000)) {
await promises_1.default.appendFile(filename, i > 0 ? ",\n" : "\n" + JSON.stringify(chunk).slice(1, -1));
i++;
}
await promises_1.default.appendFile(filename, "]");
return;
};
exports.writeJsonFile = writeJsonFile;
const writeCsvFile = async (filename, content) => {
const json2csvParser = new json2csv_1.Parser();
const csv = json2csvParser.parse(content);
return promises_1.default.writeFile(filename, csv);
};
exports.writeCsvFile = writeCsvFile;