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:
@@ -1,22 +1,22 @@
|
||||
'use strict';
|
||||
import apps from '../../apps.js';
|
||||
import * as appstore from '../../appstore.js';
|
||||
import * as backupSites from '../../backupsites.js';
|
||||
import debugModule from 'debug';
|
||||
import constants from '../../constants.js';
|
||||
import * as database from '../../database.js';
|
||||
import expect from 'expect.js';
|
||||
import * as mailer from '../../mailer.js';
|
||||
import nock from 'nock';
|
||||
import oidcClients from '../../oidcclients.js';
|
||||
import * as oidcServer from '../../oidcserver.js';
|
||||
import * as server from '../../server.js';
|
||||
import * as settings from '../../settings.js';
|
||||
import superagent from '@cloudron/superagent';
|
||||
import tasks from '../../tasks.js';
|
||||
import timers from 'timers/promises';
|
||||
import * as tokens from '../../tokens.js';
|
||||
|
||||
const apps = require('../../apps.js'),
|
||||
appstore = require('../../appstore.js'),
|
||||
backupSites = require('../../backupsites.js'),
|
||||
debug = require('debug')('box:test/common'),
|
||||
constants = require('../../constants.js'),
|
||||
database = require('../../database.js'),
|
||||
expect = require('expect.js'),
|
||||
mailer = require('../../mailer.js'),
|
||||
nock = require('nock'),
|
||||
oidcClients = require('../../oidcclients.js'),
|
||||
oidcServer = require('../../oidcserver.js'),
|
||||
server = require('../../server.js'),
|
||||
settings = require('../../settings.js'),
|
||||
superagent = require('@cloudron/superagent'),
|
||||
tasks = require('../../tasks.js'),
|
||||
timers = require('timers/promises'),
|
||||
tokens = require('../../tokens.js');
|
||||
const debug = debugModule('box:test/common');
|
||||
|
||||
const manifest = {
|
||||
'id': 'io.cloudron.test',
|
||||
@@ -46,7 +46,57 @@ const manifest = {
|
||||
}
|
||||
};
|
||||
|
||||
exports = module.exports = {
|
||||
const mockApiServerOrigin = 'http://localhost:6060';
|
||||
const dashboardDomain = 'test.example.com';
|
||||
const appstoreToken = 'toktok';
|
||||
|
||||
const owner = {
|
||||
id: null,
|
||||
username: 'superadmin',
|
||||
password: 'Foobar?1337',
|
||||
email: 'superadmin@cloudron.local',
|
||||
displayName: 'Super Admin',
|
||||
token: null
|
||||
};
|
||||
|
||||
const admin = {
|
||||
id: null,
|
||||
username: 'administrator',
|
||||
password: 'Foobar?1339',
|
||||
email: 'admin@cloudron.local',
|
||||
token: null
|
||||
};
|
||||
|
||||
const user = {
|
||||
id: null,
|
||||
username: 'user',
|
||||
password: 'Foobar?1338',
|
||||
email: 'user@cloudron.local',
|
||||
token: null
|
||||
};
|
||||
|
||||
const app = {
|
||||
id: 'appid',
|
||||
appStoreId: 'appStoreId',
|
||||
installationState: apps.ISTATE_PENDING_INSTALL,
|
||||
runState: 'running',
|
||||
subdomain: 'app',
|
||||
domain: 'test.example.com',
|
||||
fqdn: 'app.test.example.com',
|
||||
manifest,
|
||||
containerId: 'someid',
|
||||
portBindings: {},
|
||||
accessRestriction: null,
|
||||
memoryLimit: 0,
|
||||
mailboxDomain: 'test.example.com',
|
||||
secondaryDomains: [],
|
||||
redirectDomains: [],
|
||||
aliasDomains: []
|
||||
};
|
||||
|
||||
const serverUrl = `http://localhost:${constants.PORT}`;
|
||||
|
||||
export default {
|
||||
setup,
|
||||
setupServer,
|
||||
cleanup,
|
||||
@@ -54,61 +104,17 @@ exports = module.exports = {
|
||||
checkMails,
|
||||
waitForTask,
|
||||
waitForAsyncTask,
|
||||
|
||||
owner: {
|
||||
id: null,
|
||||
username: 'superadmin',
|
||||
password: 'Foobar?1337',
|
||||
email: 'superadmin@cloudron.local',
|
||||
displayName: 'Super Admin',
|
||||
token: null
|
||||
},
|
||||
|
||||
admin: {
|
||||
id: null,
|
||||
username: 'administrator',
|
||||
password: 'Foobar?1339',
|
||||
email: 'admin@cloudron.local',
|
||||
token: null
|
||||
},
|
||||
|
||||
user: {
|
||||
id: null,
|
||||
username: 'user',
|
||||
password: 'Foobar?1338',
|
||||
email: 'user@cloudron.local',
|
||||
token: null
|
||||
},
|
||||
|
||||
app: {
|
||||
id: 'appid',
|
||||
appStoreId: 'appStoreId',
|
||||
installationState: apps.ISTATE_PENDING_INSTALL,
|
||||
runState: 'running',
|
||||
subdomain: 'app',
|
||||
domain: 'test.example.com',
|
||||
fqdn: 'app.test.example.com',
|
||||
manifest,
|
||||
containerId: 'someid',
|
||||
portBindings: {},
|
||||
accessRestriction: null,
|
||||
memoryLimit: 0,
|
||||
mailboxDomain: 'test.example.com',
|
||||
secondaryDomains: [],
|
||||
redirectDomains: [],
|
||||
aliasDomains: []
|
||||
},
|
||||
|
||||
owner,
|
||||
admin,
|
||||
user,
|
||||
app,
|
||||
getDefaultBackupSite,
|
||||
|
||||
mockApiServerOrigin: 'http://localhost:6060',
|
||||
dashboardDomain: 'test.example.com',
|
||||
mockApiServerOrigin,
|
||||
dashboardDomain,
|
||||
dashboardFqdn: 'my.test.example.com',
|
||||
appstoreToken: 'toktok',
|
||||
appstoreToken,
|
||||
mailFqdn: 'my.test.example.com',
|
||||
|
||||
serverUrl: `http://localhost:${constants.PORT}`,
|
||||
|
||||
serverUrl,
|
||||
auditSource: { ip: '5.6.7.8' }
|
||||
};
|
||||
|
||||
@@ -116,7 +122,7 @@ async function setupServer() {
|
||||
debug('Setting up server');
|
||||
await database.initialize();
|
||||
await database._clear();
|
||||
await appstore._setApiServerOrigin(exports.mockApiServerOrigin);
|
||||
await appstore._setApiServerOrigin(mockApiServerOrigin);
|
||||
await oidcServer.stop();
|
||||
await server.start();
|
||||
debug('Set up server complete');
|
||||
@@ -125,13 +131,11 @@ async function setupServer() {
|
||||
async function setup() {
|
||||
debug('Setting up');
|
||||
|
||||
const owner = exports.owner, serverUrl = exports.serverUrl, user = exports.user, admin = exports.admin;
|
||||
|
||||
await setupServer();
|
||||
|
||||
// setup
|
||||
let response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
|
||||
.send({ domainConfig: { provider: 'noop', domain: exports.dashboardDomain, config: {}, tlsConfig: { provider: 'fallback' } } });
|
||||
.send({ domainConfig: { provider: 'noop', domain: dashboardDomain, config: {}, tlsConfig: { provider: 'fallback' } } });
|
||||
expect(response.status).to.eql(200);
|
||||
|
||||
await timers.setTimeout(2000);
|
||||
@@ -178,9 +182,9 @@ async function setup() {
|
||||
user.token = token2.accessToken;
|
||||
|
||||
// create app object
|
||||
await apps.add(exports.app.id, exports.app.appStoreId, '', exports.app.manifest, exports.app.subdomain, exports.app.domain, exports.app.portBindings, exports.app);
|
||||
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
|
||||
|
||||
debug('Setup complete');
|
||||
}
|
||||
@@ -194,7 +198,7 @@ async function cleanup() {
|
||||
}
|
||||
|
||||
function clearMailQueue() {
|
||||
mailer._mailQueue = [];
|
||||
mailer.clearMailQueue();
|
||||
}
|
||||
|
||||
async function checkMails(number) {
|
||||
|
||||
Reference in New Issue
Block a user