-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
47 lines (42 loc) · 1.19 KB
/
utils.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { resolve } from 'path';
import { readFileSync, existsSync, writeFileSync } from 'fs';
import parse from 'csv-parse/lib/sync';
import stringify from 'csv-stringify/lib/sync';
/**
* Read a CSV input file and parse the contents
*
* @param {string} target - Path to the input file
* @returns {Record<string, any>[]} - Array of objects
*/
export function readInput(
target: string = resolve(process.cwd(), 'input.csv'),
): Record<string, any>[] {
if (!existsSync(target)) {
throw new Error('Input file does not exist!');
}
const contents = readFileSync(target).toString('utf-8');
const records: Record<string, any>[] = parse(contents, {
columns: true,
skipEmptyLines: true,
encoding: 'utf-8',
autoParse: true,
});
return records;
}
/**
* Write result to the output CSV file.
*
* @param {Record<string, any>[]} data - data to be written
* @param {string} target - output file name
*/
export function writeOutput(
data: Record<string, any>[],
target: string = resolve(process.cwd(), 'output.csv'),
): void {
const stringified = stringify(data, {
header: true,
columns: Object.keys(data[0]),
eof: true,
});
writeFileSync(target, stringified);
}