forked from appium/appium-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crowdin-sync-translations.js
96 lines (89 loc) · 2.91 KB
/
crowdin-sync-translations.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
const { logger, tempDir, fs, zip } = require('appium-support');
const request = require('request');
const path = require('path');
const { createWriteStream } = require('fs');
const B = require('bluebird');
const { asyncify } = require('asyncbox');
const log = logger.getLogger('CROWDIN');
const PROJECT_ID = process.env.CROWDIN_PROJECT_ID;
const PROJECT_KEY = process.env.CROWDIN_PROJECT_KEY;
if (!PROJECT_ID || !PROJECT_KEY) {
throw new Error(`Both CROWDIN_PROJECT_ID and CROWDIN_PROJECT_KEY environment ` +
`variables must be set`);
}
const RESOURCES_ROOT = path.resolve('assets', 'locales');
const ORIGINAL_LANGUAGE = 'en';
const USER_AGENT = 'Appium Desktop';
async function exportTranslations () {
const options = {
url: `https://api.crowdin.com/api/project/${PROJECT_ID}/export?key=${PROJECT_KEY}`,
port: 443,
method: 'GET',
headers: {
'User-Agent': USER_AGENT,
},
};
return await new B((resolve, reject) => {
request(options)
.on('error', reject)
.on('response', (res) => {
if (res.statusCode >= 400) {
return reject(`Cannot export the translated resources in Crowdin. Error code: ${res.statusCode}`);
}
log.info(`Successfully exported Crowdin translations`);
})
.on('close', resolve);
});
}
async function downloadTranslations (dstPath) {
const options = {
url: `https://api.crowdin.com/api/project/${PROJECT_ID}/download/all.zip?key=${PROJECT_KEY}`,
port: 443,
method: 'GET',
headers: {
'User-Agent': USER_AGENT,
},
};
return await new B((resolve, reject) => {
request(options)
.on('error', reject)
.on('response', (res) => {
if (res.statusCode >= 400) {
return reject(`Cannot download the translated resources from Crowdin. Error code: ${res.statusCode}`);
}
log.info(`Successfully downloaded Crowdin translations`);
})
.pipe(createWriteStream(dstPath))
.on('close', resolve);
});
}
async function main () {
await exportTranslations();
const zipPath = await tempDir.path({prefix: 'translations', suffix: '.zip'});
try {
await downloadTranslations(zipPath);
const tmpRoot = await tempDir.openDir();
try {
await zip.extractAllTo(zipPath, tmpRoot);
for (const name of await fs.readdir(tmpRoot)) {
const currentPath = path.join(tmpRoot, name);
if (!(await fs.stat(currentPath)).isDirectory() || name === ORIGINAL_LANGUAGE) {
continue;
}
const dstPath = path.resolve(RESOURCES_ROOT, name);
if (await fs.exists(dstPath)) {
await fs.rimraf(dstPath);
}
await fs.mv(currentPath, path.resolve(RESOURCES_ROOT, name), {
mkdirp: true
});
log.info(`Successfully updated resources for the '${name}' language`);
}
} finally {
await fs.rimraf(tmpRoot);
}
} finally {
await fs.rimraf(zipPath);
}
}
asyncify(main);