-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseCustomers.js
78 lines (63 loc) · 2.9 KB
/
parseCustomers.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
/*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 converter = new Converter({"delimiter": delimiter});
converter.fromFile(inputFile, function (err, shopifyCustomers) {
if (err) return console.log("converter error: " + err);
var lightspeedCustomers = [],
bar = new ProgressBar('parsing :percent :bar', { total: shopifyCustomers.length });
shopifyCustomers.forEach(function (shopifyCustomer, index) {
bar.tick();
var lightspeedCustomer = {};
//unused fields
//Province Code,,Country Code,,,Accepts Marketing,Total Spent,Total Orders,Tags,Note,Tax Exempt
lightspeedCustomer.Registered = "";
lightspeedCustomer.Language = "";
lightspeedCustomer.Firstname = shopifyCustomer["First Name"];
lightspeedCustomer.LastnamePreposition = "";
lightspeedCustomer.Lastname = shopifyCustomer["Last Name"];
lightspeedCustomer["E-mail"] = shopifyCustomer.Email;
lightspeedCustomer.Phone = shopifyCustomer.Phone;
lightspeedCustomer.Mobile = "";
lightspeedCustomer.Company = shopifyCustomer.Company + " - " + shopifyCustomer.Address2;
lightspeedCustomer.Streetname = shopifyCustomer.Address1;
lightspeedCustomer.Number = "";
lightspeedCustomer.Extension = "";
lightspeedCustomer.Zipcode = shopifyCustomer.Zip;
lightspeedCustomer.City = shopifyCustomer.City;
lightspeedCustomer.Region = shopifyCustomer.Province;
lightspeedCustomer.Country = shopifyCustomer.Country;
lightspeedCustomer.Groups = "";
lightspeedCustomer.Birthdate = "";
lightspeedCustomer.Gender = "";
lightspeedCustomer.Confirmed = "";
lightspeedCustomers.push(lightspeedCustomer);
});
jsonexport(lightspeedCustomers, {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 customer 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;
};