up
This commit is contained in:
38
parse.js
Normal file
38
parse.js
Normal file
@ -0,0 +1,38 @@
|
||||
import { parse } from 'csv-parse';
|
||||
import fs from 'node:fs';
|
||||
import zlib from 'zlib';
|
||||
|
||||
if (process.argv.length != 3) {
|
||||
throw new Error('You should give a project dir');
|
||||
}
|
||||
const file = process.argv[2];
|
||||
const delimiter = (file.match(/\.gz$/)) ? ',' : '|';
|
||||
|
||||
const parser = parse({
|
||||
delimiter,
|
||||
columns: true,
|
||||
});
|
||||
|
||||
let lines = 0;
|
||||
parser.on('readable', function(){
|
||||
let record;
|
||||
while ((record = parser.read()) !== null) {
|
||||
console.log(record);
|
||||
lines++;
|
||||
}
|
||||
});
|
||||
parser.on('error', function(err){
|
||||
console.error(err.message);
|
||||
});
|
||||
parser.on('end', function(){
|
||||
console.log('end', lines);
|
||||
});
|
||||
|
||||
if (file.match(/\.gz$/)) fs.createReadStream(file).pipe(zlib.createGunzip()).pipe(parser);
|
||||
else fs.createReadStream(file).pipe(parser);
|
||||
|
||||
// affiche la progression
|
||||
const interval = setInterval(() => {
|
||||
console.log(`found ${lines} lines`);
|
||||
}, 1000);
|
||||
interval.unref();
|
Reference in New Issue
Block a user