Merge pull request #21154 from overleaf/msm-esm-ce-scrits

[web] Migrate `modules/server-ce-scripts` to ESM

GitOrigin-RevId: 8856f83b12d5d092e51e5765763737259c4b3985
This commit is contained in:
Miguel Serrano 2024-10-21 11:56:43 +02:00 committed by Copybot
parent c7eafd69c2
commit c4edd2fffa
12 changed files with 83 additions and 68 deletions

View file

@ -3,7 +3,7 @@ const tags = ['server-ce', 'server-pro']
const migrate = async () => { const migrate = async () => {
// Run-time import as SaaS does not ship with the server-ce-scripts module // Run-time import as SaaS does not ship with the server-ce-scripts module
const { default: runScript } = await import( const { default: runScript } = await import(
'../modules/server-ce-scripts/scripts/upgrade-user-features.js' '../modules/server-ce-scripts/scripts/upgrade-user-features.mjs'
) )
await runScript(false, { await runScript(false, {
gitBridge: 1, gitBridge: 1,

View file

@ -3,4 +3,4 @@
/** @type {WebModule} */ /** @type {WebModule} */
const ServerCeScriptsModule = {} const ServerCeScriptsModule = {}
module.exports = ServerCeScriptsModule export default ServerCeScriptsModule

View file

@ -1,9 +1,9 @@
const minimist = require('minimist') import minimist from 'minimist'
const { import {
db, db,
ObjectId, ObjectId,
waitForDb, waitForDb,
} = require('../../../app/src/infrastructure/mongodb') } from '../../../app/src/infrastructure/mongodb.js'
async function main() { async function main() {
await waitForDb() await waitForDb()

View file

@ -1,6 +1,9 @@
const { ObjectId } = require('mongodb-legacy') import mongodb from 'mongodb-legacy'
const { waitForDb, db } = require('../../../app/src/infrastructure/mongodb') import { waitForDb, db } from '../../../app/src/infrastructure/mongodb.js'
const { getMongoClient } = require('../../../app/src/infrastructure/Mongoose') import mongoose from '../../../app/src/infrastructure/Mongoose.js'
const { ObjectId } = mongodb
const { getMongoClient } = mongoose
const MIN_MONGO_VERSION = [5, 0] const MIN_MONGO_VERSION = [5, 0]

View file

@ -1,4 +1,4 @@
const RedisWrapper = require('../../../app/src/infrastructure/RedisWrapper') import RedisWrapper from '../../../app/src/infrastructure/RedisWrapper.js'
const rclient = RedisWrapper.client('health_check') const rclient = RedisWrapper.client('health_check')
rclient.on('error', err => { rclient.on('error', err => {
console.error('Cannot connect to redis.') console.error('Cannot connect to redis.')

View file

@ -1,4 +1,4 @@
const { waitForDb, db } = require('../../../app/src/infrastructure/mongodb') import { waitForDb, db } from '../../../app/src/infrastructure/mongodb.js'
async function readImagesInUse() { async function readImagesInUse() {
await waitForDb() await waitForDb()

View file

@ -1,6 +1,6 @@
const minimist = require('minimist') import minimist from 'minimist'
const { db, waitForDb } = require('../../../app/src/infrastructure/mongodb') import { db, waitForDb } from '../../../app/src/infrastructure/mongodb.js'
const UserRegistrationHandler = require('../../../app/src/Features/User/UserRegistrationHandler') import UserRegistrationHandler from '../../../app/src/Features/User/UserRegistrationHandler.js'
async function main() { async function main() {
await waitForDb() await waitForDb()

View file

@ -1,6 +1,6 @@
const { waitForDb } = require('../../../app/src/infrastructure/mongodb') import { waitForDb } from '../../../app/src/infrastructure/mongodb.js'
const UserGetter = require('../../../app/src/Features/User/UserGetter') import UserGetter from '../../../app/src/Features/User/UserGetter.js'
const UserDeleter = require('../../../app/src/Features/User/UserDeleter') import UserDeleter from '../../../app/src/Features/User/UserDeleter.js'
async function main() { async function main() {
await waitForDb() await waitForDb()

View file

@ -9,16 +9,16 @@
// Intended for Server Pro customers migrating user emails from one domain to // Intended for Server Pro customers migrating user emails from one domain to
// another. // another.
const minimist = require('minimist') import minimist from 'minimist'
const { waitForDb } = require('../../../app/src/infrastructure/mongodb')
const os = require('os') import { waitForDb } from '../../../app/src/infrastructure/mongodb.js'
const fs = require('fs') import os from 'node:os'
const csv = require('csv/sync') import fs from 'node:fs'
const { parseEmail } = require('../../../app/src/Features/Helpers/EmailHelper') import * as csv from 'csv/sync'
const UserGetter = require('../../../app/src/Features/User/UserGetter') import { parseEmail } from '../../../app/src/Features/Helpers/EmailHelper.js'
const UserUpdater = require('../../../app/src/Features/User/UserUpdater') import UserGetter from '../../../app/src/Features/User/UserGetter.js'
const UserSessionsManager = require('../../../app/src/Features/User/UserSessionsManager') import UserUpdater from '../../../app/src/Features/User/UserUpdater.js'
import UserSessionsManager from '../../../app/src/Features/User/UserSessionsManager.js'
const hostname = os.hostname() const hostname = os.hostname()
const scriptTimestamp = new Date().toISOString() const scriptTimestamp = new Date().toISOString()

View file

@ -1,5 +1,5 @@
const minimist = require('minimist') import minimist from 'minimist'
const { db, waitForDb } = require('../../../app/src/infrastructure/mongodb') import { db, waitForDb } from '../../../app/src/infrastructure/mongodb.js'
async function main() { async function main() {
await waitForDb() await waitForDb()

View file

@ -1,10 +1,11 @@
const Settings = require('@overleaf/settings') import { fileURLToPath } from 'node:url'
const logger = require('@overleaf/logger') import Settings from '@overleaf/settings'
const { db, waitForDb } = require('../../../app/src/infrastructure/mongodb') import logger from '@overleaf/logger'
const { import { db, waitForDb } from '../../../app/src/infrastructure/mongodb.js'
import {
mergeFeatures, mergeFeatures,
compareFeatures, compareFeatures,
} = require('../../../app/src/Features/Subscription/FeaturesHelper') } from '../../../app/src/Features/Subscription/FeaturesHelper.js'
const DRY_RUN = !process.argv.includes('--dry-run=false') const DRY_RUN = !process.argv.includes('--dry-run=false')
async function main(DRY_RUN, defaultFeatures) { async function main(DRY_RUN, defaultFeatures) {
@ -40,9 +41,9 @@ async function main(DRY_RUN, defaultFeatures) {
} }
} }
module.exports = main export default main
if (require.main === module) { if (fileURLToPath(import.meta.url) === process.argv[1]) {
if (DRY_RUN) { if (DRY_RUN) {
console.error('---') console.error('---')
console.error('Dry-run enabled, use --dry-run=false to commit changes') console.error('Dry-run enabled, use --dry-run=false to commit changes')

View file

@ -1,9 +1,11 @@
const { execSync } = require('child_process') import { execSync } from 'node:child_process'
const fs = require('fs') import fs from 'node:fs'
const Settings = require('@overleaf/settings') import Settings from '@overleaf/settings'
const { expect } = require('chai') import { expect } from 'chai'
const { db } = require('../../../../../app/src/infrastructure/mongodb') import { db } from '../../../../../app/src/infrastructure/mongodb.js'
const User = require('../../../../../test/acceptance/src/helpers/User').promises import UserHelper from '../../../../../test/acceptance/src/helpers/User.js'
const { promises: User } = UserHelper
/** /**
* @param {string} cmd * @param {string} cmd
@ -42,7 +44,7 @@ async function getUser(email) {
describe('ServerCEScripts', function () { describe('ServerCEScripts', function () {
describe('check-mongodb', function () { describe('check-mongodb', function () {
it('should exit with code 0 on success', function () { it('should exit with code 0 on success', function () {
run('node modules/server-ce-scripts/scripts/check-mongodb') run('node modules/server-ce-scripts/scripts/check-mongodb.mjs')
}) })
it('should exit with code 1 on error', function () { it('should exit with code 1 on error', function () {
@ -50,7 +52,7 @@ describe('ServerCEScripts', function () {
run( run(
'MONGO_SERVER_SELECTION_TIMEOUT=1' + 'MONGO_SERVER_SELECTION_TIMEOUT=1' +
'MONGO_CONNECTION_STRING=mongodb://127.0.0.1:4242 ' + 'MONGO_CONNECTION_STRING=mongodb://127.0.0.1:4242 ' +
'node modules/server-ce-scripts/scripts/check-mongodb' 'node modules/server-ce-scripts/scripts/check-mongodb.mjs'
) )
} catch (e) { } catch (e) {
expect(e.status).to.equal(1) expect(e.status).to.equal(1)
@ -62,12 +64,14 @@ describe('ServerCEScripts', function () {
describe('check-redis', function () { describe('check-redis', function () {
it('should exit with code 0 on success', function () { it('should exit with code 0 on success', function () {
run('node modules/server-ce-scripts/scripts/check-redis') run('node modules/server-ce-scripts/scripts/check-redis.mjs')
}) })
it('should exit with code 1 on error', function () { it('should exit with code 1 on error', function () {
try { try {
run('REDIS_PORT=42 node modules/server-ce-scripts/scripts/check-redis') run(
'REDIS_PORT=42 node modules/server-ce-scripts/scripts/check-redis.mjs'
)
} catch (e) { } catch (e) {
expect(e.status).to.equal(1) expect(e.status).to.equal(1)
return return
@ -79,28 +83,28 @@ describe('ServerCEScripts', function () {
describe('create-user', function () { describe('create-user', function () {
it('should exit with code 0 on success', function () { it('should exit with code 0 on success', function () {
const out = run( const out = run(
'node modules/server-ce-scripts/scripts/create-user --email=foo@bar.com' 'node modules/server-ce-scripts/scripts/create-user.mjs --email=foo@bar.com'
) )
expect(out).to.include('/user/activate?token=') expect(out).to.include('/user/activate?token=')
}) })
it('should create a regular user by default', async function () { it('should create a regular user by default', async function () {
run( run(
'node modules/server-ce-scripts/scripts/create-user --email=foo@bar.com' 'node modules/server-ce-scripts/scripts/create-user.mjs --email=foo@bar.com'
) )
expect(await getUser('foo@bar.com')).to.deep.equal({ isAdmin: false }) expect(await getUser('foo@bar.com')).to.deep.equal({ isAdmin: false })
}) })
it('should create an admin user with --admin flag', async function () { it('should create an admin user with --admin flag', async function () {
run( run(
'node modules/server-ce-scripts/scripts/create-user --admin --email=foo@bar.com' 'node modules/server-ce-scripts/scripts/create-user.mjs --admin --email=foo@bar.com'
) )
expect(await getUser('foo@bar.com')).to.deep.equal({ isAdmin: true }) expect(await getUser('foo@bar.com')).to.deep.equal({ isAdmin: true })
}) })
it('should exit with code 1 on missing email', function () { it('should exit with code 1 on missing email', function () {
try { try {
run('node modules/server-ce-scripts/scripts/create-user') run('node modules/server-ce-scripts/scripts/create-user.mjs')
} catch (e) { } catch (e) {
expect(e.status).to.equal(1) expect(e.status).to.equal(1)
return return
@ -119,19 +123,26 @@ describe('ServerCEScripts', function () {
it('should log missing user', function () { it('should log missing user', function () {
const email = 'does-not-exist@example.com' const email = 'does-not-exist@example.com'
const out = run( const out = run(
'node modules/server-ce-scripts/scripts/delete-user --email=' + email 'node modules/server-ce-scripts/scripts/delete-user.mjs --email=' +
email
) )
expect(out).to.include('not in database, potentially already deleted') expect(out).to.include('not in database, potentially already deleted')
}) })
it('should exit with code 0 on success', function () { it('should exit with code 0 on success', function () {
const email = user.email const email = user.email
run('node modules/server-ce-scripts/scripts/delete-user --email=' + email) run(
'node modules/server-ce-scripts/scripts/delete-user.mjs --email=' +
email
)
}) })
it('should have deleted the user on success', async function () { it('should have deleted the user on success', async function () {
const email = user.email const email = user.email
run('node modules/server-ce-scripts/scripts/delete-user --email=' + email) run(
'node modules/server-ce-scripts/scripts/delete-user.mjs --email=' +
email
)
const dbEntry = await user.get() const dbEntry = await user.get()
expect(dbEntry).to.not.exist expect(dbEntry).to.not.exist
const softDeletedEntry = await db.deletedUsers.findOne({ const softDeletedEntry = await db.deletedUsers.findOne({
@ -143,7 +154,7 @@ describe('ServerCEScripts', function () {
it('should exit with code 1 on missing email', function () { it('should exit with code 1 on missing email', function () {
try { try {
run('node modules/server-ce-scripts/scripts/delete-user') run('node modules/server-ce-scripts/scripts/delete-user.mjs')
} catch (e) { } catch (e) {
expect(e.status).to.equal(1) expect(e.status).to.equal(1)
return return
@ -199,7 +210,7 @@ describe('ServerCEScripts', function () {
it('should do a dry run by default', async function () { it('should do a dry run by default', async function () {
run( run(
`node modules/server-ce-scripts/scripts/migrate-user-emails.js ${csv}` `node modules/server-ce-scripts/scripts/migrate-user-emails.mjs ${csv}`
) )
for (const user of usersToMigrate) { for (const user of usersToMigrate) {
const dbEntry = await user.get() const dbEntry = await user.get()
@ -213,13 +224,13 @@ describe('ServerCEScripts', function () {
it('should exit with code 0 when successfully migrating user emails', function () { it('should exit with code 0 when successfully migrating user emails', function () {
run( run(
`node modules/server-ce-scripts/scripts/migrate-user-emails.js --commit ${csv}` `node modules/server-ce-scripts/scripts/migrate-user-emails.mjs --commit ${csv}`
) )
}) })
it('should migrate the user emails with the --commit option', async function () { it('should migrate the user emails with the --commit option', async function () {
run( run(
`node modules/server-ce-scripts/scripts/migrate-user-emails.js --commit ${csv}` `node modules/server-ce-scripts/scripts/migrate-user-emails.mjs --commit ${csv}`
) )
for (const user of usersToMigrate) { for (const user of usersToMigrate) {
const dbEntry = await user.get() const dbEntry = await user.get()
@ -233,7 +244,7 @@ describe('ServerCEScripts', function () {
it('should leave other user emails unchanged', async function () { it('should leave other user emails unchanged', async function () {
run( run(
`node modules/server-ce-scripts/scripts/migrate-user-emails.js --commit ${csv}` `node modules/server-ce-scripts/scripts/migrate-user-emails.mjs --commit ${csv}`
) )
for (const user of otherUsers) { for (const user of otherUsers) {
const dbEntry = await user.get() const dbEntry = await user.get()
@ -244,7 +255,7 @@ describe('ServerCEScripts', function () {
it('should exit with code 1 when there are failures migrating user emails', function () { it('should exit with code 1 when there are failures migrating user emails', function () {
try { try {
run( run(
`node modules/server-ce-scripts/scripts/migrate-user-emails.js --commit ${csvfail}` `node modules/server-ce-scripts/scripts/migrate-user-emails.mjs --commit ${csvfail}`
) )
} catch (e) { } catch (e) {
expect(e.status).to.equal(1) expect(e.status).to.equal(1)
@ -256,12 +267,12 @@ describe('ServerCEScripts', function () {
it('should migrate other users when there are failures with the --continue option', async function () { it('should migrate other users when there are failures with the --continue option', async function () {
try { try {
run( run(
`node modules/server-ce-scripts/scripts/migrate-user-emails.js --commit ${csvfail}` `node modules/server-ce-scripts/scripts/migrate-user-emails.mjs --commit ${csvfail}`
) )
} catch (e) { } catch (e) {
expect(e.status).to.equal(1) expect(e.status).to.equal(1)
run( run(
`node modules/server-ce-scripts/scripts/migrate-user-emails.js --commit --continue ${csvfail}` `node modules/server-ce-scripts/scripts/migrate-user-emails.mjs --commit --continue ${csvfail}`
) )
for (const user of usersToMigrate) { for (const user of usersToMigrate) {
const dbEntry = await user.get() const dbEntry = await user.get()
@ -301,7 +312,7 @@ describe('ServerCEScripts', function () {
expect(await getTagNames()).to.deep.equal([oldName]) expect(await getTagNames()).to.deep.equal([oldName])
run( run(
`node modules/server-ce-scripts/scripts/rename-tag --user-id=${user.id} --old-name=${oldName} --new-name=${newName}` `node modules/server-ce-scripts/scripts/rename-tag.mjs --user-id=${user.id} --old-name=${oldName} --new-name=${newName}`
) )
expect(await getTagNames()).to.deep.equal([newName]) expect(await getTagNames()).to.deep.equal([newName])
@ -334,7 +345,7 @@ describe('ServerCEScripts', function () {
beforeEach('run script on user a', function () { beforeEach('run script on user a', function () {
newUserATimeout = userATimeout - 1 newUserATimeout = userATimeout - 1
run( run(
`node modules/server-ce-scripts/scripts/change-compile-timeout --user-id=${userA.id} --compile-timeout=${newUserATimeout}` `node modules/server-ce-scripts/scripts/change-compile-timeout.mjs --user-id=${userA.id} --compile-timeout=${newUserATimeout}`
) )
}) })
@ -353,7 +364,7 @@ describe('ServerCEScripts', function () {
it('should reject zero timeout', async function () { it('should reject zero timeout', async function () {
try { try {
run( run(
`node modules/server-ce-scripts/scripts/change-compile-timeout --user-id=${userA.id} --compile-timeout=0` `node modules/server-ce-scripts/scripts/change-compile-timeout.mjs --user-id=${userA.id} --compile-timeout=0`
) )
expect.fail('should error out') expect.fail('should error out')
} catch (err) { } catch (err) {
@ -366,7 +377,7 @@ describe('ServerCEScripts', function () {
it('should reject a 20min timeout', async function () { it('should reject a 20min timeout', async function () {
try { try {
run( run(
`node modules/server-ce-scripts/scripts/change-compile-timeout --user-id=${userA.id} --compile-timeout=1200` `node modules/server-ce-scripts/scripts/change-compile-timeout.mjs --user-id=${userA.id} --compile-timeout=1200`
) )
expect.fail('should error out') expect.fail('should error out')
} catch (err) { } catch (err) {
@ -410,13 +421,13 @@ describe('ServerCEScripts', function () {
beforeEach('downgrade userCustomTimeoutLower', async function () { beforeEach('downgrade userCustomTimeoutLower', async function () {
run( run(
`node modules/server-ce-scripts/scripts/change-compile-timeout --user-id=${userCustomTimeoutLower.id} --compile-timeout=42` `node modules/server-ce-scripts/scripts/change-compile-timeout.mjs --user-id=${userCustomTimeoutLower.id} --compile-timeout=42`
) )
}) })
beforeEach('upgrade userCustomTimeoutHigher', async function () { beforeEach('upgrade userCustomTimeoutHigher', async function () {
run( run(
`node modules/server-ce-scripts/scripts/change-compile-timeout --user-id=${userCustomTimeoutHigher.id} --compile-timeout=360` `node modules/server-ce-scripts/scripts/change-compile-timeout.mjs --user-id=${userCustomTimeoutHigher.id} --compile-timeout=360`
) )
}) })
@ -451,7 +462,7 @@ describe('ServerCEScripts', function () {
let output let output
beforeEach('run script', function () { beforeEach('run script', function () {
output = run( output = run(
`node modules/server-ce-scripts/scripts/upgrade-user-features` `node modules/server-ce-scripts/scripts/upgrade-user-features.mjs`
) )
}) })
@ -480,7 +491,7 @@ describe('ServerCEScripts', function () {
let output let output
beforeEach('run script', function () { beforeEach('run script', function () {
output = run( output = run(
`node modules/server-ce-scripts/scripts/upgrade-user-features --dry-run=false` `node modules/server-ce-scripts/scripts/upgrade-user-features.mjs --dry-run=false`
) )
}) })
@ -537,7 +548,7 @@ describe('ServerCEScripts', function () {
cmd += ` OVERLEAF_IS_SERVER_PRO=${OVERLEAF_IS_SERVER_PRO}` cmd += ` OVERLEAF_IS_SERVER_PRO=${OVERLEAF_IS_SERVER_PRO}`
} }
return ( return (
cmd + ' node modules/server-ce-scripts/scripts/check-texlive-images' cmd + ' node modules/server-ce-scripts/scripts/check-texlive-images.mjs'
) )
} }