-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·127 lines (105 loc) · 3.3 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
#! /usr/bin/env node
/* eslint-disable no-console */
const fs = require('fs');
const request = require('request');
const colors = require('colors'); // eslint-disable-line no-unused-vars
function loadConfig () {
const configPath = `${process.cwd()}/.phraseapp.json`;
if (!fs.existsSync(configPath)) {
console.error("Error: pharaseapp config file doesn't exist.".red.bold);
process.exit(1);
}
let phrase;
try {
phrase = JSON.parse(fs.readFileSync(configPath));
} catch (e) {
console.error('Error: Your pharaseapp config file is incorrect .'.red.bold);
process.exit(1);
}
if (!phrase.locales) {
console.error('Error: please provide locales in config file ( for example: locales: "pl_PL" ).'.red.bold);
process.exit(1);
}
if (!phrase.access_token) {
console.error(
'Error: please provide access_token in config file .'.red.bold,
);
process.exit(1);
}
if (!phrase.project_id) {
console.error('Error: please provide project_id in config file .'.red.bold);
process.exit(1);
}
return phrase;
}
function getPhraseAppQueryParams (tag, fallbackLocale, branch) {
const queryParams = {
file_format: 'react_simple_json',
tag,
};
if (fallbackLocale) {
queryParams.include_empty_translations = 'true';
queryParams.fallback_locale_id = fallbackLocale;
}
if (branch) {
queryParams.branch = branch;
}
return Object.keys(queryParams)
.map(key => `${key}=${queryParams[key]}`)
.join('&');
}
async function loadLocale (phrase) {
for (const locale of phrase.locales) {
const filepath = `${phrase.path}${locale.locale_id}.json`;
let file = {};
if (fs.existsSync(filepath)) {
file = JSON.parse(fs.readFileSync(filepath));
}
const tags = locale.tags || [false];
for (const tag of tags) {
await new Promise((resolve, reject) => {
const url = `https://${
phrase.access_token
}@api.phraseapp.com/api/v2/projects/${phrase.project_id}/locales/${
locale.locale_id
}/download?${getPhraseAppQueryParams(tag, locale.fallback_locale_id, phrase.branch)}`;
request.get(url, (error, response, body) => {
if (!error && response.statusCode === 200) {
fs.writeFileSync(
filepath,
JSON.stringify(Object.assign(file, JSON.parse(body)), null, '\t'),
);
console.info(
`Success: locale file [${locale.locale_id}.json]${
tag ? ` for tag ${tag} ` : ' '
}was updated`.green.bold,
);
resolve();
} else {
if (response.statusCode !== 401) {
console.error(
`Error: locale file [${locale.locale_id}.json] error message: ${
JSON.parse(body).message
}`.red.bold,
);
} else {
console.error('Error: Please check your access_token'.red.bold);
}
reject(response);
}
});
});
}
}
}
function loadLocalesFromConfig () {
// load phraseapp config
const phrase = loadConfig();
// make sure dir to load files into exists
if (!fs.existsSync(phrase.path)) {
fs.mkdirSync(phrase.path);
}
// Update configs from phraseapp cli
loadLocale(phrase);
}
loadLocalesFromConfig();