-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.mjs
More file actions
29 lines (23 loc) · 682 Bytes
/
csv.mjs
File metadata and controls
29 lines (23 loc) · 682 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import fs from "fs";
import { format } from "fast-csv";
export async function writeCSVFile(arrayData, filePath) {
const fileExists = fs.existsSync(filePath);
let csvStream;
let writableStream;
if (fileExists) {
csvStream = format({ headers: false, objectMode: true });
writableStream = fs.createWriteStream(filePath, {
flags: "a",
includeEndRowDelimiter: true,
});
writableStream.write("\n");
} else {
csvStream = format({ headers: true, objectMode: true });
writableStream = fs.createWriteStream(filePath);
}
csvStream.pipe(writableStream);
arrayData.forEach((row) => {
csvStream.write(row);
});
csvStream.end();
}