Skip to content

Commit

Permalink
Merge pull request #620 from forcedotcom/hknokh-revert-617
Browse files Browse the repository at this point in the history
fix:  revert merged PR #617
  • Loading branch information
hknokh authored Jul 31, 2023
2 parents 16da2b5 + 2bfe239 commit 5a1274c
Showing 1 changed file with 14 additions and 46 deletions.
60 changes: 14 additions & 46 deletions src/modules/components/common_components/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/


import { Buffer } from 'buffer';
import { execSync } from 'child_process';
import * as parse2 from 'csv-parse/lib/sync';
Expand Down Expand Up @@ -43,14 +44,14 @@ import {
RESOURCES,
} from './logger';
import { CONSTANTS } from './statics';
import { Transform } from 'stream';

const parse = (parse2 as any).parse || parse2;

const glob = (glob2 as any).glob || glob2;

const { closest } = require('fastest-levenshtein')

const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const createCsvStringifier = require('csv-writer').createObjectCsvStringifier;


Expand Down Expand Up @@ -836,51 +837,18 @@ export class Common {
}
return;
}

class CsvTransformStream extends Transform {
_first: boolean
_stringifier: any

constructor() {
super({objectMode: true});

this._first = true;
this._stringifier = createCsvStringifier({
fieldDelimiter: Common.csvWriteFileDelimiter,
header: (columns || Object.keys(array[0])).map(x => {
return {
id: x,
title: x
}
}),
})
}

_transform(record, encoding, callback) {
//passes records one by one
const line = this._stringifier.stringifyRecords([record])
if(this._first) {
this._first = false;
callback(null, this._stringifier.getHeaderString() + line)
} else {
callback(null, line)
}
}
}

const csvTransformStream = new CsvTransformStream();
csvTransformStream.pipe(fs.createWriteStream(filePath));

for(const record of array) {
csvTransformStream.write(record);
}

csvTransformStream.end();

return new Promise((resolve, reject) => {
csvTransformStream.on('finish', resolve)
csvTransformStream.on('error', reject)
})
const csvWriter = createCsvWriter({
fieldDelimiter: Common.csvWriteFileDelimiter,
header: (columns || Object.keys(array[0])).map(x => {
return {
id: x,
title: x
}
}),
path: filePath,
encoding: "utf8"
});
return csvWriter.writeRecords(array);
} catch (ex) {
throw new CommandExecutionError(this.logger.getResourceString(RESOURCES.writingCsvFileError, filePath, ex.message));
}
Expand Down

0 comments on commit 5a1274c

Please sign in to comment.