-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (111 loc) · 3.46 KB
/
index.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
127
128
129
130
131
132
133
134
135
var http = require('http');
var https = require('https');
var fs = require('fs');
var coffee = require('coffee-script');
var optimist = require('optimist');
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
optimist.usage(
'Usage:\n' +
' seed-translation --project [name] --env [local staging production] --deploy-path [file/path]\n\n' +
'Examples:\n' +
' seed-translation --project galaxy_zoo --env production --deploy-path locales\n' +
' seed-translation --project galaxy_zoo --env staging --deploy-path locales\n'
);
var options = optimist.options({
h: { alias: 'help', description: 'Print usage' },
p: { alias: 'project', description: 'Project name ' },
e: { alias: 'env', description: 'Environment (local, staging, or production)' },
d: { alias: 'deploy-path', description: 'Deploy path for your language files' }
}).demand('env').demand('project').argv
var post = function (seed) {
var json = {
translation: seed.data,
locale: seed.locale.toLowerCase().replace(/_/, '-')
};
var servers = {
local: { host: '127.0.0.1', port: 3000, http: http },
staging: { host: 'dev.zooniverse.org', port: 443, http: https },
production: { host: 'api.zooniverse.org', port: 443, http: https }
};
var server = servers[options.env];
var message = 'Seeding locale to ' + options.env;
if(options['deploy-path']) {
json.deploy_path = options['deploy-path'];
message += ' with deploy path ' + json.deploy_path
}
json = JSON.stringify(json);
var opts = {
host: server.host,
port: server.port,
path: '/projects/' + options.project + '/translations/seed',
method: 'POST',
auth: process.env.OUROBOROS_AUTH,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(json)
}
};
console.log(message);
var request = server.http.request(opts, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
try {
var resultObject = JSON.parse(responseString);
if(resultObject.id) {
console.log('Done');
}
else {
console.log('Something went wrong');
}
}
catch(e) {
console.log('Something went wrong');
}
});
});
request.write(json);
request.end();
}
var checkPath = function(path) {
path = process.cwd() + '/' + path;
if(fs.existsSync(path)) {
return path;
}
else {
return null;
}
}
var findSeed = function() {
var seedLocales = ['en', 'en-us', 'en-US', 'en_us', 'en_US', 'en-gb', 'en-GB', 'en_gb', 'en_GB'];
var localePaths = ['app/lib', 'app/translations', 'lib', 'translations'];
var localeExtensions = ['coffee', 'json'];
var seedPath = null;
var seedLocale = null;
localePaths.forEach(function(dir) {
seedLocales.forEach(function(seed) {
localeExtensions.forEach(function(ext) {
seedLocale = seedLocale || seed
seedPath = seedPath || checkPath(dir + '/' + seed + '.' + ext);
});
});
});
return { path: seedPath, locale: seedLocale };
}
var seed = findSeed();
if(!seed.path) {
console.log('Could not find a language file');
process.exit(0)
}
console.log('Found language file at', seed.path);
seed.data = fs.readFileSync(seed.path, 'utf-8');
if(seed.path.match(/\.coffee$/)) {
seed.data = coffee.eval(seed.data);
}
else {
seed.data = JSON.parse(seed.data);
}
post(seed);