-
Notifications
You must be signed in to change notification settings - Fork 0
/
lokalise_upload.js
57 lines (48 loc) · 1.88 KB
/
lokalise_upload.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
// request Upload file
(function(callback) {
'use strict';
if (!process.env.LOKALISE_APIKEY) {
console.log("Please setup the LOKALISE_APIKEY env before using this script");
return;
}
const httpTransport = require('https');
const responseEncoding = 'utf8';
const httpOptions = {
hostname: 'api.lokalise.com',
port: '443',
path: '/api2/projects/983881975d551c5fdba845.54008358/files/upload',
method: 'POST',
headers: {"Content-Type":"application/json","x-api-token":process.env.LOKALISE_APIKEY}
};
httpOptions.headers['User-Agent'] = 'node ' + process.version;
const fs = require('fs');
const contents = fs.readFileSync('src/translations/en.json', {encoding: 'base64'});
//console.log("file contents", contents);
const request = httpTransport.request(httpOptions, (res) => {
let responseBufs = [];
let responseStr = '';
res.on('data', (chunk) => {
if (Buffer.isBuffer(chunk)) {
responseBufs.push(chunk);
}
else {
responseStr = responseStr + chunk;
}
}).on('end', () => {
responseStr = responseBufs.length > 0 ?
Buffer.concat(responseBufs).toString(responseEncoding) : responseStr;
callback(null, res.statusCode, res.headers, responseStr);
});
})
.setTimeout(0)
.on('error', (error) => {
callback(error);
});
request.write("{\"filename\":\"en.json\",\"data\":\"" + contents + "\",\"lang_iso\":\"en\",\"tags\":[\"js_upload\",\"profile\"]}")
request.end();
})((error, statusCode, headers, body) => {
console.log('ERROR:', error);
console.log('STATUS:', statusCode);
console.log('HEADERS:', JSON.stringify(headers));
console.log('BODY:', body);
});