-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (49 loc) · 1.79 KB
/
index.js
File metadata and controls
62 lines (49 loc) · 1.79 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
var fs = require("fs");
var mkdirp = require("mkdirp");
var csv = require("fast-csv");
var args = require('minimist')(process.argv.slice(2));
if (!args._[0]) {
console.log("Please pass the name of the key of the file you want to generate.");
return;
}
var key = args._[0];
// make sure we have the files we need
if (!fs.existsSync(__dirname + "/schema/" + key + ".json")) {
console.log("I couldn't find", key + ".json", "in the schema directory. (See README).")
return;
}
if (!fs.existsSync(__dirname + "/csv/" + key + ".csv")) {
console.log("I couldn't find", key + ".csv", "in the csv directory. (See README).")
return;
}
var schema = require(__dirname + "/schema/" + key + ".json");
// create the json directory if need be
mkdirp(__dirname + "/json/" + key, function(err) {
if (err) {
console.error("Error creating the folder in the json directory");
console.error(err);
return;
}
// read the csv
var data = [];
var stream = fs.createReadStream(__dirname + "/csv/" + key + ".csv");
var csvStream = csv({ headers: true })
.on("data", function(datum){
data.push(datum);
})
.on("end", function(){
if (!schema.hasOwnProperty("keys")) {
schema.keys = Object.keys(data[0]);
console.log("You should define which keys you want as lookup items in the schema. Defaulting to all of them.");
}
schema.keys.forEach(function(property) {
var json = {};
data.forEach(function(datum) {
json[datum[property]] = datum;
});
fs.writeFileSync(__dirname + "/json/" + key + "/" + property.toLowerCase().replace(/\s+/g, "_") + ".json", JSON.stringify(json, null, 2));
console.log("Wrote", __dirname + "/json/" + key + "/" + property.toLowerCase().replace(/\s+/g, "_") + ".json");
});
});
stream.pipe(csvStream);
});