-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(import): Add several features to productType json generator #47
Conversation
…ncoding and csv delimiter
Instead of having all three in one PR, good if each were separate PRs, especially when they are independent. |
@hisabimbola - yes I agree but in this case, I just took IO layer from another project and use it here with minor changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dropped some comments about the PR
"jszip": "2.5.x", | ||
"lodash": "^4.16.6", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we using lodash and underscore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for a notice - I will unify this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated here 9262a82
@@ -1,11 +1,15 @@ | |||
_ = require 'underscore' | |||
Promise = require 'bluebird' | |||
fs = Promise.promisifyAll require('fs') | |||
Path = require 'path' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason for capitalising Path?
Class are the only Object in javascript that is capitalise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all requires (except fs) are capitalized there so I was following convention
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated here 9262a82
fileType = getFileType(path) | ||
|
||
if not isSupportedFileType(fileType) | ||
return Promise.reject(Error("File type #{fileType} is not supported. Use one of #{supportedFileTypes}")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To use the Error
object, I think you need to instantiate it with the new
keyword
new Error()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated here 9262a82
|
||
stream.end() | ||
|
||
createReader = (fileType, delimiter = ',', encoding = 'utf-8' ) -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we factor out all this helper methods for test to a folder helper
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated here 9262a82
expect(data[0].numb).to.equal('123') | ||
|
||
|
||
it 'should not read csv file with unsupported encoding', -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no assertions in this test. How do we validate this test and what it's trying to test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can return a promise as a result of the function and mocha
will check if the promise ends with an error. This test tries to create an instance of Reader with unsupported encoding which should throw an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and if it throw an error, the test should fail right?
I think this test can be rewritten.
Handle all promises and assert that it's fails as we expect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check this update 25bdb72 if it is ok this way.
if not header | ||
header = row | ||
else | ||
rows.push Reader.mapRow(header, row) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason for not using this to access mapRow
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated here 9262a82
buffers.push buffer | ||
stream.on 'error', (err) -> reject(err) | ||
stream.on 'end', => | ||
buffer = Buffer.concat(buffers) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reason why I said I do not see the importance in streaming with this module. We saving all in memory here. Good as reading the file sync...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are loaded two files at the same time (attributes and product-type-attributes) so at least we save some time by reading them in parallel instead of synchronous version.
.then (workbook) -> | ||
header = null | ||
rows = [] | ||
worksheet = workbook.getWorksheet(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we hardcoding 1 here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was assuming that the worksheet in the xlsx file will be always the first one (as it is when exporting productTypes using product-type-export tool)
@@ -196,8 +196,22 @@ class ProductTypeGenerator | |||
i18n = {} | |||
languages = @_languages header, _.keys row | |||
for language in languages | |||
if row["#{header}.#{language}"].trim() isnt '' | |||
i18n[language] = row["#{header}.#{language}"].trim() | |||
# condition was commented out due to an error: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not get this comment?
which error?
Meanwhile, we should not commit commented out code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was one issue that when you have a productType with LENUM
attribute it exported first empty value like this:
"values": [
{
"key": "",
"label": {}
}
Which is wrong because you are missing a value in label and Sphere thus you will get an error: attributes -> type -> elementType -> values -> label: Values of LocalizedString must not be empty.
So I removed that if
and now creates first empty value with language code:
"label": {
"de": ""
}
Anyway - I removed that comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated here 9262a82
iconv.encode(row, 'win1250') | ||
] | ||
|
||
writeCsv(tempFile, dataInput) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a race condition with this test. I think we want to use mocha async method(done) to write this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mocha can work with promises too - so you don't have to use a done callback, it will try to use a promise returned from the function instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ach so.
Did not know, thanks.
rename: (dest, matchedSrcPath) -> | ||
dest + matchedSrcPath | ||
dest + matchedSrcPath.replace('coffee', 'js') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of doing this replace and removing ext
parameter totally, what if we add the ext with .js
only
ext: '.js'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hisabimbola I tried that but then it was renaming:
- abcd.spec.coffee -> abcd.js
- helper.coffee -> helper.js
Which did not start any tests because they did not have .spec.js suffix anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. Nice to hear...
|
||
read: (file) => | ||
debugLog "READER::stream file %s", file | ||
@inputStream = fs.createReadStream file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, no too much pain point... just we not utilizing the importance of the stream. That is too much for this PR.
Something we can do later, probably in a rewrite.
|
||
read: (file) => | ||
debugLog "READER::stream file %s", file | ||
@inputStream = fs.createReadStream file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this is fine for now.
iconv.encode(row, 'win1250') | ||
] | ||
|
||
writeCsv(tempFile, dataInput) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ach so.
Did not know, thanks.
expect(data[0].numb).to.equal('123') | ||
|
||
|
||
it 'should not read csv file with unsupported encoding', -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and if it throw an error, the test should fail right?
I think this test can be rewritten.
Handle all promises and assert that it's fails as we expect.
@hisabimbola - unsupported encoding test updated here 63d8cda |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM now.
This PR adds these features: