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>
This commit is contained in:
+38
-38
@@ -1,24 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
const apps = require('../apps.js'),
|
||||
appstore = require('../appstore.js'),
|
||||
backupSites = require('../backupsites.js'),
|
||||
constants = require('../constants.js'),
|
||||
cron = require('../cron.js'),
|
||||
dashboard = require('../dashboard.js'),
|
||||
database = require('../database.js'),
|
||||
domains = require('../domains.js'),
|
||||
expect = require('expect.js'),
|
||||
fs = require('node:fs'),
|
||||
locks = require('../locks.js'),
|
||||
mailer = require('../mailer.js'),
|
||||
mailServer = require('../mailserver.js'),
|
||||
nock = require('nock'),
|
||||
path = require('node:path'),
|
||||
settings = require('../settings.js'),
|
||||
tasks = require('../tasks.js'),
|
||||
timers = require('timers/promises'),
|
||||
users = require('../users.js');
|
||||
import apps from '../apps.js';
|
||||
import * as appstore from '../appstore.js';
|
||||
import * as backupSites from '../backupsites.js';
|
||||
import constants from '../constants.js';
|
||||
import * as cron from '../cron.js';
|
||||
import * as dashboard from '../dashboard.js';
|
||||
import * as database from '../database.js';
|
||||
import * as domains from '../domains.js';
|
||||
import expect from 'expect.js';
|
||||
import fs from 'node:fs';
|
||||
import locks from '../locks.js';
|
||||
import * as mailer from '../mailer.js';
|
||||
import * as mailServer from '../mailserver.js';
|
||||
import nock from 'nock';
|
||||
import path from 'node:path';
|
||||
import * as settings from '../settings.js';
|
||||
import tasks from '../tasks.js';
|
||||
import timers from 'timers/promises';
|
||||
import * as users from '../users.js';
|
||||
|
||||
const manifest = {
|
||||
'id': 'io.cloudron.test',
|
||||
@@ -164,7 +162,13 @@ const proxyApp = {
|
||||
};
|
||||
Object.freeze(proxyApp);
|
||||
|
||||
exports = module.exports = {
|
||||
const mockApiServerOrigin = 'http://localhost:6060';
|
||||
const dashboardDomain = domain.domain;
|
||||
const dashboardFqdn = `my.${domain.domain}`;
|
||||
const appstoreToken = 'atoken';
|
||||
const serverUrl = `http://localhost:${constants.PORT}`;
|
||||
|
||||
export {
|
||||
createTree,
|
||||
domainSetup,
|
||||
databaseSetup,
|
||||
@@ -172,23 +176,19 @@ exports = module.exports = {
|
||||
cleanup,
|
||||
checkMails,
|
||||
clearMailQueue,
|
||||
|
||||
getDefaultBackupSite,
|
||||
|
||||
mockApiServerOrigin: 'http://localhost:6060',
|
||||
dashboardDomain: domain.domain,
|
||||
dashboardFqdn: `my.${domain.domain}`,
|
||||
|
||||
mockApiServerOrigin,
|
||||
dashboardDomain,
|
||||
dashboardFqdn,
|
||||
app,
|
||||
proxyApp,
|
||||
admin,
|
||||
auditSource,
|
||||
domain, // the domain object
|
||||
domain, // the domain object,
|
||||
manifest,
|
||||
user,
|
||||
appstoreToken: 'atoken',
|
||||
|
||||
serverUrl: `http://localhost:${constants.PORT}`,
|
||||
appstoreToken,
|
||||
serverUrl,
|
||||
};
|
||||
|
||||
function createTree(root, obj) {
|
||||
@@ -218,8 +218,8 @@ async function databaseSetup() {
|
||||
|
||||
await database.initialize();
|
||||
await database._clear();
|
||||
await appstore._setApiServerOrigin(exports.mockApiServerOrigin);
|
||||
await dashboard._setLocation(constants.DASHBOARD_SUBDOMAIN, exports.dashboardDomain);
|
||||
await appstore._setApiServerOrigin(mockApiServerOrigin);
|
||||
await dashboard._setLocation(constants.DASHBOARD_SUBDOMAIN, dashboardDomain);
|
||||
|
||||
// duplicated here since we clear the database
|
||||
await backupSites.addDefault(auditSource);
|
||||
@@ -238,7 +238,7 @@ async function setup() {
|
||||
const ownerId = await users.createOwner(admin.email, admin.username, admin.password, admin.displayName, auditSource);
|
||||
admin.id = ownerId;
|
||||
await apps.add(app.id, app.appStoreId, '', app.manifest, app.subdomain, app.domain, app.portBindings, app);
|
||||
await settings._set(settings.APPSTORE_API_TOKEN_KEY, exports.appstoreToken); // appstore token
|
||||
await settings._set(settings.APPSTORE_API_TOKEN_KEY, appstoreToken); // appstore token
|
||||
const userId = await users.add(user.email, user, auditSource);
|
||||
user.id = userId;
|
||||
await tasks.stopAllTasks();
|
||||
@@ -247,7 +247,7 @@ async function setup() {
|
||||
|
||||
async function cleanup() {
|
||||
nock.cleanAll();
|
||||
mailer._mailQueue = [];
|
||||
mailer.clearMailQueue();
|
||||
|
||||
await cron.stopJobs();
|
||||
await database._clear();
|
||||
@@ -255,13 +255,13 @@ async function cleanup() {
|
||||
}
|
||||
|
||||
function clearMailQueue() {
|
||||
mailer._mailQueue = [];
|
||||
mailer.clearMailQueue();
|
||||
}
|
||||
|
||||
async function checkMails(number) {
|
||||
await timers.setTimeout(1000);
|
||||
expect(mailer._mailQueue.length).to.equal(number);
|
||||
const emails = mailer._mailQueue;
|
||||
const emails = [...mailer._mailQueue]; // copy before clearing
|
||||
clearMailQueue();
|
||||
|
||||
// return for further investigation
|
||||
|
||||
Reference in New Issue
Block a user