-
Notifications
You must be signed in to change notification settings - Fork 8
/
fromJosm.js
126 lines (107 loc) · 3.34 KB
/
fromJosm.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
// Imports
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const rmdir = require('rmdir-recursive').sync;
const xml2js = require('xml2js');
const xmlmapper = require('./josm-to-h2geo-mapper');
// Constants
const outputDir = 'out/';
const baseDir = 'presets/';
/**
* Process yaml file and translate into JSON. Output in output/ directory.
* @param f the input yaml file
* @return {"name": {"en": "The Preset Name"}, "description": {"en": "Your preset description"}, "file": "preset_file.json"}
*/
var processXml = function (f) {
// Convert Yaml to JSON
var parser = new xml2js.Parser();
parser.parseString(fs.readFileSync(baseDir + f), function (err, result) {
if (err) {
console.error("Error while parsing: " + err);
throw err;
}
var xmlConverted = result;
if (!xmlConverted.presets.$.shortdescription || !xmlConverted.presets.$.description) {
console.error("Error in structure");
console.log(result);
throw "Invalid Yaml Structure for file " + f;
}
var parsed = xmlmapper(xmlConverted);
var filename = path.basename(f, path.extname(f)) + '.json';
fs.writeFileSync(outputDir + filename, JSON.stringify(parsed));
console.log(parsed);
return {"name": parsed.name, "description": parsed.description, "file": filename, "version": parsed.version};
});
};
/**
* Process json file. Output in output/ directory.
* @param f the input json file
* @return {"name": {"en": "The Preset Name"}, "description": {"en": "Your preset description"}, "file": "preset_file.json"}
*/
var processJson = function (f) {
var parsed = JSON.parse(fs.readFileSync(baseDir + f));
if (!parsed.name) {
throw "Invalid JSON Structure for file " + f;
}
var filename = path.basename(f);
var filenameYml = path.basename(f, path.extname(f)) + '.yml';
// Write to output directory
fs.writeFileSync(outputDir + filename, JSON.stringify(parsed));
// Convert JSON to Yaml
fs.writeFileSync(baseDir + filenameYml, yaml.safeDump(parsed));
// Remove Old JSON File
fs.unlinkSync(baseDir + filename);
return {"name": parsed.name, "description": parsed.description, "file": filename};
};
/**
* Main processing function factory
* @param presets the presets object
* @returns {Function} the function which will process all files
*/
var getFileProcessor = function(presets) {
return function (f) {
try {
// Only process files
var s = fs.statSync(baseDir + f);
if (!s.isFile()) {
return;
}
var ext = path.extname(f);
var basename = path.basename(f);
var p;
// Process yaml or json files
switch (ext) {
case '.xml':
p = processXml(f);
presets[p.file] = p;
break;
case '.json':
p = processJson(f);
presets[p.file] = p;
break;
}
} catch (err) {
console.error(err);
}
}
};
var exec = function() {
// Create output directory
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
var dir = fs.readdirSync(baseDir);
var output = {
revision: process.argv[2],
lastUpdate: new Date().toISOString(),
presets: {}
};
/**
* Parse directory
*/
dir.forEach(getFileProcessor(output.presets));
fs.writeFileSync(outputDir + 'presets.json', JSON.stringify(output));
};
// Execute code
exec();