Merge pull request #21202 from overleaf/ls-scripts-to-esm-2

Migrate scripts folder to esm 2/x

GitOrigin-RevId: 1698bc4f13e026fa281d37a4914a2f997849c761
This commit is contained in:
Liangjun Song 2024-10-22 09:21:39 +01:00 committed by Copybot
parent f2ec640b06
commit 902ae750dc
21 changed files with 184 additions and 184 deletions

View file

@ -8,7 +8,7 @@
* or "hello <name>", when User.findOne() finds something. * or "hello <name>", when User.findOne() finds something.
*/ */
import runScript from '../scripts/example/script_for_migration.js' import runScript from '../scripts/example/script_for_migration.mjs'
const tags = [] const tags = []

View file

@ -46,7 +46,7 @@ undo the changes made in `migrate`.
#### Running scripts as a migration #### Running scripts as a migration
To run a script in a migration file, look at `migrations/20190730093801_script_example.js`, which runs the script To run a script in a migration file, look at `migrations/20190730093801_script_example.js`, which runs the script
`scripts/example/script_for_migration.js`. This uses a method where the script can be run standalone via `node`, or `scripts/example/script_for_migration.mjs`. This uses a method where the script can be run standalone via `node`, or
through the migrations' mechanism. through the migrations' mechanism.
### Running migrations ### Running migrations

View file

@ -1,4 +1,4 @@
const GoogleBigQuery = require('@google-cloud/bigquery').BigQuery import { BigQuery as GoogleBigQuery } from '@google-cloud/bigquery'
let dataset = null let dataset = null
@ -25,6 +25,6 @@ async function query(query) {
return rows return rows
} }
module.exports = { export default {
query, query,
} }

View file

@ -1,13 +1,13 @@
const GoogleBigQueryHelper = require('./helpers/GoogleBigQueryHelper') import GoogleBigQueryHelper from './helpers/GoogleBigQueryHelper.mjs'
const { Subscription } = require('../../app/src/models/Subscription') import { Subscription } from '../../app/src/models/Subscription.js'
const { waitForDb } = require('../../app/src/infrastructure/mongodb') import { waitForDb } from '../../app/src/infrastructure/mongodb.js'
const AnalyticsManager = require('../../app/src/Features/Analytics/AnalyticsManager') import AnalyticsManager from '../../app/src/Features/Analytics/AnalyticsManager.js'
const { import { DeletedSubscription } from '../../app/src/models/DeletedSubscription.js'
DeletedSubscription, import minimist from 'minimist'
} = require('../../app/src/models/DeletedSubscription') import _ from 'lodash'
const minimist = require('minimist') import mongodb from 'mongodb-legacy'
const _ = require('lodash')
const { ObjectId } = require('mongodb-legacy') const { ObjectId } = mongodb
let FETCH_LIMIT, COMMIT, VERBOSE let FETCH_LIMIT, COMMIT, VERBOSE
@ -260,12 +260,11 @@ const setup = () => {
} }
setup() setup()
main() try {
.then(() => { await main()
console.error('Done.') console.error('Done.')
process.exit(0) process.exit(0)
}) } catch (error) {
.catch(error => {
console.error({ error }) console.error({ error })
process.exit(1) process.exit(1)
}) }

View file

@ -1,5 +1,5 @@
const { db, waitForDb } = require('../../app/src/infrastructure/mongodb') import { db, waitForDb } from '../../app/src/infrastructure/mongodb.js'
const minimist = require('minimist') import minimist from 'minimist'
const argv = minimist(process.argv.slice(2)) const argv = minimist(process.argv.slice(2))
const commit = argv.commit !== undefined const commit = argv.commit !== undefined

View file

@ -1,16 +1,14 @@
'use strict' import fs from 'fs'
import minimist from 'minimist'
const fs = require('fs') import readline from 'readline'
const minimist = require('minimist') import {
const readline = require('readline')
const {
db, db,
ObjectId, ObjectId,
waitForDb, waitForDb,
} = require('../../app/src/infrastructure/mongodb') } from '../../app/src/infrastructure/mongodb.js'
const DocstoreManager = import DocstoreManagerModule from '../../app/src/Features/Docstore/DocstoreManager.js'
require('../../app/src/Features/Docstore/DocstoreManager').promises
const { promises: DocstoreManager } = DocstoreManagerModule
const argv = minimist(process.argv.slice(2)) const argv = minimist(process.argv.slice(2))
const commit = argv.commit !== undefined const commit = argv.commit !== undefined

View file

