Fix shopify extractor

This commit is contained in:
Kishan B 2018-05-27 15:47:55 +05:30
parent 2cd8937700
commit 4573b34200
3 changed files with 20 additions and 33 deletions

View File

@ -8,12 +8,12 @@ const program = require('commander'),
function extractDataFromSite(siteUrl, outputDir){
Promise.resolve(1).then(function fetchPage(pageNo) {
extractProductsFromPage(siteUrl, pageNo).then(products => {
writeProductsAsMarkdown(products, outputDir);
writeProductsAsMarkdown(products, outputDir).catch(err => console.log(err));
if(products.length > 0) {
fetchPage(pageNo + 1);
}
});
});
}).catch(err => console.log(err));
}).catch(err => console.log(err));
}
function getFileContent(product){
@ -31,21 +31,22 @@ function getFrontMatter(product) {
if(product.options[0].name == "Title"){
frontMatter.options = {}
} else {
frontMatter.options = product.options.map(option => {
let frontMatterOption = {};
frontMatterOption[option.name] = option.values;
return frontMatterOption;
});
frontMatter.options = product.options.reduce((map, option) => {
map[option.name] = option.values;
return map;
}, {});
}
frontMatter.actualPrice = null;
frontMatter.comparePrice = null;
frontMatter.inStock = null;
if(product.variants[0].title === "Default Title") {
frontMatter.variants = [];
frontMatter.actualPrice = product.variants[0].price;
frontMatter.comparePrice = product.variants[0].compare_at_price;
if(product.variants[0].compare_at_price && product.variants[0].compare_at_price > 0) {
frontMatter.comparePrice = product.variants[0].compare_at_price;
}
frontMatter.inStock = product.variants[0].available;
} else {
frontMatter.actualPrice = null;
frontMatter.comparePrice = null;
frontMatter.inStock = null;
frontMatter.variants = product.variants.map(variant => {
let frontMatterVariant = {};
frontMatterVariant.optionCombination = [variant.option1, variant.option2, variant.option3].filter(val => val != null);
@ -65,15 +66,15 @@ function getContent(product){
function writeProductsAsMarkdown(products, outputDir){
return new Promise(function (resolve, reject){
products.forEach(product => {
let outputFile = outputDir + "/" + product.handle + ".md";
let outputFileName = product.title.replace(/\W/g, '-').replace(/-+/g, "-").toLowerCase();
let outputFile = outputDir + "/" + outputFileName + ".md";
fs.writeFile(outputFile, getFileContent(product), function(err) {
if(err) {
console.dir(err);
}
});
if(err) {
reject(err);
}
});
});
});
}
function extractProductsFromPage(siteUrl, pageNo){

View File

@ -268,14 +268,6 @@
"uuid": "^3.1.0"
}
},
"rxjs": {
"version": "6.2.0",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.2.0.tgz",
"integrity": "sha512-qBzf5uu6eOKiCZuAE0SgZ0/Qp+l54oeVxFfC2t+mJ2SFI6IB8gmMdJHs5DUMu5kqifqcCtsKS2XHjhZu6RKvAw==",
"requires": {
"tslib": "^1.9.0"
}
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
@ -304,11 +296,6 @@
"punycode": "^1.4.1"
}
},
"tslib": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.1.tgz",
"integrity": "sha512-avfPS28HmGLLc2o4elcc2EIq2FcH++Yo5YxpBZi9Yw93BCTGFthI4HPE4Rpep6vSYQaK8e69PelM44tPj+RaQg=="
},
"tunnel-agent": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",

View File

@ -10,7 +10,6 @@
"license": "MIT",
"dependencies": {
"commander": "^2.15.1",
"request": "^2.87.0",
"rxjs": "^6.2.0"
"request": "^2.87.0"
}
}