-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsonp.js
57 lines (47 loc) · 1.68 KB
/
jsonp.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
var fs=require('fs');
var path = require('path');
var config = require('./config.json')
var _parser = require('./parser'); // it import global variable parser instead, go hell
var _yaml_parser = require('./yaml_parser');
function isVaild(fname){
var cl =fname.split('.');
return cl[cl.length - 1] === 'txt';
}
function techDataMap(root){
//root = '';
var files = fs.readdirSync(root);
var dataMap = {};
for(fn in files){
console.log(fn, files[fn], isVaild(files[fn]));
if (isVaild(files[fn])){
//var path = root + '\\' + files[fn];
var _path = path.join(root,files[fn]);
var data = fs.readFileSync(_path, 'utf-8');
//console.log(data);
dataMap[files[fn]] = parser.parse(data);
}
}
return dataMap;
}
function localDataMap(_path){
console.log(_path);
var data = fs.readFileSync(_path, 'utf-8');
var dataMap = yaml_parser.parse(data);
return dataMap[Object.keys(dataMap)[0]]; // yaml_parser.parse will return country key, simplify it.
}
function allDataMap(game_root){
var localPath = path.join(game_root,'localisation',config.localFile);
var techPath = path.join(game_root,'common','technology');
var techMap = techDataMap(techPath);
var localMap = localDataMap(localPath);
return {technology : techMap,localisation:localMap};
}
function toJSONP(game_root, funcName, outputName){
var techMap = allDataMap(game_root);
var jsonpString = funcName+'(' + JSON.stringify(techMap) + ')';
fs.writeFileSync(outputName, jsonpString);
}
//var data = toJSONP('./technology', 'callback');
//fs.writeFileSync('jsonpData.js', data);
//toJSONP('./technology', 'loadData', 'jsonpData.js');
toJSONP(config["stellaris-path"],'loadData','jsonpData.js');