Files
cloudron-box/scripts/cjs-to-esm/revert-and-fix.mjs
Girish Ramakrishnan 96dc79cfe6 Migrate codebase from CommonJS to ES Modules
- 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>
2026-02-14 15:11:45 +01:00

61 lines
2.3 KiB
JavaScript

#!/usr/bin/env node
// Revert the broken extraction from fix-remaining-exports.mjs
// and instead use a proper approach:
// 1. Keep export default { ... } for files with computed properties
// 2. Change all `import X from './local.js'` for those files to
// `import * as _X from './local.js'; const X = _X.default;`
// Actually that's ugly. Better approach:
// Just lazify the top-level constant evaluation in services.js
// OR use a re-export pattern.
//
// Actually, the simplest approach: restore the 34 files from git,
// then re-run the first two conversion scripts on them,
// then for files with complex exports, add a thin named-export wrapper.
import fs from 'node:fs';
import path from 'node:path';
import { execSync } from 'node:child_process';
const ROOT = process.cwd();
// Find files that still have export default but with broken syntax
const files = execSync('git diff --name-only', { cwd: ROOT, encoding: 'utf-8' })
.trim().split('\n')
.filter(f => f.startsWith('src/') || f === 'syslog.js');
// Check which files have syntax errors
const brokenFiles = [];
for (const file of files) {
try {
execSync(`node --check ${file}`, { cwd: ROOT, stdio: 'pipe' });
} catch {
brokenFiles.push(file);
}
}
console.log(`Found ${brokenFiles.length} files with syntax errors:`);
brokenFiles.forEach(f => console.log(` ${f}`));
// Restore these files from git
for (const file of brokenFiles) {
execSync(`git checkout -- ${file}`, { cwd: ROOT });
console.log(`Restored: ${file}`);
}
// Now re-run the initial CJS→ESM conversion on just these files
console.log('\nRe-running CJS→ESM conversion on restored files...');
const fileArgs = brokenFiles.join(' ');
execSync(`node scripts/cjs-to-esm.mjs ${fileArgs}`, { cwd: ROOT, stdio: 'inherit' });
// Now re-run the first export fix (shorthand-only) on these files
// But we need a targeted approach: for each file, check if its exports
// are shorthand-only, and if so convert to named exports.
// Files with computed properties will keep export default.
console.log('\nRe-running export fixes...');
execSync('node scripts/fix-exports.mjs', { cwd: ROOT, stdio: 'inherit' });
console.log('\nDone. Files with complex exports will keep export default.');
console.log('Circular dependency issues will be fixed by lazifying top-level access.');