Skip to content

Commit f75858f

Browse files
authored
Merge pull request #1 from FRSgit/feat--add-possibility-to-customize-push-&-pull-requests
feat: add possibility to customize pull & push requests & syncTerms
2 parents 1812919 + 32b2f46 commit f75858f

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 1.15.0
2+
3+
`2021-09-02`
4+
5+
- 🌟 Added possibility to pass custom parameters to pull or push API requests
6+
- 🌟 Added syncTerms option support
7+
18
## 1.14.0
29

310
`2021-03-11`

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ Create a **poeditor-config.json** in the root directory, and config information
3737
"apiToken": "", // POEditor api token
3838
"projectId": 0, // project id
3939
"fileType": "", // fileType to upload or download, supports files format (po, pot, mo, xls, csv, resw, resx, android_strings, apple_strings, xliff, properties, key_value_json, json, xmb, xtb)
40-
"targetDir": "" // directory where translated files live
40+
"targetDir": "", // directory where translated files live
41+
"syncTerms": true, // (optional) set it to true if you want to sync your terms (terms that are not found in the uploaded file will be deleted from project and the new ones added)
42+
"sourceLang": "en-US", // (optional, required when syncTerms set to true) language to sync the terms from
43+
"pullParams": {}, // (optional) allows to pass any parameters available on the POEditor's export endpoint, full list here: https://poeditor.com/docs/api#projects_export
44+
"pushParams": {} // (optional) allows to pass any parameters available on the POEditor's upload endpoint, full list here: https://poeditor.com/docs/api#projects_upload
4145
}
4246
```
4347

packages/commands/pull.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ async function getTermFiles(config) {
6464
id: config.projectId,
6565
language: lang.code,
6666
type: tempFileType,
67+
...(config.pullParams || {})
6768
};
6869

6970
try {

packages/commands/push.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function push() {
6060

6161
async function putTermFiles(config) {
6262
const targetDir = path.resolve(cwd, config.targetDir);
63-
const paths = await globby([targetDir]);
63+
let paths = await globby([targetDir]);
6464
const sleep = (func, timeout) => {
6565
return new Promise(async (resolve) => {
6666
setTimeout(() => {
@@ -69,12 +69,24 @@ async function putTermFiles(config) {
6969
}, timeout);
7070
});
7171
};
72+
73+
paths = paths.map(url => ({ url, parsedUrl: path.parse(url) }));
74+
75+
if (config.syncTerms) {
76+
// if syncTerms === true make sure sourceLang will be the last in the paths array
77+
// (we want to sourceLang terms be synced as a last request so they won't get overwritten by terms from other locale files)
78+
paths.sort((a, b) => {
79+
if (a.parsedUrl.name === config.sourceLang) return 1;
80+
if (b.parsedUrl.name === config.sourceLang) return -1;
81+
return 0;
82+
});
83+
}
7284

73-
const promises = paths.map((url, index) => {
85+
const promises = paths.map(({ url, parsedUrl }, index) => {
7486
return new Promise(async (resolve, reject) => {
75-
let timeout = index * 30000 + 10;
87+
const timeout = index * 30000 + 10;
7688
return sleep(async () => {
77-
if (path.parse(url).ext.slice(1) !== fileTypeMap[config.fileType]) {
89+
if (parsedUrl.ext.slice(1) !== fileTypeMap[config.fileType]) {
7890
console.log(
7991
chalk.red(
8092
`\n 😭 Incorrect fileType, ${
@@ -89,7 +101,7 @@ async function putTermFiles(config) {
89101
await putTermFile({
90102
...config,
91103
file: url,
92-
language: path.parse(url).name,
104+
language: parsedUrl.name,
93105
});
94106
resolve(null);
95107
} catch (err) {
@@ -121,6 +133,15 @@ async function putTermFile(config) {
121133
formData.append("file", fs.createReadStream(config.file));
122134
formData.append("overwrite", "1");
123135

136+
if (config.syncTerms && config.sourceLang && config.language === config.sourceLang) {
137+
formData.append('sync_terms', '1');
138+
}
139+
140+
if (config.pushParams) {
141+
Object.keys(config.pushParams)
142+
.forEach(paramName => formData.set(paramName, config.pushParams[paramName]))
143+
}
144+
124145
const res = await api.post("/projects/upload", formData, {
125146
headers: {
126147
"Content-Type": `multipart/form-data; boundary=${formData._boundary}`,

0 commit comments

Comments
 (0)