-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtranslate.shared.mjs
85 lines (70 loc) · 2.39 KB
/
translate.shared.mjs
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
import fs from 'node:fs/promises';
import path from 'node:path';
import { execa } from 'execa';
import picocolors from 'picocolors';
import { log } from './translate.openai.mjs';
export const exit = (message) => {
if (!message) {
// eslint-disable-next-line unicorn/no-process-exit
process.exit(0);
}
console.error(picocolors.red(message));
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
};
export const validExtensions = new Set(['.mdx', '.md']);
export const docsBaseDir = 'docs';
export const i18nBaseDir = 'i18n';
export const translateDir = 'docusaurus-plugin-content-docs/current';
// Recursively get all files in the docs directory.
/** @type {(dir: string) => Promise<string[]>} */
export const walk = async (dir) => {
const files = await fs.readdir(dir);
const entries = await Promise.all(
files.map(async (file) => {
const filePath = path.join(dir, file);
const stats = await fs.stat(filePath);
if (stats.isDirectory()) {
return filePath.startsWith('.') ? [] : walk(filePath);
}
if (!validExtensions.has(path.extname(filePath))) {
return [];
}
return filePath;
})
);
return entries.flat();
};
/**
* Given a list of files, filter out if the target locale file has a newer timestamp in Git.
* @type {(files: string[]) => Promise<string[]>}
*/
export const filterFiles = async (files, locale, sync, check) => {
if (!sync && !check) {
return files;
}
log('Checking for files that need to be translated by comparing timestamps in Git...');
const result = await Promise.all(
files.map(async (file) => {
const targetFile = file.replace(docsBaseDir, path.join(i18nBaseDir, locale, translateDir));
const [sourceTimestamp, targetTimestamp] = await Promise.all([
execa`git log -1 --format=%cd --date=iso-local -- ${file}`,
execa`git log -1 --format=%cd --date=iso-local -- ${targetFile}`,
]);
return sourceTimestamp.stdout > targetTimestamp.stdout ? file : null;
})
);
if (check) {
const outdatedFiles = result.filter(Boolean);
if (outdatedFiles.length > 0) {
exit(
`The following files are outdated and need to be translated:\n${outdatedFiles
.map((file) => ` - ${file}`)
.join('\n')}`
);
}
log(picocolors.green('All files are up to date.'));
exit();
}
return result.filter(Boolean);
};