UNPKG

auto-translatr

Version:
130 lines (129 loc) 5.41 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.read = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const utils_1 = require("./utils"); // Helper function to recursively find files with specific extensions const findFiles = (dir, config) => { const files = []; try { const items = fs.readdirSync(dir); for (const item of items) { const fullPath = path.join(dir, item); const stat = fs.statSync(fullPath); if (stat.isDirectory()) { // Skip directories that are in the ignore list if (!config.ignore.includes(item)) { files.push(...findFiles(fullPath, config)); } } else if (stat.isFile()) { const ext = path.extname(item); if (config.include.includes(ext)) { files.push(fullPath); } } } } catch (error) { console.warn(`Warning: Could not read directory ${dir}:`, error); } return files; }; // Helper function to extract t('...') calls from file content const extractTranslationCalls = (content) => { // Regex to match t('...') or t("...") calls // This handles escaped quotes and captures the content inside const regex = /\bt\s*\(\s*(['"`])((?:(?!\1)[^\\]|\\.)*)(\1)\s*\)/g; const matches = []; let match; while ((match = regex.exec(content)) !== null) { // match[2] contains the text inside the quotes const text = match[2] .replace(/\\'/g, "'") // Unescape single quotes .replace(/\\"/g, '"') // Unescape double quotes .replace(/\\\\/g, '\\'); // Unescape backslashes if (text.trim()) { matches.push(text.trim()); } } return matches; }; const read = (config) => __awaiter(void 0, void 0, void 0, function* () { try { console.log('🔍 Searching for translation calls in codebase...'); // Start from current working directory const rootDir = process.cwd(); const files = findFiles(rootDir, config); console.log(`📁 Found ${files.length} files to scan`); // Collect all unique translation strings const translationStrings = new Set(); for (const filePath of files) { try { const content = fs.readFileSync(filePath, 'utf-8'); const calls = extractTranslationCalls(content); if (calls.length > 0) { console.log(`📄 Found ${calls.length} translation calls in ${path.relative(rootDir, filePath)}`); calls.forEach(call => translationStrings.add(call)); } } catch (error) { console.warn(`Warning: Could not read file ${filePath}:`, error); } } console.log(`📝 Found ${translationStrings.size} unique translation strings`); // Convert to translations object with kebab-case keys const translations = {}; translationStrings.forEach(text => { translations[text] = text; }); return (0, utils_1.right)(translations); } catch (error) { return (0, utils_1.left)(error); } }); exports.read = read;