Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function finishWorksheetsAndArchive(
exports: Array,
dir: string,
output: stream$Writable,
logger: Object
) {
if (exports.length === 0) return archiveDir(dir, output, logger)
const writeStreams: Array = exports.map(
exportInfo => exportInfo.excel.stream
)
onStreamsFinished(writeStreams, err => {
if (err) {
logger.error(err)
output.emit('error', err)
} else {
archiveDir(dir, output, logger)
logger.info('All products have been written to ZIP file')
}
})
// close all excel workbooks
return exports
export function finishWorksheetsAndArchive(
exports: Array,
dir: string,
output: stream$Writable,
logger: Object
) {
if (exports.length === 0) return archiveDir(dir, output, logger)
const writeStreams: Array = exports.map(
exportInfo => exportInfo.excel.stream
)
onStreamsFinished(writeStreams, err => {
if (err) {
logger.error(err)
output.emit('error', err)
} else {
archiveDir(dir, output, logger)
logger.info('All products have been written to ZIP file')
}
})
// close all excel workbooks
return exports
.map((exportInfo: Object): Object => exportInfo.excel)
.forEach((excel: Object) => excel.finish())
}
.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')
})
}
onStreamsFinished(writeStreams, err => {
if (err) {
logger.error(err)
output.emit('error', err)
} else {
archiveDir(dir, output, logger)
logger.info('All products have been written to ZIP file')
}
})