From 4122f1ae6987ad958fe836b8cfa8565243c5fa51 Mon Sep 17 00:00:00 2001 From: Robin Windey Date: Tue, 1 Aug 2023 15:55:57 +0200 Subject: [PATCH] fix: Wait for CSV file stream * Stream behaviour was introduced with #617 * We have been waiting for the csvTransformStream only * FileStream was not fully flushed so we missed CSV records #618 --- src/modules/components/common_components/common.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/modules/components/common_components/common.ts b/src/modules/components/common_components/common.ts index 12d2e9ec..4f0cb979 100644 --- a/src/modules/components/common_components/common.ts +++ b/src/modules/components/common_components/common.ts @@ -868,8 +868,10 @@ export class Common { } } + const fileStream = fs.createWriteStream(filePath); const csvTransformStream = new CsvTransformStream(); - csvTransformStream.pipe(fs.createWriteStream(filePath)); + + csvTransformStream.pipe(fileStream); for(const record of array) { csvTransformStream.write(record); @@ -877,9 +879,10 @@ export class Common { csvTransformStream.end(); + // Wait for file to be fully written #618 return new Promise((resolve, reject) => { - csvTransformStream.on('finish', resolve) - csvTransformStream.on('error', reject) + fileStream.on('finish', resolve); + fileStream.on('error', reject); }) } catch (ex) { throw new CommandExecutionError(this.logger.getResourceString(RESOURCES.writingCsvFileError, filePath, ex.message));