diff --git a/lib/File-builder.js b/lib/File-builder.js index 0971a9e..3d9d4e2 100644 --- a/lib/File-builder.js +++ b/lib/File-builder.js @@ -39,13 +39,7 @@ class FileBuilder { return dirPath } - writeStreamInfo (incomingCsvLine, callback) { - this.writeStreamInfoAsync(incomingCsvLine) - .then(info => callback(null, info)) - .catch(err => callback(err)) - } - - async writeStreamInfoAsync (incomingCsvLine) { + async findStreamInfo (incomingCsvLine) { const dirPath = this.dirPathFromCsvLine(incomingCsvLine) const fileConfig = this.fileConfigFromCsvLine(incomingCsvLine) const key = dirPath + '/' + fileConfig.filename + '.csv' @@ -58,7 +52,7 @@ class FileBuilder { this.files[key] = info return info - } // writeStreamInfo + } // findStreamInfo async createWriteStream (key, dirPath, fileConfig) { if (this.knownDirs.indexOf(dirPath) === -1) { diff --git a/lib/Writer.js b/lib/Writer.js index 1b562a9..9b6d153 100644 --- a/lib/Writer.js +++ b/lib/Writer.js @@ -1,4 +1,5 @@ const Writable = require('stream').Writable +const writeToStream = require('./write-to-stream') class FileWriter extends Writable { constructor (fileBuilder, options) { @@ -15,28 +16,26 @@ class FileWriter extends Writable { } _write (incomingCsvLine, encoding, callback) { + this.doWrite(incomingCsvLine) + .then(() => callback()) + .catch(err => callback(err)) + } + + async doWrite (incomingCsvLine) { if (!(this.firstLine && this.skipFirstLine)) { this.firstLine = false - this.fileBuilder.writeStreamInfo(incomingCsvLine, function (err, info) { - if (err) { - callback(err) - } else { - info.writeStream.write(info.transformer.transform(incomingCsvLine), function (err) { - if (err) { - callback(err) - } else { - info.count += 1 - callback() - } - }) - } - }) + + const info = await this.fileBuilder.findStreamInfo(incomingCsvLine) + await writeToStream( + info.writeStream, + info.transformer.transform(incomingCsvLine) + ) + info.count += 1 } else { // Skipped this first line this.firstLine = false - callback() } - } -} + } // _write +} // FileWriter module.exports = FileWriter diff --git a/lib/write-to-stream.js b/lib/write-to-stream.js index 603b8c3..0338322 100644 --- a/lib/write-to-stream.js +++ b/lib/write-to-stream.js @@ -1,4 +1,4 @@ -async function writeToStream(stream, output) { +async function writeToStream (stream, output) { return new Promise((resolve, reject) => { stream.write( output,