-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
executable file
·83 lines (79 loc) · 1.86 KB
/
util.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
#!/usr/bin/env node
module.exports.normalizeLocale = function(locale) {
var l = locale.split(/[-_]/);
l[0] = l[0].toLowerCase();
if( l[1] ){
l[1] =l[1].toUpperCase();
} else {
if(l[0] == 'en') {
l[1] = 'US';
} else {
l[1] = l[0].toUpperCase();
}
}
return l[0] + '_' + l[1];
}
var appConfig = module.exports.appConfig = function(inProduction) {
var file = process.env.APP_CONFIG ;
file = file || require('fs').readFileSync(__dirname + '/app_config'+(inProduction?'.prod':'') +'.json');
var cfg = JSON.parse( file );
var toIana = [],
fromIana = [],
toShort = [],
fromShort = [];
for (var i in cfg['LANGS']) {
var l = cfg['LANGS'][i],
iana = l.replace(/_/,'-'),
short = l.split('_')[0];
toIana[l] = iana;
fromIana[iana] = l;
toShort[l] = short;
fromShort[short] = l;
}
cfg['LANGS_DESC'] = {
toIana: toIana,
fromIana: fromIana,
toShort: toShort,
fromShort: fromShort,
};
return cfg;
}
if (!module.parent) {
var argv = require('optimist').boolean('dev').boolean('json').argv,
sys = require('sys'),
path = require('path'),
APP_CONFIG = appConfig(!argv.dev);
if(argv._[0] == 'readCfg') {
var node = APP_CONFIG;
for(var i=1; i<argv._.length && node; ++i){
node = node[argv._[i]]
}
if(node){
if(argv.json) {
sys.puts(JSON.stringify(node));
} else {
if(typeof(node) == 'string') {
sys.puts(node);
} else if(typeof(node) == 'number') {
sys.puts(node);
} else {
for(var i in node){
sys.puts(node[i]);
}
}
}
}
}else if(argv._[0] == "herokuCfg"){
var child_exec = require('child_process').exec;
child_exec("heroku config:add APP_CONFIG='" + JSON.stringify(APP_CONFIG) +"'",
function(error, stdout, stderr){
if (error !== null) {
console.log('exec error: ' + error);
} else {
console.log(stdout);
console.log(stderr);
}
}
);
}
}