@ -6,7 +6,9 @@
* in the migrations directory. * in the migrations directory.
*/ */
const { User } = require('../../app/src/models/User') import { User } from '../../app/src/models/User.js'
import { fileURLToPath } from 'url'
// const somePackage = require('some-package') // const somePackage = require('some-package')
const runScript = async () => { const runScript = async () => {
@ -15,13 +17,14 @@ const runScript = async () => {
console.log(`Hello ${name}!`) console.log(`Hello ${name}!`)
} }
if (require.main === module) { if (fileURLToPath(import.meta.url) === process.argv[1]) {
runScript() try {
.then(() => process.exit()) await runScript()
.catch(err => { process.exit()
console.error(err) } catch (error) {
console.error(error)
process.exit(1) process.exit(1)
}) }
} }
module.exports = runScript export default runScript

View file

@ -1,6 +1,9 @@
// Increase default mongo query timeout from 1min to 1h import { waitForDb, db } from '../../app/src/infrastructure/mongodb.js'
process.env.MONGO_SOCKET_TIMEOUT = process.env.MONGO_SOCKET_TIMEOUT || '360000' import { ensureMongoTimeout } from '../helpers/env_variable_helper.mjs'
const { waitForDb, db } = require('../../app/src/infrastructure/mongodb') // Ensure default mongo query timeout has been increased 1h
if (!process.env.MONGO_SOCKET_TIMEOUT) {
ensureMongoTimeout(360000)
}
async function main() { async function main() {
await checkAllProjectsAreMigrated() await checkAllProjectsAreMigrated()

View file

@ -1,6 +1,6 @@
const HistoryRangesSupportMigration = require('../../app/src/Features/History/HistoryRangesSupportMigration') import HistoryRangesSupportMigration from '../../app/src/Features/History/HistoryRangesSupportMigration.mjs'
const { waitForDb } = require('../../app/src/infrastructure/mongodb') import { waitForDb } from '../../app/src/infrastructure/mongodb.js'
const minimist = require('minimist') import minimist from 'minimist'
async function main() { async function main() {
await waitForDb() await waitForDb()
@ -31,7 +31,7 @@ async function main() {
} }
function usage() { function usage() {
console.error(`Usage: migrate_ranges_support.js [OPTIONS] console.error(`Usage: migrate_ranges_support.mjs [OPTIONS]
Options: Options:
@ -112,11 +112,10 @@ function arrayOpt(value) {
} }
} }
main() try {
.then(() => { await main()
process.exit(0) process.exit(0)
}) } catch (error) {
.catch(err => { console.error(error)
console.error(err)
process.exit(1) process.exit(1)
}) }

View file

@ -6,9 +6,10 @@
// node scripts/merge_group_subscription_members \ // node scripts/merge_group_subscription_members \
// --target [targetSubscriptionId] --source [sourceSubscriptionId] --commit // --target [targetSubscriptionId] --source [sourceSubscriptionId] --commit
const { db, ObjectId, waitForDb } = require('../app/src/infrastructure/mongodb') import { db, ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js'
const SubscriptionUpdater = require('../app/src/Features/Subscription/SubscriptionUpdater')
const minimist = require('minimist') import SubscriptionUpdater from '../app/src/Features/Subscription/SubscriptionUpdater.js'
import minimist from 'minimist'
const argv = minimist(process.argv.slice(2), { const argv = minimist(process.argv.slice(2), {
string: ['target', 'source'], string: ['target', 'source'],
boolean: ['commit'], boolean: ['commit'],
@ -93,12 +94,11 @@ async function main() {
} }
} }
main() try {
.then(() => { await main()
console.error('Done.') console.error('Done.')
process.exit(0) process.exit(0)
}) } catch (error) {
.catch(error => {
console.error({ error }) console.error({ error })
process.exit(1) process.exit(1)
}) }

View file

@ -1,11 +1,9 @@
const { import {
db, db,
waitForDb, waitForDb,
READ_PREFERENCE_SECONDARY, READ_PREFERENCE_SECONDARY,
} = require('../../app/src/infrastructure/mongodb') } from '../../app/src/infrastructure/mongodb.js'
const { import { hashSecret } from '../../modules/oauth2-server/app/src/SecretsHelper.js'
hashSecret,
} = require('../../modules/oauth2-server/app/src/SecretsHelper')
async function main() { async function main() {
await waitForDb() await waitForDb()
@ -38,11 +36,10 @@ async function hashSecrets(collection, field) {
console.log(`${hashedCount} secrets hashed`) console.log(`${hashedCount} secrets hashed`)
} }
main() try {
.then(() => { await main()
process.exit(0) process.exit(0)
}) } catch (error) {
.catch(err => { console.error(error)
console.error(err)
process.exit(1) process.exit(1)
}) }

View file

@ -1,8 +1,6 @@
const minimist = require('minimist') import minimist from 'minimist'
const { waitForDb, db } = require('../../app/src/infrastructure/mongodb') import { waitForDb, db } from '../../app/src/infrastructure/mongodb.js'
const { import { hashSecret } from '../../modules/oauth2-server/app/src/SecretsHelper.js'
hashSecret,
} = require('../../modules/oauth2-server/app/src/SecretsHelper')
async function main() { async function main() {
const opts = parseArgs() const opts = parseArgs()
@ -91,11 +89,10 @@ Options:
`) `)
} }
main() try {
.then(() => { await main()
process.exit(0) process.exit(0)
}) } catch (error) {
.catch(err => { console.error(error)
console.error(err)
process.exit(1) process.exit(1)
}) }

View file

@ -1,9 +1,9 @@
const minimist = require('minimist') import minimist from 'minimist'
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 { import { hashSecret } from '../../modules/oauth2-server/app/src/SecretsHelper.js'
hashSecret,
} = require('../../modules/oauth2-server/app/src/SecretsHelper') const { ObjectId } = mongodb
async function main() { async function main() {
const opts = parseArgs() const opts = parseArgs()
@ -121,11 +121,10 @@ function toArray(value) {
} }
} }
main() try {
.then(() => { await main()
process.exit(0) process.exit(0)
}) } catch (error) {
.catch(err => { console.error(error)
console.error(err)
process.exit(1) process.exit(1)
}) }

View file

@ -1,9 +1,9 @@
const minimist = require('minimist') import minimist from 'minimist'
const { import {
waitForDb, waitForDb,
db, db,
READ_PREFERENCE_SECONDARY, READ_PREFERENCE_SECONDARY,
} = require('../../app/src/infrastructure/mongodb') } from '../../app/src/infrastructure/mongodb.js'
async function main() { async function main() {
const opts = parseArgs() const opts = parseArgs()
@ -114,11 +114,10 @@ Options:
`) `)
} }
main() try {
.then(() => { await main()
process.exit(0) process.exit(0)
}) } catch (error) {
.catch(err => { console.error(error)
console.error(err)
process.exit(1) process.exit(1)
}) }

View file

@ -1,10 +1,11 @@
const RedisWrapper = require('@overleaf/redis-wrapper') import RedisWrapper from '@overleaf/redis-wrapper'
const Settings = require('@overleaf/settings') import Settings from '@overleaf/settings'
const SessionManager = require('../app/src/Features/Authentication/SessionManager') import SessionManager from '../app/src/Features/Authentication/SessionManager.js'
import minimist from 'minimist'
const redis = RedisWrapper.createClient(Settings.redis.websessions) const redis = RedisWrapper.createClient(Settings.redis.websessions)
const argv = require('minimist')(process.argv.slice(2), { const argv = minimist(process.argv.slice(2), {
string: ['count'], string: ['count'],
boolean: ['dry-run', 'help'], boolean: ['dry-run', 'help'],
alias: { alias: {
@ -92,7 +93,9 @@ async function scanAndPurge() {
redis.quit() redis.quit()
} }
scanAndPurge().catch(err => { try {
console.error(err) await scanAndPurge()
} catch (error) {
console.error(error)
process.exit() process.exit()
}) }

View file

@ -1,12 +1,13 @@
// recover docs from redis where there is no doc in mongo but the project exists // recover docs from redis where there is no doc in mongo but the project exists
const minimist = require('minimist') import minimist from 'minimist'
const { db, waitForDb, ObjectId } = require('../app/src/infrastructure/mongodb')
const ProjectEntityUpdateHandler = require('../app/src/Features/Project/ProjectEntityUpdateHandler') import { db, waitForDb, ObjectId } from '../app/src/infrastructure/mongodb.js'
const ProjectEntityRestoreHandler = require('../app/src/Features/Project/ProjectEntityRestoreHandler') import ProjectEntityUpdateHandler from '../app/src/Features/Project/ProjectEntityUpdateHandler.js'
const RedisWrapper = require('@overleaf/redis-wrapper') import ProjectEntityRestoreHandler from '../app/src/Features/Project/ProjectEntityRestoreHandler.js'
const Settings = require('@overleaf/settings') import RedisWrapper from '@overleaf/redis-wrapper'
const logger = require('@overleaf/logger') import Settings from '@overleaf/settings'
import logger from '@overleaf/logger'
const opts = parseArgs() const opts = parseArgs()
const redis = RedisWrapper.createClient(Settings.redis.web) const redis = RedisWrapper.createClient(Settings.redis.web)
@ -172,11 +173,10 @@ async function deleteDocFromRedis(projectId, docId) {
await redis.srem(`DocsIn:{${projectId}}`, projectId) await redis.srem(`DocsIn:{${projectId}}`, projectId)
} }
main() try {
.then(() => { await main()
process.exit(0) process.exit(0)
}) } catch (error) {
.catch(err => { console.error(error)
console.error(err)
process.exit(1) process.exit(1)
}) }

View file

@ -1,9 +1,10 @@
const RecurlyWrapper = require('../../app/src/Features/Subscription/RecurlyWrapper') import RecurlyWrapper from '../../app/src/Features/Subscription/RecurlyWrapper.js'
const minimist = require('minimist') import minimist from 'minimist'
const logger = require('@overleaf/logger') import logger from '@overleaf/logger'
import { fileURLToPath } from 'url'
const waitMs = const waitMs =
require.main === module fileURLToPath(import.meta.url) === process.argv[1]
? timeout => new Promise(resolve => setTimeout(() => resolve(), timeout)) ? timeout => new Promise(resolve => setTimeout(() => resolve(), timeout))
: () => Promise.resolve() : () => Promise.resolve()
@ -116,16 +117,15 @@ const main = async () => {
} }
} }
if (require.main === module) { if (fileURLToPath(import.meta.url) === process.argv[1]) {
main() try {
.then(() => { await main()
logger.info('Done.') logger.info('Done.')
process.exit(0) process.exit(0)
}) } catch (error) {
.catch(err => { logger.error({ error }, 'Error')
logger.error({ err }, 'Error')
process.exit(1) process.exit(1)
}) }
} }
module.exports = { main } export default { main }

View file

@ -1,6 +1,6 @@
const RecurlyWrapper = require('../../app/src/Features/Subscription/RecurlyWrapper') import RecurlyWrapper from '../../app/src/Features/Subscription/RecurlyWrapper.js'
const async = require('async') import async from 'async'
const CSVParser = require('json2csv').Parser import { Parser as CSVParser } from 'json2csv'
const NOW = new Date() const NOW = new Date()

View file

@ -2,10 +2,11 @@
// app/templates/plans/groups.json // app/templates/plans/groups.json
// //
// Usage example: // Usage example:
// node scripts/recurly/get_recurly_group_prices.js // node scripts/recurly/get_recurly_group_prices.mjs
const recurly = require('recurly') import recurly from 'recurly'
const Settings = require('@overleaf/settings')
import Settings from '@overleaf/settings'
const recurlySettings = Settings.apis.recurly const recurlySettings = Settings.apis.recurly
const recurlyApiKey = recurlySettings ? recurlySettings.apiKey : undefined const recurlyApiKey = recurlySettings ? recurlySettings.apiKey : undefined
@ -37,11 +38,10 @@ async function main() {
console.log(JSON.stringify(prices, undefined, 2)) console.log(JSON.stringify(prices, undefined, 2))
} }
main() try {
.then(() => { await main()
process.exit(0) process.exit(0)
}) } catch (error) {
.catch(error => {
console.error({ error }) console.error({ error })
process.exit(1) process.exit(1)
}) }

View file

@ -1,18 +1,11 @@
const { waitForDb } = require('../app/src/infrastructure/mongodb') import { waitForDb } from '../app/src/infrastructure/mongodb.js'
const minimist = require('minimist') import minimist from 'minimist'
const InstitutionsManager = require('../app/src/Features/Institutions/InstitutionsManager') import InstitutionsManager from '../app/src/Features/Institutions/InstitutionsManager.js'
const institutionId = parseInt(process.argv[2]) const institutionId = parseInt(process.argv[2])
if (isNaN(institutionId)) throw new Error('No institution id') if (isNaN(institutionId)) throw new Error('No institution id')
console.log('Refreshing users at institution', institutionId) console.log('Refreshing users at institution', institutionId)
waitForDb()
.then(main)
.catch(err => {
console.error(err)
process.exit(1)
})
function main() { function main() {
const argv = minimist(process.argv.slice(2)) const argv = minimist(process.argv.slice(2))
if (!argv.notify) { if (!argv.notify) {
@ -37,3 +30,11 @@ function main() {
} }
) )
} }
try {
await waitForDb()
await main()
} catch (error) {
console.error(error)
process.exit(1)
}

View file

@ -2,10 +2,12 @@ import sinon from 'sinon'
import chai, { expect } from 'chai' import chai, { expect } from 'chai'
import chaiAsPromised from 'chai-as-promised' import chaiAsPromised from 'chai-as-promised'
import sinonChai from 'sinon-chai' import sinonChai from 'sinon-chai'
import { main } from '../../../scripts/recurly/collect_paypal_past_due_invoice.js' import CollectPaypalPastDueInvoice from '../../../scripts/recurly/collect_paypal_past_due_invoice.mjs'
import RecurlyWrapper from '../../../app/src/Features/Subscription/RecurlyWrapper.js' import RecurlyWrapper from '../../../app/src/Features/Subscription/RecurlyWrapper.js'
import OError from '@overleaf/o-error' import OError from '@overleaf/o-error'
const { main } = CollectPaypalPastDueInvoice
chai.use(chaiAsPromised) chai.use(chaiAsPromised)
chai.use(sinonChai) chai.use(sinonChai)