-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
200 lines (198 loc) · 7.59 KB
/
index.js
File metadata and controls
200 lines (198 loc) · 7.59 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
const path = require('path');
const fs = require('fs');
const csvParse = require('csv-parse');
const csvStringify = require('csv-stringify');
const importPath = process.argv[2] || undefined;
const exportPath = process.argv[3] || undefined;
let importFromCSV = () => {
// async read file in from specified path, then run callback
console.log('attempting to read in data from ' + importPath + '...');
fs.readFile(path.join(__dirname + '/' + importPath), (err, data) => {
if (err) {
console.error(err);
return process.exit(-1);
}
// csv-parser module to parse the data, then run callback
console.log('attempting to parse input data...');
csvParse(data, {
columns: header => header.map(h => h.trim())
},
(err, inputData) => {
console.log('parsed input data:');
console.log(inputData);
let result = [];
let obj = {};
// for each row as 'o'
inputData.forEach(d => {
if (d['Sizes'].length > 0) {
let handle = 'prom-dress-style-' + d['Style'].trim();
console.log(d['Sizes']);
let maxSize = d['Sizes'].split('-')[1].trim();
let colors = d['Colors'].split(',');
if (!obj[handle]) {
obj[handle] = {
'title': 'Prom Dress Style ' + d['Style'].trim(),
'vendor': 'Sherri Hill',
'type': 'Prom',
'variants': []
}
}
for (let color of colors) {
obj[handle]['variants'].push({
'size': '00',
'color': color,
'price': parseInt(d['Price']) * 2,
'SKU': d['Style'].split('/')[0].trim()
});
}
for (let size = 0; size <= maxSize; size += 2) {
for (let color of colors) {
obj[handle]['variants'].push({
'size': size,
'color': color,
'price': parseInt(d['Price']) * 2,
'SKU': d['Style'].split('/')[0].trim()
});
}
}
}
});
Object.keys(obj).forEach(key => {
for (let i = 0; i < obj[key].variants.length; i++) {
if (i == 0) {
result.push({
'Handle': key,
'Title': obj[key]['title'],
'Body (HTML)': `<div class="productView">
<article class="productView-description" itemprop="description">
<div class="tabs-contents">
<div class="tab-content is-active" id="tab-description">
DESCRIPTION
</div>
</div>
</article>
</div>`,
'Vendor': obj[key]['vendor'],
'Type': obj[key]['type'],
'Tags': '',
'Published': 'TRUE',
'Option1 Name': 'Size',
'Option1 Value': obj[key]['variants'][i]['size'],
'Option2 Name': 'Color',
'Option2 Value': obj[key]['variants'][i]['color'].trim(),
'Option3 Name': '',
'Option3 Value': '',
'Variant SKU': obj[key]['variants'][i]['SKU'],
'Variant Grams': '1814',
'Variant Inventory Tracker': '',
'Variant Inventory Qty': '0',
'Variant Inventory Policy': 'continue',
'Variant Fulfillment Service': 'manual',
'Variant Price': obj[key]['variants'][i]['price'],
'Variant Compare At Price': '',
'Variant Requires Shipping': 'TRUE',
'Variant Taxable': 'TRUE',
'Variant Barcode': '',
'Image Src': '',
'Image Position': '',
'Image Alt Text': '',
'Gift Card': '',
'SEO Title': '',
'SEO Description': '',
'Google Shopping / Google Product Category': '',
'Google Shopping / Gender': '',
'Google Shopping / Age Group': '',
'Google Shopping / MPN': '',
'Google Shopping / AdWords Grouping': '',
'Google Shopping / AdWords Labels': '',
'Google Shopping / Condition': '',
'Google Shopping / Custom Product': '',
'Google Shopping / Custom Label 0': '',
'Google Shopping / Custom Label 1': '',
'Google Shopping / Custom Label 2': '',
'Google Shopping / Custom Label 3': '',
'Google Shopping / Custom Label 4': '',
'Variant Image': '',
'Variant Weight Unit': 'lb',
'Variant Tax Code': '',
'Cost per item': ''
});
} else {
result.push({
'Handle': key,
'Title': '',
'Body (HTML)':'',
'Vendor': '',
'Type': '',
'Tags': '',
'Published': '',
'Option1 Name': '',
'Option1 Value': obj[key]['variants'][i]['size'],
'Option2 Name': '',
'Option2 Value': obj[key]['variants'][i]['color'].trim(),
'Option3 Name': '',
'Option3 Value': '',
'Variant SKU': obj[key]['variants'][i]['SKU'],
'Variant Grams': '1814',
'Variant Inventory Tracker': '',
'Variant Inventory Qty': '0',
'Variant Inventory Policy': 'continue',
'Variant Fulfillment Service': 'manual',
'Variant Price': obj[key]['variants'][i]['price'],
'Variant Compare At Price': '',
'Variant Requires Shipping': 'TRUE',
'Variant Taxable': 'TRUE',
'Variant Barcode': '',
'Image Src': '',
'Image Position': '',
'Image Alt Text': '',
'Gift Card': '',
'SEO Title': '',
'SEO Description': '',
'Google Shopping / Google Product Category': '',
'Google Shopping / Gender': '',
'Google Shopping / Age Group': '',
'Google Shopping / MPN': '',
'Google Shopping / AdWords Grouping': '',
'Google Shopping / AdWords Labels': '',
'Google Shopping / Condition': '',
'Google Shopping / Custom Product': '',
'Google Shopping / Custom Label 0': '',
'Google Shopping / Custom Label 1': '',
'Google Shopping / Custom Label 2': '',
'Google Shopping / Custom Label 3': '',
'Google Shopping / Custom Label 4': '',
'Variant Image': '',
'Variant Weight Unit': 'lb',
'Variant Tax Code': '',
'Cost per item': ''
});
}
}
});
console.log('finished mapping data fields:');
console.log(result);
console.log('attempting to tranform result into CSV...');
csvStringify(result, {
header: true
}, (err, ouputData) => {
if (err) {
console.error(err);
return process.exit(-1);
}
console.log('attempting to write file to ' + exportPath + '...');
fs.writeFile(exportPath, ouputData, (err, data) => {
if (err) {
console.error(err);
return process.exit(-1);
}
console.log('done');
});
});
});
});
}
if (importPath && exportPath) {
importFromCSV();
}