GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — aicodingstack.io/scripts/refactor/sort-locales-fields.ts at main · aicodingstack/aicodingstack.io Expand file tree/ Copy pathsort-locales-fields.tsMore file actions 121 lines (96 loc) · 3.31 KB / Copy pathsort-locales-fields.tsFile metadata and controls121 lines (96 loc) · 3.31 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 #!/usr/bin/env node /** * Sort fields in all translation JSON files alphabetically * This script recursively processes all JSON files in the translations directory * and sorts their object keys alphabetically (including nested objects). */ import fs from 'node:fs/promises' import path from 'node:path' import { fileURLToPath } from 'node:url' const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) const ROOT_DIR = path.resolve(__dirname, '../..') const LOCALES_DIR = path.join(ROOT_DIR, 'translations') interface JsonFile { fullPath: string relativePath: string } /** * Recursively sort object keys alphabetically * Arrays are preserved as-is, only object keys are sorted */ function sortObjectKeysRecursively(obj: unknown): unknown { if (Array.isArray(obj)) { return obj.map(item => sortObjectKeysRecursively(item)) } if (obj && typeof obj === 'object') { const sorted: Record<string, unknown> = {} const keys = Object.keys(obj).sort() for (const key of keys) { sorted[key] = sortObjectKeysRecursively((obj as Record<string, unknown>)[key]) } return sorted } return obj } /** * Process a single JSON file */ async function processJsonFile(filePath: string, relativePath: string): Promise<void> { try { // Read the file const content = await fs.readFile(filePath, 'utf-8') const data = JSON.parse(content) // Sort the data recursively const sortedData = sortObjectKeysRecursively(data) // Write back to file with 2-space indentation const jsonContent = `${JSON.stringify(sortedData, null, 2)}\n` await fs.writeFile(filePath, jsonContent, 'utf-8') console.log(` ✅ ${relativePath}`) } catch (error) { console.error(` ❌ Error processing ${relativePath}:`, (error as Error).message) } } /** * Recursively find all JSON files in a directory */ async function findJsonFiles(dirPath: string, basePath = dirPath): Promise<JsonFile[]> { const jsonFiles: JsonFile[] = [] try { const entries = await fs.readdir(dirPath, { withFileTypes: true }) for (const entry of entries) { const fullPath = path.join(dirPath, entry.name) if (entry.isDirectory()) { // Recursively search subdirectories const subFiles = await findJsonFiles(fullPath, basePath) jsonFiles.push(...subFiles) } else if (entry.isFile() && entry.name.endsWith('.json')) { // Calculate relative path from base const relativePath = path.relative(basePath, fullPath) jsonFiles.push({ fullPath, relativePath }) } } } catch (error) { console.error(` ⚠️ Error reading directory ${dirPath}:`, (error as Error).message) } return jsonFiles } /** * Main function */ async function main(): Promise<void> { console.log('🔄 Sorting translations JSON files alphabetically...\n') // Find all JSON files in translations directory const jsonFiles = await findJsonFiles(LOCALES_DIR) if (jsonFiles.length === 0) { console.log('⚠️ No JSON files found in translations directory') return } console.log(`📁 Found ${jsonFiles.length} JSON file(s) to process\n`) // Process each file for (const { fullPath, relativePath } of jsonFiles) { await processJsonFile(fullPath, relativePath) } console.log('\n✨ Done!') } main().catch(console.error) |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |