-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
262 lines (233 loc) · 9.48 KB
/
converter.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import fs from 'fs'
import csv from 'csv-parser'
import { debug } from 'console'
import ustr from 'underscore.string'
import yaml from 'yamljs'
import TurndownService from 'turndown'
//enable disable functions
//control flags that I set manually depending on the task
const exportImageList = false;
const generateMDFiles = false;
const exportMenus = true;
const names = [];
// date will be date of this invocation
const importDate = new Date().toISOString();
const sourcePath = './csv-file';
const resultPath = './resulting-md-files';
const resultListsPath = './resulting-lists';
const imageListName = 'product-images.txt';
// set the target path for images, the image URLS will be flattened with the intent of all images imported here
const imageDest = 'images/product/';
const files = fs.readdirSync(sourcePath);
// Create the resultPath in case it doesn't exist
if (!fs.existsSync(resultPath)) {
fs.mkdirSync(resultPath);
}
if (!fs.existsSync(resultListsPath)) {
fs.mkdirSync(resultListsPath);
}
else {
//delete any existing output file
try {
fs.unlinkSync(`${resultListsPath}/${imageListName}`);
console.log('Purged existing list output.');
}
catch (err) {
//nah
}
}
function exportImageNames(data) {
let images = [];
// Parsing images which are also a comma list
if (data['Images']) {
//console.log(data['Images']);
images = data['Images']
.split(',')
.map(imgurl => imgurl.substring(imgurl.indexOf('/uploads/') + 9))
.map(imgurl => ustr.clean(imgurl));
images.forEach(function(img){
console.log(img);
fs.appendFile(`${resultListsPath}/${imageListName}`, img + '\n', (err) => {
if (err) {
console.log(err);
}
else {
console.log("Image list file appended");
}
});
});
}
}
// Use the object from csv-parser to create a hugo readable md
function createMd(data) {
let out = {};
let name = ustr.slugify(data.Name);
let sku = ustr.unquote(data['SKU'], "'");
let tags = [];
let images = [];
let categories = [];
//description contains a strange newline return artifact '\r\\n\r\\n' that we can clean
let content = ustr(data['Description']).replace(/\\r\\\\n|\\n/gm,' ').clean().trim().value();
console.log(content);
// console.log(sku);
let ptype = data.Type;
console.log(`${ptype} product ${data.Name} found`);
//my items contain duplicate names and I want to slugify the product name for use as a file name
//use this array to keep count of the dupes
//if there is more than one, we will start adding -2, and -3, and so on
names.push(name);
let cnt = names.filter(nm => nm === name).length;
if(cnt > 1)
name = name.concat(`-${cnt}`);
// Parsing tags since they usually come in this format: "tag1, tag2, tag3"
// tags are for specific things about this product
if (data['Tags']) {
//console.log(data['Tags']);
//transform to an array of strings with single spaces and no surrounding quotes
tags = data['Tags']
.split(',')
.map(tag => ustr(tag).trim().unquote("'").decapitalize().value());
}
// Parsing categories which is also a comma list
// I liked having categories in WooCommerce
// Categories are for broadly grouping many products
if (data['Categories']) {
//console.log(data['Tags']);
//transform to an array of strings with single spaces and no surrounding quotes
categories = data['Categories']
.split(',')
.map(kitty => ustr(kitty).trim().clean().unquote("'").titleize().value());
}
// Parsing images which are also a comma list
if (data['Images']) {
//console.log(data['Images']);
images = data['Images']
.split(',')
.map(imgurl => imgurl.substring(imgurl.lastIndexOf('/') + 1))
.map(imgurl => ustr.clean(imgurl))
.map(imgurl => ustr(imageDest).concat(imgurl).value());
}
out.title = ustr.clean(data['Name']);
out.date = importDate;
out.enabled = false;
out.draft = true;
//clean html-like tags out of the short description when it goes into the front matter like this
out.description = ustr(data['Short description']).replace(/(<([^>]+)>)/gi,'').replace(/\\r\\\\n|\\n/gm,' ').clean().value();
out.price = ustr.numberFormat(ustr.toNumber(data['Regular price'], 2), 2, ".", ",");
out.sku = sku;
out.images = images;
out.thumbnail = ""
out.label = ""
//console.log(images);
out.tags = tags;
//console.log(tags);
out.categories = categories;
// Write file
// we want our product hugo .md file with the yaml at the top for the front matter and the content below
fs.writeFileSync(`${resultPath}/${name}.md`, `---\r\n${yaml.stringify(out, null, 2)}\r\n---\r\n${content}`);
}
// a menu item is different enough from a product that we will
// do a separate function instead of embedding a zillion boolean flags
function createMenu(data) {
const turndownService = new TurndownService();
let out = {};
let name = ustr.slugify(data.Name);
//let sku = ustr.unquote(data['SKU'], "'");
let tags = [];
let images = [];
let categories = [];
//let ingredients = [];
//description contains a strange newline return artifact '\r\\n\r\\n' that we can clean
// adding removal of html-like tags when my description has mark-up
// let content = ustr(data['Short description']).replace(/(<([^>]+)>)/gi,'').replace(/\\r\\\\n|\\n/gm,' ').trim().clean().value();
let content_html = ustr(data['Short description']).replace(/\\r\\\\n|\\n/gm,' ').trim().clean().value();
let content = turndownService.turndown(content_html);
// console.log(content);
// console.log(sku);
let ptype = data.Type;
console.log(`${ptype} product ${data.Name} found`);
//my items contain duplicate names and I want to slugify the product name for use as a file name
//use this array to keep count of the dupes
//if there is more than one, we will start adding -2, and -3, and so on
names.push(name);
let cnt = names.filter(nm => nm === name).length;
if(cnt > 1)
name = name.concat(`-${cnt}`);
// Parsing tags since they usually come in this format: "tag1, tag2, tag3"
// tags are for specific things about this product
if (data['Tags']) {
//console.log(data['Tags']);
//transform to an array of strings with single spaces and no surrounding quotes
tags = data['Tags']
.split(',')
.map(tag => ustr(tag).trim().unquote("'").decapitalize().value());
}
// Parsing categories which is also a comma list
// I liked having categories in WooCommerce
// Categories are for broadly grouping many products
if (data['Categories']) {
//console.log(data['Tags']);
//transform to an array of strings with single spaces and no surrounding quotes
categories = data['Categories']
.split(',')
.map(kitty => ustr(kitty).trim().clean().unquote("'").titleize().value());
}
// Parsing images which are also a comma list
if (data['Images']) {
//console.log(data['Images']);
images = data['Images']
.split(',')
.map(imgurl => imgurl.substring(imgurl.lastIndexOf('/') + 1))
.map(imgurl => ustr.clean(imgurl))
.map(imgurl => ustr(imageDest).concat(imgurl).value());
}
out.title = ustr.clean(data['Name']);
out.date = importDate;
out.enabled = false;
out.draft = true;
out.description = ustr.clean(data['Name']);
out.images = images;
out.thumbnail = ""
//out.ingredients = ingredients;
//console.log(images);
out.tags = tags;
//console.log(tags);
out.categories = categories;
// Write file
// we want our product hugo .md file with the yaml at the top for the front matter and the content below
fs.writeFileSync(`${resultPath}/${name}.md`, `---\r\n${yaml.stringify(out, null, 2)}\r\n---\r\n${content}`);
}
// This is to ignore the .gitkeep and allow multiple .csv
console.log(`Files:\r\n${files}`)
files.forEach(file => {
if (file.split('.').at(-1) === 'csv') {
fs.createReadStream(sourcePath + '/' + file)
.pipe(csv())
.on('data', (row) => {
//console.log(row);
if(generateMDFiles) {
//requirement is for simpe and variable types only
if (row['Type'] === 'simple' || row['Type'] === 'variable') {
createMd(row);
}
else {
console.log(`Skipped ${row['Type']}.`)
}
}
if(exportMenus) {
//menu is a variable types only
if (row['Type'] === 'variable') {
createMenu(row);
}
else {
console.log(`Skipped ${row['Type']}.`)
}
}
if(exportImageList) {
//parse out image names
exportImageNames(row);
}
})
.on('end', () => { console.log('DONE') })
}
})