Browse Source

Create generated file

node10
root 12 months ago
parent
commit
8718f552f1
  1. 7
      src/models/file.js

7
src/models/file.js

@ -123,7 +123,6 @@ class File {
parse(columns) { parse(columns) {
const stream = new PassThrough(); const stream = new PassThrough();
const fileStream = fs.createWriteStream(this.generatedpath); const fileStream = fs.createWriteStream(this.generatedpath);
stream.pipe(fileStream);
// check if columns is valid // check if columns is valid
if (!columns || !columns.length) { if (!columns || !columns.length) {
@ -165,25 +164,27 @@ class File {
emitter.emit('parse.start', { url: this.url, filepath: this.filepath, headers, result: result }); emitter.emit('parse.start', { url: this.url, filepath: this.filepath, headers, result: result });
stream.write(result.join(';') + "\n"); stream.write(result.join(';') + "\n");
fileStream.write(result.join(';') + "\n");
}) })
.on('data', (row) => { .on('data', (row) => {
// Emit a parse.data event with the url, filepath and data // Emit a parse.data event with the url, filepath and data
let result = this.processRow(row, columnsIndex, columnsFiltered); let result = this.processRow(row, columnsIndex, columnsFiltered);
emitter.emit('parse.data', { url: this.url, filepath: this.filepath, data: row, result, index: count }); emitter.emit('parse.data', { url: this.url, filepath: this.filepath, data: row, result, index: count });
stream.write(result.join(';') + "\n"); stream.write(result.join(';') + "\n");
fileStream.write(result.join(';') + "\n");
count++; count++;
}) })
.on('error', (err) => { .on('error', (err) => {
// Emit a parse.error event with the error // Emit a parse.error event with the error
emitter.emit('parse.error', { url: this.url, filepath: this.filepath, error: err.message }); emitter.emit('parse.error', { url: this.url, filepath: this.filepath, error: err.message });
fileStream.end(); fileStream.close();
fs.unlink(this.generatedpath, () => {}); fs.unlink(this.generatedpath, () => {});
}) })
.on('end', () => { .on('end', () => {
// Emit a parse.end event with the url and filepath // Emit a parse.end event with the url and filepath
stream.end(); stream.end();
fileStream.end(); fileStream.close();
emitter.emit('parse.end', { url: this.url, filepath: this.filepath, count: count - 1, generated: path.basename(this.generatedpath) }); emitter.emit('parse.end', { url: this.url, filepath: this.filepath, count: count - 1, generated: path.basename(this.generatedpath) });
}); });

Loading…
Cancel
Save