- Convert all require()/module.exports to import/export across 260+ files - Add "type": "module" to package.json to enable ESM by default - Add migrations/package.json with "type": "commonjs" to keep db-migrate compatible - Convert eslint.config.js to ESM with sourceType: "module" - Replace __dirname/__filename with import.meta.dirname/import.meta.filename - Replace require.main === module with process.argv[1] === import.meta.filename - Remove 'use strict' directives (implicit in ESM) - Convert dynamic require() in switch statements to static import lookup maps (dns.js, domains.js, backupformats.js, backupsites.js, network.js) - Extract self-referencing exports.CONSTANT patterns into standalone const declarations (apps.js, services.js, locks.js, users.js, mail.js, etc.) - Lazify SERVICES object in services.js to avoid circular dependency TDZ issues - Add clearMailQueue() to mailer.js for ESM-safe queue clearing in tests - Add _setMockApp() to ldapserver.js for ESM-safe test mocking - Add _setMockResolve() wrapper to dig.js for ESM-safe DNS mocking in tests - Convert backupupload.js to use dynamic imports so --check exits before loading the module graph (which requires BOX_ENV) - Update check-install to use ESM import for infra_version.js - Convert scripts/ (hotfix, release, remote_hotfix.js, find-unused-translations) - All 1315 tests passing Migration stats (AI-assisted using Cursor with Claude): - Wall clock time: ~3-4 hours - Assistant completions: ~80-100 - Estimated token usage: ~1-2M tokens Co-authored-by: Cursor <cursoragent@cursor.com>
132 lines
4.1 KiB
JavaScript
Executable File
132 lines
4.1 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import superagent from '@cloudron/superagent';
|
|
import translations from '../dashboard/public/translation/en.json';
|
|
|
|
// get token from https://translate.cloudron.io/accounts/profile/#api
|
|
const token = '';
|
|
|
|
const flat = flattenObject(translations);
|
|
const keys = Object.keys(flat);
|
|
|
|
function flattenObject(obj, prefix = '', result = {}) {
|
|
for (const key in obj) {
|
|
if (obj.hasOwnProperty(key)) {
|
|
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
|
|
if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
|
|
// Rekursiver Aufruf für verschachtelte Objekte
|
|
flattenObject(obj[key], newKey, result);
|
|
} else {
|
|
// Wert direkt zuweisen
|
|
result[newKey] = obj[key];
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
async function listFiles(dir) {
|
|
const files = await fs.promises.readdir(dir);
|
|
let result = [];
|
|
|
|
for (const file of files) {
|
|
const fullPath = path.join(dir, file);
|
|
const stat = await fs.promises.stat(fullPath);
|
|
|
|
if (stat.isDirectory()) {
|
|
result = result.concat(await listFiles(fullPath));
|
|
} else if (stat.isFile()) {
|
|
result.push(fullPath);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
async function grepFile(fullPath, searchString) {
|
|
const content = await fs.promises.readFile(fullPath, 'utf-8');
|
|
return content.includes(searchString);
|
|
}
|
|
|
|
async function findDuplicates() {
|
|
const duplicates = [];
|
|
for (const key of keys) {
|
|
const value = flat[key];
|
|
const dups = keys.filter(k => flat[k] === value && k !== key);
|
|
if (dups.length === 0) continue;
|
|
|
|
// skip double entries
|
|
if (duplicates.find(d => d.value === value)) continue;
|
|
|
|
duplicates.push({ key, value, dups });
|
|
}
|
|
|
|
console.log('Keys with the same values multiple times:');
|
|
console.log('----------------------------------------------------------');
|
|
duplicates.forEach(d => console.log(`${d.key}\n\t${d.dups.join(' \n\t')} \n => "${d.value}"\n `));
|
|
console.log('----------------------------------------------------------');
|
|
console.log(' Count: ' + duplicates.length);
|
|
console.log('----------------------------------------------------------\n\n');
|
|
}
|
|
|
|
async function findUnused() {
|
|
const keyUsage = {};
|
|
let files = await listFiles(path.resolve('./dashboard/src/'));
|
|
files = files.concat(await listFiles(path.resolve('./src/')));
|
|
|
|
for (const file of files) {
|
|
const content = await fs.promises.readFile(file, 'utf-8');
|
|
|
|
for (const key of keys) {
|
|
if (!keyUsage[key]) keyUsage[key] = 0;
|
|
if (content.includes(key)) keyUsage[key]++;
|
|
}
|
|
}
|
|
const unusedKeys = Object.keys(keyUsage).filter(k => keyUsage[k] === 0)
|
|
|
|
console.log('Unused keys:');
|
|
console.log('----------------------------------------------------------');
|
|
console.log(unusedKeys.join('\n'));
|
|
console.log('----------------------------------------------------------');
|
|
console.log(' Count: ' + unusedKeys.length);
|
|
console.log('----------------------------------------------------------\n\n');
|
|
|
|
return unusedKeys;
|
|
}
|
|
|
|
async function purge(key) {
|
|
let result = await superagent.get('https://translate.cloudron.io/api/units/')
|
|
.set('Authorization', `Token ${token}`)
|
|
.query({q: `language:en AND component:dashboard AND key:${key}`});
|
|
|
|
const id = result.body.results[0].id;
|
|
|
|
console.log(` => deleting ${key} with id ${id}`);
|
|
await superagent.del(`https://translate.cloudron.io/api/units/${id}/`)
|
|
.set('Authorization', `Token ${token}`);
|
|
|
|
console.log(' done');
|
|
}
|
|
|
|
(async () => {
|
|
|
|
console.log(`Found ${keys.length} strings`);
|
|
console.log('----------------------------------------------------------\n\n');
|
|
|
|
console.log('Toplevel keys to be moved:');
|
|
console.log('----------------------------------------------------------');
|
|
console.log(Object.keys(flat).find(k => k.indexOf('.') === -1));
|
|
console.log('----------------------------------------------------------\n\n');
|
|
|
|
await findDuplicates();
|
|
// const unused = await findUnused();
|
|
|
|
// for (const key of unused) {
|
|
// await purge(key);
|
|
// }
|
|
|
|
})();
|