Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.each((product: Object): void => {
// check what productType are we exporting
if (product.productType && product.productType !== lastProductType) {
lastProductType = product.productType
lastExport = exportByProductType[lastProductType]
// if we haven't started exporting this productType yet
if (!lastExport) {
// generate a temp file name
const fileName: string = `${slugify(product.productType)}.xlsx`
const filePath: string = path.join(tmpDir, fileName)
// create headers and file stream
lastExport = {
productType: lastProductType,
headers: mapHeaders(Object.keys(product)),
excel: new ProductExcel(fs.createWriteStream(filePath)),
}
// write a header row to XLSX file
const headerNames: Array = lastExport.headers.map(
header => header.label
)
lastExport.excel.writeHeader(headerNames)
// register new export in cache
exportByProductType[lastProductType] = lastExport
}
}
// we have lastExport variable containing all necessary info
lastExport.excel.writeRow(mapValues(lastExport.headers, product))
export function writeToSingleXlsxFile(
productStream: highlandStream,
output: stream$Writable,
logger: Object,
headers: Array
) {
const mappedHeaders = mapHeaders(headers)
const headerNames = mappedHeaders.map(header => header.label)
const excel = new ProductExcel(output)
excel.writeHeader(headerNames)
productStream
.each(product => {
const row = mapValues(mappedHeaders, product)
if (row.join('')) excel.writeRow(row)
})
.done(() => {
// finish workbook and close the stream
excel.finish()
logger.info('All products have been written to XLSX file')
})
}