Created
February 14, 2022 18:54
-
-
Save pbrumblay/40540f12e3f302e4857cc2da91602a48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
import path from 'path'; | |
import csv from '@fast-csv/parse'; | |
import { Parser } from 'json2csv'; | |
const myArgs = process.argv.slice(2); | |
const directoryPath = myArgs[0]; | |
const rows = []; | |
const files = await fs.promises.readdir(directoryPath); | |
const doWork = []; | |
async function readFile(file) { | |
const fullPath = path.join(directoryPath, file); | |
return new Promise((resolve, reject) => { | |
const s = fs.createReadStream(fullPath).pipe(csv.parse({ headers: true })); | |
s.on('data', row => { | |
if(row['full_url'].startsWith('https://web-qa') || row['full_url'].startsWith('https://web-lume-main')) { | |
rows.push(row); | |
} | |
}); | |
s.on('end', resolve); | |
s.on('error', reject); | |
}); | |
} | |
for(const f of files) { | |
if(f.endsWith('_details.csv')) { | |
doWork.push(readFile(f)); | |
} | |
} | |
await Promise.all(doWork); | |
const fields = Object.keys(rows[0]); | |
const json2csvParser = new Parser({fields}); | |
const mergedcsv = json2csvParser.parse(rows); | |
console.log(mergedcsv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment