Fix shopify extractor
This commit is contained in:
parent
2cd8937700
commit
4573b34200
|
@ -8,12 +8,12 @@ const program = require('commander'),
|
||||||
function extractDataFromSite(siteUrl, outputDir){
|
function extractDataFromSite(siteUrl, outputDir){
|
||||||
Promise.resolve(1).then(function fetchPage(pageNo) {
|
Promise.resolve(1).then(function fetchPage(pageNo) {
|
||||||
extractProductsFromPage(siteUrl, pageNo).then(products => {
|
extractProductsFromPage(siteUrl, pageNo).then(products => {
|
||||||
writeProductsAsMarkdown(products, outputDir);
|
writeProductsAsMarkdown(products, outputDir).catch(err => console.log(err));
|
||||||
if(products.length > 0) {
|
if(products.length > 0) {
|
||||||
fetchPage(pageNo + 1);
|
fetchPage(pageNo + 1);
|
||||||
}
|
}
|
||||||
});
|
}).catch(err => console.log(err));
|
||||||
});
|
}).catch(err => console.log(err));
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFileContent(product){
|
function getFileContent(product){
|
||||||
|
@ -31,21 +31,22 @@ function getFrontMatter(product) {
|
||||||
if(product.options[0].name == "Title"){
|
if(product.options[0].name == "Title"){
|
||||||
frontMatter.options = {}
|
frontMatter.options = {}
|
||||||
} else {
|
} else {
|
||||||
frontMatter.options = product.options.map(option => {
|
frontMatter.options = product.options.reduce((map, option) => {
|
||||||
let frontMatterOption = {};
|
map[option.name] = option.values;
|
||||||
frontMatterOption[option.name] = option.values;
|
return map;
|
||||||
return frontMatterOption;
|
}, {});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
frontMatter.actualPrice = null;
|
||||||
|
frontMatter.comparePrice = null;
|
||||||
|
frontMatter.inStock = null;
|
||||||
if(product.variants[0].title === "Default Title") {
|
if(product.variants[0].title === "Default Title") {
|
||||||
frontMatter.variants = [];
|
frontMatter.variants = [];
|
||||||
frontMatter.actualPrice = product.variants[0].price;
|
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;
|
frontMatter.inStock = product.variants[0].available;
|
||||||
} else {
|
} else {
|
||||||
frontMatter.actualPrice = null;
|
|
||||||
frontMatter.comparePrice = null;
|
|
||||||
frontMatter.inStock = null;
|
|
||||||
frontMatter.variants = product.variants.map(variant => {
|
frontMatter.variants = product.variants.map(variant => {
|
||||||
let frontMatterVariant = {};
|
let frontMatterVariant = {};
|
||||||
frontMatterVariant.optionCombination = [variant.option1, variant.option2, variant.option3].filter(val => val != null);
|
frontMatterVariant.optionCombination = [variant.option1, variant.option2, variant.option3].filter(val => val != null);
|
||||||
|
@ -65,15 +66,15 @@ function getContent(product){
|
||||||
function writeProductsAsMarkdown(products, outputDir){
|
function writeProductsAsMarkdown(products, outputDir){
|
||||||
return new Promise(function (resolve, reject){
|
return new Promise(function (resolve, reject){
|
||||||
products.forEach(product => {
|
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) {
|
fs.writeFile(outputFile, getFileContent(product), function(err) {
|
||||||
if(err) {
|
if(err) {
|
||||||
console.dir(err);
|
reject(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractProductsFromPage(siteUrl, pageNo){
|
function extractProductsFromPage(siteUrl, pageNo){
|
||||||
|
|
|
@ -268,14 +268,6 @@
|
||||||
"uuid": "^3.1.0"
|
"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": {
|
"safe-buffer": {
|
||||||
"version": "5.1.2",
|
"version": "5.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
|
||||||
|
@ -304,11 +296,6 @@
|
||||||
"punycode": "^1.4.1"
|
"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": {
|
"tunnel-agent": {
|
||||||
"version": "0.6.0",
|
"version": "0.6.0",
|
||||||
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",
|
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"commander": "^2.15.1",
|
"commander": "^2.15.1",
|
||||||
"request": "^2.87.0",
|
"request": "^2.87.0"
|
||||||
"rxjs": "^6.2.0"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue