This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.js
67 lines (59 loc) · 1.53 KB
/
seed.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
const fs = require('fs');
const csv = require('fast-csv');
// Uses the first csv file it finds
var csvFile = '';
const files = fs.readdirSync('.')
files.forEach(function(file) {
if (file.endsWith('.csv')) {
csvFile = file;
return;
}
});
if (csvFile.length === 0) {
console.log('CSV file not found');
process.exit(1);
} else {
console.log('using ' + csvFile);
}
const stream = fs.createReadStream(csvFile);
const tips = [];
var columnNames = [];
var first = true;
const csvStream = csv()
.on('data', function(data){
// whatever
if (first) {
columnNames = data.map(function(d) {
return d.toLowerCase();
});
first = false;
} else {
const tip = {};
for (var i = 0; i < columnNames.length; i++) {
tip[columnNames[i]] = data[i];
}
tips.push(tip);
}
})
.on('end', function() {
tips.forEach(function(tip) {
const rawText = tip.text;
tip.title = '';
tip.subtexts= [];
const level1 = rawText.split('%');
tip.title = level1[0];
for (var i = 1; i < level1.length; i++) {
const level2 = level1[i].split('`');
tip.subtexts.push({level: 1, text: level2[0]});
for (var j = 1; j < level2.length; j++) {
tip.subtexts.push({level: 2, text: level2[j]});
}
}
tip.text = tip.text.toLowerCase();
});
const json = JSON.stringify(tips);
fs.writeFile('js/tips.json', json, 'utf8', function() {
console.log('written to js/tips.json')
})
});
stream.pipe(csvStream);