chore: add i18n query script (#2518)

This commit is contained in:
Finley Ge
2024-08-26 12:31:30 +08:00
committed by GitHub
parent dbfe1fca31
commit a1a9a0b463
5 changed files with 185 additions and 2 deletions

View File

@@ -10,7 +10,8 @@
"postinstall": "sh ./scripts/postinstall.sh",
"initIcon": "node ./scripts/icon/init.js",
"previewIcon": "node ./scripts/icon/index.js",
"checkI18n": "node ./scripts/i18n/delete-unused-keys.js"
"i18n:delete-unused-keys": "node ./scripts/i18n/delete-unused-keys.js",
"i18n:query": "node ./scripts/i18n/query.js"
},
"devDependencies": {
"@chakra-ui/cli": "^2.4.1",

24
pnpm-lock.yaml generated
View File

@@ -657,6 +657,12 @@ importers:
specifier: ^5.1.3
version: 5.5.3
scripts/i18n:
devDependencies:
'@phenomnomnominal/tsquery':
specifier: ^6.1.3
version: 6.1.3(typescript@5.5.3)
scripts/icon:
dependencies:
express:
@@ -2814,6 +2820,11 @@ packages:
'@petamoriken/float16@3.8.7':
resolution: {integrity: sha512-/Ri4xDDpe12NT6Ex/DRgHzLlobiQXEW/hmG08w1wj/YU7hLemk97c+zHQFp0iZQ9r7YqgLEXZR2sls4HxBf9NA==}
'@phenomnomnominal/tsquery@6.1.3':
resolution: {integrity: sha512-CEqpJ872StsxRmwv9ePCZ4BCisrJSlREUC5XxIRYxhvODt4aQoJFFmjTgaP6meyKiiXxxN/VWPZ58j4yHXRkmw==}
peerDependencies:
typescript: ^3 || ^4 || ^5
'@pkgjs/parseargs@0.11.0':
resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==}
engines: {node: '>=14'}
@@ -3229,6 +3240,9 @@ packages:
'@types/eslint@8.56.10':
resolution: {integrity: sha512-Shavhk87gCtY2fhXDctcfS3e6FdxWkCx1iUZ9eEUbh7rTqlZT0/IzOkCOVt0fCjcFuZ9FPYfuezTBImfHCDBGQ==}
'@types/esquery@1.5.4':
resolution: {integrity: sha512-yYO4Q8H+KJHKW1rEeSzHxcZi90durqYgWVfnh5K6ZADVBjBv2e1NEveYX5yT2bffgN7RqzH3k9930m+i2yBoMA==}
'@types/estree-jsx@1.0.5':
resolution: {integrity: sha512-52CcUVNFyfb1A2ALocQw/Dd1BQFNmSdkuC3BkZ6iqhdMfQz7JWOFRuJFloOzjk+6WijU56m9oKXFAXc7o3Towg==}
@@ -11489,6 +11503,12 @@ snapshots:
'@petamoriken/float16@3.8.7': {}
'@phenomnomnominal/tsquery@6.1.3(typescript@5.5.3)':
dependencies:
'@types/esquery': 1.5.4
esquery: 1.6.0
typescript: 5.5.3
'@pkgjs/parseargs@0.11.0':
optional: true
@@ -11947,6 +11967,10 @@ snapshots:
'@types/estree': 1.0.5
'@types/json-schema': 7.0.15
'@types/esquery@1.5.4':
dependencies:
'@types/estree': 1.0.5
'@types/estree-jsx@1.0.5':
dependencies:
'@types/estree': 1.0.5

15
scripts/i18n/package.json Normal file
View File

@@ -0,0 +1,15 @@
{
"name": "i18n",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@phenomnomnominal/tsquery": "^6.1.3"
}
}

94
scripts/i18n/query.js Normal file
View File

@@ -0,0 +1,94 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var tsquery_1 = require("@phenomnomnominal/tsquery");
var path = require("path");
var fs = require("fs");
//
var root = path.join(__dirname, '../../');
// get all files in the project recursively
function getAllFiles(dirPath, arrayOfFiles) {
if (arrayOfFiles === void 0) { arrayOfFiles = []; }
var files = fs.readdirSync(dirPath);
files.forEach(function (file) {
var filePath = path.join(dirPath, file);
if (fs.statSync(filePath).isDirectory()) {
arrayOfFiles = getAllFiles(filePath, arrayOfFiles);
}
else {
arrayOfFiles.push(filePath);
}
});
return arrayOfFiles;
}
var allFiles = getAllFiles(root).filter(function (file) { return file.endsWith('.ts') || file.endsWith('.tsx'); })
.filter(function (file) { return !file.includes('node_modules'); })
.filter(function (file) { return !file.includes('jieba'); });
function processFiles(allFiles) {
return __awaiter(this, void 0, void 0, function () {
var fileContents, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, Promise.all(allFiles.map(function (file) { return fs.readFileSync(file, 'utf-8'); }))];
case 1:
fileContents = _a.sent();
// 处理每个文件的内容
fileContents.forEach(function (content, index) {
var astTree = (0, tsquery_1.ast)(content);
var res = (0, tsquery_1.query)(astTree, 'JsxText,StringLiteral');
for (var _i = 0, res_1 = res; _i < res_1.length; _i++) {
var node = res_1[_i];
var text = node.getText().trim();
if (text.length > 0 && text.match(/[\u4e00-\u9fa5]/g)) {
console.log(allFiles[index], text);
}
}
});
return [3 /*break*/, 3];
case 2:
error_1 = _a.sent();
console.error('Error processing files:', error_1);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
});
}
processFiles(allFiles);

49
scripts/i18n/query.ts Normal file
View File

@@ -0,0 +1,49 @@
import { ast, query } from '@phenomnomnominal/tsquery';
import * as path from 'path';
import * as fs from 'fs';
//
const root = path.join(__dirname, '../../');
// get all files in the project recursively
function getAllFiles(dirPath: string, arrayOfFiles: string[] = []): string[] {
const files = fs.readdirSync(dirPath);
files.forEach((file) => {
const filePath = path.join(dirPath, file);
if (fs.statSync(filePath).isDirectory()) {
arrayOfFiles = getAllFiles(filePath, arrayOfFiles);
} else {
arrayOfFiles.push(filePath);
}
});
return arrayOfFiles;
}
const allFiles = getAllFiles(root)
.filter((file) => file.endsWith('.ts') || file.endsWith('.tsx'))
.filter((file) => !file.includes('node_modules'))
.filter((file) => !file.includes('jieba'));
async function processFiles(allFiles: string[]) {
try {
// 并行读取所有文件内容
const fileContents = await Promise.all(allFiles.map((file) => fs.readFileSync(file, 'utf-8')));
// 处理每个文件的内容
fileContents.forEach((content, index) => {
const astTree = ast(content);
const res = query(astTree, 'JsxText,StringLiteral');
for (const node of res) {
const text = node.getText().trim();
if (text.length > 0 && text.match(/[\u4e00-\u9fa5]/g)) {
console.log(allFiles[index], text);
}
}
});
} catch (error) {
console.error('Error processing files:', error);
}
}
processFiles(allFiles);