Skip to content

Commit

Permalink
fix(export): Fix tests, disable debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
junajan committed Oct 19, 2016
1 parent 3a72841 commit 8dde89a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 43 deletions.
29 changes: 18 additions & 11 deletions src/coffee/export.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Export
@options.outputDelimiter = @options.outputDelimiter || ","
@options.templateDelimiter = @options.templateDelimiter || ","
@options.encoding = @options.encoding || "utf8"
@options.exportFormat = @options.exportFormat || "csv"

@queryOptions =
queryString: @options.export?.queryString?.trim()
Expand Down Expand Up @@ -204,10 +205,9 @@ class Export
decodeURIComponent(queryStringParser.stringify(query))

_processChunk: (writer, products, productTypes, createFileWhenEmpty, header, exportMapper, outputFile) =>
console.warn "Fetched #{products.body.count} product(s)."
data = []
# if there are no products to export
if not products.body.count && not createFileWhenEmpty
if not products.length && not createFileWhenEmpty
return Promise.resolve()

(if @createdFiles[outputFile]
Expand All @@ -217,7 +217,7 @@ class Export
writer.setHeader header.rawHeader
)
.then =>
_.each products.body.results, (product) =>
_.each products, (product) =>
# filter variants
product.variants = @_filterVariantsByAttributes(
product.variants,
Expand All @@ -241,11 +241,7 @@ class Export
export: (templateContent, outputFile, productTypes, staged = true, customWherePredicate = false, createFileWhenEmpty = false) ->
@_parse(templateContent)
.then (header) =>
writer = new Writer
csvDelimiter: @options.outputDelimiter,
encoding: @options.encoding,
format: @options.exportFormat,
outputFile: outputFile
writer = null
errors = header.validate()
rowsReaded = 0

Expand All @@ -260,9 +256,20 @@ class Export
header._productTypeLanguageIndexes(productType)

@_getProductService(staged, customWherePredicate)
.process( (products) =>
rowsReaded += products.body.results.length
@_processChunk writer, products, productTypes, createFileWhenEmpty, header, exportMapper, outputFile
.process( (res) =>
rowsReaded += res.body.count
console.warn "Fetched #{res.body.count} product(s)."

# init writer and create output file
# when doing full export - don't create empty files
if not writer && (createFileWhenEmpty || rowsReaded)
writer = new Writer
csvDelimiter: @options.outputDelimiter,
encoding: @options.encoding,
format: @options.exportFormat,
outputFile: outputFile

@_processChunk writer, res.body.results, productTypes, createFileWhenEmpty, header, exportMapper, outputFile
, {accumulate: false})
.then ->
if createFileWhenEmpty || rowsReaded
Expand Down
55 changes: 24 additions & 31 deletions src/coffee/io/writer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ iconv = require 'iconv-lite'
fs = Promise.promisifyAll require('fs')
Excel = require 'exceljs'

DEBUG = true
DEBUG = false
debugLog = if DEBUG then console.log else _.noop

class Writer
Expand All @@ -29,29 +29,19 @@ class Writer
stream: @outputStream,
useStyles: true,
useSharedStrings: true
};
}

@workbook = new Excel.stream.xlsx.WorkbookWriter @options.workbookOpts
@worksheet = @workbook.addWorksheet('Products', {views:[{ySplit:1}]});
@worksheet = @workbook.addWorksheet('Products', {views:[{ySplit:1}]})

# encode all strings in subarrays
encode: (data) =>
encode: (string) =>
if @options.encoding == @options.defaultEncoding
return Promise.resolve(data)
return string

if not iconv.encodingExists(@options.encoding)
return Promise.reject 'Encoding does not exist: '+ @options.encoding
if not iconv.encodingExists @options.encoding
throw new Error 'Encoding does not exist: '+ @options.encoding

new Promise (resolve) =>
# iterate throught rows and cells
data = data.map (row) =>
row.map (item) =>
if _.isString(item)
iconv.encode(item, @options.encoding)
else
item

resolve(data)
iconv.encode string, @options.encoding

# create header
setHeader: (header) =>
Expand Down Expand Up @@ -80,19 +70,22 @@ class Writer
Promise.resolve()

_writeCsvRows: (data) =>
@encode data
.then (data) =>
new Promise (resolve, reject) =>
data.push([])
parsedCsv = Csv().from(data, {delimiter: @options.csvDelimiter})

# can't use .pipe - it would close stream right after first batch
parsedCsv.to.string (string) =>
@outputStream.write(string)
resolve()

parsedCsv
.on 'error', (err) -> reject err
new Promise (resolve, reject) =>
data.push([])
parsedCsv = Csv().from(data, {delimiter: @options.csvDelimiter})

# can't use .pipe - it would close stream right after first batch
parsedCsv.to.string (string) =>
try
string = @encode(string)
catch e
return reject e

@outputStream.write(string)
resolve()

parsedCsv
.on 'error', (err) -> reject err

flush: () =>
debugLog("WRITER::flushing content")
Expand Down
1 change: 0 additions & 1 deletion src/spec/integration/export.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ describe 'Export integration tests', ->
"""
productType,name,variantId
"""
@export.exportDefault(template, outputLocation, false)
.then (result) ->
Expand Down

0 comments on commit 8dde89a

Please sign in to comment.