forked from live-codes/livecodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n-upload.mjs
More file actions
105 lines (87 loc) · 2.94 KB
/
Copy pathi18n-upload.mjs
File metadata and controls
105 lines (87 loc) · 2.94 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
100
101
102
103
104
105
import { LokaliseApi } from '@lokalise/node-api';
import fs from 'fs';
import path from 'path';
import { exit } from 'process';
const outDir = path.resolve('src/livecodes/i18n/locales/en');
const api = new LokaliseApi({ apiKey: process.env.LOKALISE_API_TOKEN });
const projectID = process.env.LOKALISE_PROJECT_ID;
const uploadParams = {
// Since we're uploading files from Github where related history is stored, we can safely use cleanup_mode
cleanup_mode: true,
replace_modified: true,
convert_placeholders: false,
};
const pushToLokalise = () => {
const ciMode = process.env.CI === 'true';
const forceLocalMode = process.argv.slice(2).includes('--force');
if (!ciMode && !forceLocalMode) {
console.error('This script is intended to be run in CI mode or with --force flag.');
exit(1);
}
const branchName = process.argv[2];
if (!branchName) {
console.error('Branch name is required');
exit(1);
}
if (!fs.existsSync(outDir)) {
console.error(`Directory ${outDir} doesn't exist, please run i18n-export first`);
exit(1);
}
fs.readdir(outDir, async (err, files) => {
if (err) {
console.error(err);
exit(1);
}
const data = files
.filter((file) => file.endsWith('.lokalise.json'))
.map((file) => {
return {
data: fs.readFileSync(path.join(outDir, file)).toString('base64'),
filename: file,
lang_iso: 'en',
};
});
console.log(
`Following files will be uploaded to Lokalise:\n${data.map((file) => file.filename).join('\n')}`,
);
// If branch doesn't exist, create it
const branch = await api.branches().list({ project_id: projectID });
if (!branch.items.some((b) => b.name === branchName)) {
console.log(`Branch ${branchName} doesn't exist. Creating...`);
await api.branches().create({ name: branchName }, { project_id: projectID });
}
// Upload files to Lokalise and store their process IDs
const processes = (
await Promise.all(
data.map((file) =>
api.files().upload(`${projectID}:${branchName}`, {
...file,
...uploadParams,
}),
),
)
).map((process) => process.process_id);
// Wait until all files are processed using poll
console.log('Waiting for files to be processed...');
const timeout = 60000;
const delay = 2500;
const startTime = Date.now();
while (true) {
const statuses = await Promise.all(
processes.map((process_id) =>
api.queuedProcesses().get(process_id, { project_id: `${projectID}:${branchName}` }),
),
);
const finished = statuses.every((status) => status.status === 'finished');
if (finished) {
break;
}
if (Date.now() - startTime > timeout) {
console.error('Timeout exceeded. Aborting...');
exit(1);
}
await new Promise((resolve) => setTimeout(resolve, delay));
}
});
};
pushToLokalise();