-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.js
More file actions
99 lines (84 loc) · 2.38 KB
/
lib.js
File metadata and controls
99 lines (84 loc) · 2.38 KB
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
#! /usr/bin/env node
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const request = require('request');
const downloadFile = require('download-file');
function getSettings(argv) {
let argvSettings = _.find(argv, (a) => a.indexOf('--settings') >= 0) || '';
let paramsString = argvSettings.split('=')[1];
if (!paramsString) {
paramsString = 'settings.json';
}
return require(path.resolve(process.cwd(), paramsString));
}
function parseProjects(argv) {
const settings = getSettings(argv);
let argvProjects = _.find(argv, (a) => a.indexOf('--projects') >= 0);
if (!argvProjects) {
return [];
}
let paramsString = argvProjects.split('=')[1];
let paramsAr = paramsString ? paramsString.split(',') : [];
return _.filter(settings.projects, (p) => paramsAr.indexOf(p.key) >= 0);
}
function exportTranslation(api_token, project, langCode, destination) {
const req = {
url: 'https://api.poeditor.com/v2/projects/export',
body: `api_token=${api_token}&id=${project.id}&language=${langCode}&type=key_value_json`,
};
return makeRequest(req)
.then((response) => {
return response.body.result.url;
})
.then((url) => downloadFile(url, {
directory: path.resolve(destination, project.path),
filename: `${langCode}.json`
}, (e) => {
if (e) {
console.log(e);
throw e;
}
}));
}
function importTranslation(api_token, project, langCode, destination, overwrite = 0, syncTerms = 0) {
const req = {
url: 'https://api.poeditor.com/v2/projects/upload',
formData: {
api_token: api_token,
id: project.id,
language: langCode,
updating: 'terms_translations',
overwrite,
sync_terms: syncTerms,
file: {
value: fs.createReadStream(path.resolve(destination, project.path, `${langCode}.json`)),
options: {
filename: `${langCode}.json`
}
}
}
};
return makeRequest(req);
}
function makeRequest(req) {
const defaultReq = {
method: 'POST',
json: true,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
};
return new Promise((resolve, reject) => {
request(_.merge(defaultReq, req), (err, res) => {
if (err) {
return reject(err);
}
resolve(res);
});
});
}
module.exports = {
getSettings,
parseProjects,
exportTranslation,
importTranslation,
};