Files
it-tools-pro/scripts/build-locales-files.mjs
T
personal82555 01ff133ac9 IT-TOOLS-PRO v1 - 定制版开发者工具站
12个原创工具(批量条码/二维码识别/在线翻译/OCR/面单设计/AI修图/网站收藏/PDF工具/图片工具/文档转换等)
+ 卡密授权系统(CardAuth对接,30分钟试用,换卡/撤销)
+ 体验优化(中文默认/收藏/最近使用/菜单展开/宽屏/品牌页脚)
2026-10-11 23:58:39 +08:00

62 lines
1.4 KiB
JavaScript

import { existsSync, writeFileSync } from 'node:fs';
import { Glob } from 'bun';
import _ from 'lodash';
async function getPathsFromGlobs({ patterns, onlyFiles = true }) {
const filePaths = [];
for (const pattern of patterns) {
const glob = new Glob(pattern);
for await (const filePath of glob.scan({ onlyFiles, cwd: '.' })) {
filePaths.push(filePath);
}
}
return { filePaths };
}
function getLocaleKey({ filePath }) {
const fileName = filePath.split('/').pop();
return fileName.replace(/\.yml$/, '');
}
async function createMissingLocaleFile({ localeKey }) {
const fileName = `${localeKey}.yml`;
const { filePaths: localesDirs } = await getPathsFromGlobs({
patterns: [
'locales',
'src/tools/*/locales',
],
onlyFiles: false,
});
for (const localesDir of localesDirs) {
const filePath = `${localesDir}/${fileName}`;
if (existsSync(filePath)) {
console.log(`Locale file already exists: ${filePath}`);
continue;
}
console.log(`Creating missing locale file: ${filePath}`);
writeFileSync(filePath, '', 'utf8');
}
}
const { filePaths } = await getPathsFromGlobs({
patterns: [
'locales/*.yml',
'src/tools/*/locales/*.yml',
],
});
await Promise.all(
_.chain(filePaths)
.map(filePath => getLocaleKey({ filePath }))
.uniq()
.map(localeKey => createMissingLocaleFile({ localeKey }))
.value(),
);