-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseProducts.js
155 lines (131 loc) · 6.55 KB
/
parseProducts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*jshint node:true*/
//author: github.com/jnaklaas
var Converter = require("csvtojson").Converter,
jsonexport = require('jsonexport'),
fs = require('fs'),
ProgressBar = require('progress'),
argv = require('minimist')(process.argv.slice(2));
var inputFile = argv._[0],
outputFile = (argv._[1]) ? argv._[1] : 'output.csv';
var delimiter;
switch(argv.delimiter){
case "comma": delimiter = ","; break;
case "semicolon": delimiter = ";"; break;
default: delimiter = ";";
}
var tabs = (argv.tabs) ? true : false;
var converter = new Converter({"delimiter": delimiter});
converter.fromFile(inputFile, function (err, shopifyProducts) {
if (err) return console.log("converter error: " + err);
var lightspeedProducts = [],
lastShopifyProduct,
shopifyProductVariant,
ID_incrementor = 0,
numImgs = 0,
numProductOptions = 0,
bar = new ProgressBar('parsing :percent :bar', { total: shopifyProducts.length });
shopifyProducts.forEach(function (shopifyProduct, index) {
bar.tick();
//if Product Options hidden product
if(shopifyProduct.Type === 'OPTIONS_HIDDEN_PRODUCT'){
numProductOptions++;
return;
}
//if product image
if(shopifyProduct.Title === '' && shopifyProduct["Option1 Value"] === ''){
lightspeedProducts[lightspeedProducts.length-1].Images += ", " + shopifyProduct["Image Src"];
numImgs++;
return;
}
//if product || product variant
var lightspeedProduct = {};
//if product variant
if(shopifyProduct.Title === ''){
shopifyProductVariant = shopifyProduct;
shopifyProduct = lastShopifyProduct;
}
//if product, increment ID
else{
shopifyProductVariant = shopifyProduct;
lastShopifyProduct = shopifyProduct;
ID_incrementor++;
}
lightspeedProduct.Internal_ID = ID_incrementor;
lightspeedProduct.Internal_Variant_ID = "";
lightspeedProduct.Visible = (shopifyProduct.Published)? "Y" : "N";
lightspeedProduct.Brand = "";
lightspeedProduct.Supplier = shopifyProduct.Vendor;
lightspeedProduct.EN_Title_Short = shopifyProduct.Title;
lightspeedProduct.EN_Title_Long = shopifyProduct.Title;
var result = shopifyProduct["Body (HTML)"].match(/<p(.*?)<\/p>/m);
lightspeedProduct.EN_Description_Short = (result)? result[0].replace(/<\/?[^>]+(>|$)/g, "") : "";
lightspeedProduct.EN_Description_Long = shopifyProduct["Body (HTML)"].replace(/<h6.*?>/g, "\n—").replace(/<\/h6>/g, "—\n");
lightspeedProduct.EN_Variant = "";
if(shopifyProduct["Option1 Name"] !== "Title" && shopifyProduct["Option1 Name"] !== "")
lightspeedProduct.EN_Variant += shopifyProduct["Option1 Name"] + ": " + shopifyProductVariant['Option1 Value'];
if(shopifyProduct["Option2 Name"] !== "")
lightspeedProduct.EN_Variant += ", " + shopifyProduct["Option2 Name"] + ": " + shopifyProductVariant['Option2 Value'];
if(shopifyProduct["Option3 Name"] !== "")
lightspeedProduct.EN_Variant += ", " + shopifyProduct["Option3 Name"] + ": " + shopifyProductVariant['Option3 Value'];
lightspeedProduct.Price = shopifyProductVariant["Variant Price"];
lightspeedProduct.Price_Old = "";
lightspeedProduct.Price_Cost = "";
lightspeedProduct.Price_Unit = "";
lightspeedProduct.Unit = "";
lightspeedProduct.Tax = 0.2100;
lightspeedProduct.Stock_Track = "Y";
lightspeedProduct.Stock_Disable_Sold_Out = "N";
lightspeedProduct.Stock_Level = 10;
lightspeedProduct.Stock_Min = 0;
lightspeedProduct.Stock_Alert = 5;
lightspeedProduct.Article_Code = "";
lightspeedProduct.EAN = "";
lightspeedProduct.SKU = shopifyProductVariant["Variant SKU"];
lightspeedProduct.Weight = shopifyProductVariant["Variant Grams"] + shopifyProductVariant["Variant Weight Unit"] ;
lightspeedProduct.Volume = "";
lightspeedProduct.Colli = "";
lightspeedProduct.Size_X = "";
lightspeedProduct.Size_Y = "";
lightspeedProduct.Size_Z = "";
lightspeedProduct.Matrix = "";
lightspeedProduct.Data_01 = "";
lightspeedProduct.Data_02 = "";
lightspeedProduct.Data_03 = "";
lightspeedProduct.Buy_Min = "";
lightspeedProduct.Buy_Max = "";
lightspeedProduct.NL_Google_Category = "";
lightspeedProduct.NL_Category_1 = "";
lightspeedProduct.NL_Category_2 = "";
lightspeedProduct.NL_Category_3 = "";
lightspeedProduct.NL_Meta_Title = shopifyProduct.Title;
lightspeedProduct.NL_Meta_Description = shopifyProduct["Body (HTML)"].replace(/<\/?[^>]+(>|$)/g, "").substr(0,160);
lightspeedProduct.NL_Meta_Keywords = "";
lightspeedProduct.Images = (shopifyProductVariant["Image Src"]) ? shopifyProductVariant["Image Src"] : shopifyProduct["Image Src"];
if(shopifyProductVariant["Variant Image"] && shopifyProductVariant["Variant Image"] !== shopifyProductVariant["Image Src"]){
lightspeedProduct.Images += ", " + shopifyProductVariant["Variant Image"];
}
lightspeedProduct.Tags = shopifyProduct.Tags.replace(/;/g, ",");
lightspeedProducts.push(lightspeedProduct);
});
//console.log(lightspeedProducts[0]);
console.log("parsed " + shopifyProducts.length + " lines: ");
console.log("... " + ID_incrementor + " products");
console.log("... " + (lightspeedProducts.length - ID_incrementor) + " product variants");
console.log("... " + numImgs + " images");
console.log("... " + numProductOptions + ' "Product Options" plugin lines');
jsonexport(lightspeedProducts, {rowDelimiter: ";",orderHeaders: false, 'handleString': handleString}, function (err, csv) {
if (err) return console.log("jsonexport error: " + err);
fs.writeFile(outputFile, csv, function (err) {
if (err) return console.log("fs error: " + err);
console.log("\u270B High five! Lightspeed product CSV generated!");
});
});
});
var handleString = function(string, name){
if(string.length < 2 ) return string;
//if contains quotes or delimiter, surround with quotes and double quotes
if(string.indexOf('"' > -1) || string.indexOf(delimiter > -1)){
string = '"' + string.replace(/"/g, '""') + '"';
}
return string;
};