-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtx
executable file
·210 lines (185 loc) · 6.08 KB
/
tx
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env node
(function (crypto, rest, Q) {
'use strict';
var action = process.argv[2];
var txProject = process.argv[3];
var txResource = process.argv[4];
// skip (node + tx.js + action + project + resource)
var args = process.argv.splice(5);
txResource = txResource.replace(/\./g, '');
var baseUrl = 'https://www.transifex.com/api/2';
var transifexResourceUrl = baseUrl + '/project/' + txProject + '/resource/' + txResource + '/content';
switch (action) {
case 'rm':
getResource()
.then(removeKeysFromResource, throwError)
.then(updateTransifexResource, throwError)
.done();
break;
case 'add':
getResource()
.then(addKeysToResource, throwError)
.then(updateTransifexResource, throwError)
.done();
break;
case 'comment':
getStringFromTx(args[0])
.then(function () {
return setCommentForString(args[0], args[1]);
}, throwError)
.done();
break;
default:
console.log('The action "' + action + '" is not supported');
}
/**
* Get resource content from Transifex
* @return {Q.Promise}
*/
function getResource() {
var deferred = Q.defer();
rest
.get(transifexResourceUrl, {
username: process.env.TX_USER,
password: process.env.TX_PASS
})
.on('success', function (data, response) {
deferred.resolve(JSON.parse(data.content));
})
.on('fail', function (err, response) {
deferred.reject(err);
})
.on('error', function (err, response) {
deferred.reject(err);
});
return deferred.promise;
}
/**
* Update resource on Transifex with the new content
* @param {Object} resourceContent
* @return {Q.Promise}
*/
function updateTransifexResource(resourceContent) {
var deferred = Q.defer();
console.log('Uploading updated resource to Transifex...');
rest.put(transifexResourceUrl, {
headers: {
"Content-Type": "application/json"
},
data: JSON.stringify({ "content": JSON.stringify(resourceContent)}),
username: process.env.TX_USER,
password: process.env.TX_PASS
})
.on('success', function (data, response) {
console.log('Transifex resource updated');
deferred.resolve(data);
})
.on('fail', function (err, response) {
deferred.reject(err);
});
return deferred.promise;
}
/**
* Remove keys (args) from resource content
* @param {Object} resourceContent
* @return {Q.Promise}
*/
function removeKeysFromResource(resourceContent) {
console.log('Removing keys from resource "' + txResource + '" of project "' + txProject + '"');
var resourceKeysBefore = Object.keys(resourceContent);
args.forEach(function (key) {
delete resourceContent[key];
});
var resourceKeysAfter = Object.keys(resourceContent);
console.log('Number of keys that will be deleted: ' + (resourceKeysBefore.length - resourceKeysAfter.length));
return Q.when(resourceContent);
}
/**
* Add keys (args) to resource content
* @param {Object} resourceContent
* @return {Q.Promise}
*/
function addKeysToResource(resourceContent) {
console.log('Adding keys to resource "' + txResource + '" of project "' + txProject + '"');
var resourceKeysBefore = Object.keys(resourceContent);
args.forEach(function (key) {
if (!resourceContent[key]) {
resourceContent[key] = key;
}
});
var resourceKeysAfter = Object.keys(resourceContent);
console.log('Number of keys that will be added: ' + (resourceKeysAfter.length - resourceKeysBefore.length));
return Q.when(resourceContent);
}
/**
* Get the string from Transifex to check if it exists
* @param {String} word
* @return {Q.Promise}
*/
function getStringFromTx(word) {
var deferred = Q.defer();
var url = baseUrl + '/project/' + txProject + '/resource/' + txResource + '/source/' + getHashString(word);
console.log('Fetching string "' + word + '" on Transifex...');
rest
.get(url, {
username: process.env.TX_USER,
password: process.env.TX_PASS
})
.on('success', function (data, response) {
console.log('String "' + word + '" found!');
deferred.resolve(data);
})
.on('fail', function (err, response) {
console.log('String "' + word + '" not found!');
deferred.reject(err);
})
.on('error', function (err, response) {
deferred.reject(err);
});
return deferred.promise;
}
/**
* Update word on the Transifex resource to display the users's comment
* @param {String} word
* @param {String} comment
* @return {Q.Promise}
*/
function setCommentForString(word, comment) {
var deferred = Q.defer();
var url = baseUrl + '/project/' + txProject + '/resource/' + txResource + '/source/' + getHashString(word);
console.log('Adding comment "' + comment + '" on string "' + word + '" on Transifex...');
rest.put(url, {
headers: {
"Content-Type": "application/json"
},
data: JSON.stringify({ "comment": comment }),
username: process.env.TX_USER,
password: process.env.TX_PASS
})
.on('success', function (data, response) {
console.log('Transifex string "' + word + '" successfully updated!');
deferred.resolve(data);
})
.on('fail', function (err, response) {
console.log('Transifex string "' + word + '" could not be updated.');
deferred.reject(err);
});
return deferred.promise;
}
/**
* Compute the hash string for the given word+context
* @param {String} word
* @param {String} context
* @return {String}
*/
function getHashString(word, context) {
word = word.replace(/\./g, '\\.');
context = context || '';
var md5sum = crypto.createHash('md5');
md5sum.update([word, context].join(':'), 'utf-8');
return md5sum.digest('hex');
}
function throwError(err) {
throw new Error(err);
}
})(require('crypto'), require('restler'), require('q'));