Merge pull request #21282 from overleaf/ls-scripts-to-esm-5

Migrate rest of the scripts to esm

GitOrigin-RevId: 421f3ccd15342d34113be8d22e343d08533177ea
This commit is contained in:
Liangjun Song 2024-10-24 12:14:00 +01:00 committed by Copybot
parent e3b93f0a22
commit 14cd8f5479
13 changed files with 140 additions and 125 deletions

View file

@ -1,12 +1,14 @@
// Usage: node scripts/add_user_count_to_csv.js [OPTS] [INPUT-FILE] // Usage: node scripts/add_user_count_to_csv.mjs [OPTS] [INPUT-FILE]
// Looks up the number of users for each domain in the input csv file and adds // Looks up the number of users for each domain in the input csv file and adds
// columns for the number of users in the domain, subdomains, and total. // columns for the number of users in the domain, subdomains, and total.
const fs = require('fs') import fs from 'fs'
const csv = require('csv/sync') // https://github.com/import-js/eslint-plugin-import/issues/1810
const minimist = require('minimist') // eslint-disable-next-line import/no-unresolved
const UserGetter = require('../app/src/Features/User/UserGetter') import * as csv from 'csv/sync'
const { db, waitForDb } = require('../app/src/infrastructure/mongodb') import minimist from 'minimist'
const _ = require('lodash') import UserGetter from '../app/src/Features/User/UserGetter.js'
import { db, waitForDb } from '../app/src/infrastructure/mongodb.js'
import _ from 'lodash'
const argv = minimist(process.argv.slice(2), { const argv = minimist(process.argv.slice(2), {
string: ['domain', 'output'], string: ['domain', 'output'],
@ -84,12 +86,11 @@ async function getUsersByHostnameWithSubdomain(domain, projection) {
return await db.users.find(query, { projection }).toArray() return await db.users.find(query, { projection }).toArray()
} }
main() try {
.then(() => { await main()
console.error('Done') console.log('Done')
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,10 @@
const minimist = require('minimist') import minimist from 'minimist'
const { promisify } = require('util') import { promisify } from 'util'
const bcrypt = require('bcrypt') import bcrypt from 'bcrypt'
const { promiseMapWithLimit } = require('@overleaf/promise-utils') import { promiseMapWithLimit } from '@overleaf/promise-utils'
const csv = require('csv/sync') // https://github.com/import-js/eslint-plugin-import/issues/1810
// eslint-disable-next-line import/no-unresolved
import * as csv from 'csv/sync'
const bcryptCompare = promisify(bcrypt.compare) const bcryptCompare = promisify(bcrypt.compare)
const bcryptGenSalt = promisify(bcrypt.genSalt) const bcryptGenSalt = promisify(bcrypt.genSalt)
@ -111,11 +113,10 @@ async function main() {
if (argv.csv) console.log(csv.stringify(STATS, { header: true })) if (argv.csv) console.log(csv.stringify(STATS, { header: true }))
} }
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,7 +1,7 @@
# Usage # Usage
``` ```
node scripts/learn/checkSanitize https://LEARN_WIKI node scripts/learn/checkSanitize/index.mjs https://LEARN_WIKI
``` ```
## Bulk export ## Bulk export

View file

@ -1,15 +1,15 @@
const crypto = require('crypto') import crypto from 'crypto'
const fs = require('fs') import fs from 'fs'
const Path = require('path') import Path from 'path'
import cheerio from 'cheerio'
const cheerio = require('cheerio') // checkSanitizeOptions is only used in dev env
const prettier = require('prettier') // eslint-disable-next-line import/no-extraneous-dependencies
const sanitizeHtml = require('sanitize-html') import prettier from 'prettier'
import sanitizeHtml from 'sanitize-html'
const { import { sanitizeOptions } from '../../../modules/learn/app/src/sanitizeOptions.js'
sanitizeOptions, import { fileURLToPath } from 'url'
} = require('../../../modules/learn/app/src/sanitizeOptions')
const __dirname = Path.dirname(fileURLToPath(import.meta.url))
const EXTRACT_STYLE = process.env.EXTRACT_STYLES === 'true' const EXTRACT_STYLE = process.env.EXTRACT_STYLES === 'true'
const OMIT_STYLE = process.env.OMIT_STYLE !== 'false' const OMIT_STYLE = process.env.OMIT_STYLE !== 'false'
const DUMP_CSS_IN = Path.join( const DUMP_CSS_IN = Path.join(
@ -115,6 +115,4 @@ function checkSanitizeOptions(page, title, text) {
console.error('sanitizedToText:', peak(sanitizedToText, offsetText)) console.error('sanitizedToText:', peak(sanitizedToText, offsetText))
} }
module.exports = { export default checkSanitizeOptions
checkSanitizeOptions,
}

View file

@ -1,11 +1,14 @@
const { checkSanitizeOptions } = require('./checkSanitizeOptions') import checkSanitizeOptions from './checkSanitizeOptions.mjs'
const { getAllPagesAndCache, scrapeAndCachePage } = require('./scrape') import Scrape from './scrape.mjs'
import { fileURLToPath } from 'url'
const { getAllPagesAndCache, scrapeAndCachePage } = Scrape
async function main() { async function main() {
const BASE_URL = process.argv.pop() const BASE_URL = process.argv.pop()
if (!BASE_URL.startsWith('http')) { if (!BASE_URL.startsWith('http')) {
throw new Error( throw new Error(
'Usage: node scripts/learn/checkSanitize https://LEARN_WIKI' 'Usage: node scripts/learn/checkSanitize/index.mjs https://LEARN_WIKI'
) )
} }
@ -27,9 +30,12 @@ async function main() {
} }
} }
if (require.main === module) { if (fileURLToPath(import.meta.url) === process.argv[1]) {
main().catch(err => { try {
console.error(err) await main()
process.exit(0)
} catch (error) {
console.error(error)
process.exit(1) process.exit(1)
}) }
} }

View file

@ -1,12 +1,14 @@
const Path = require('path') import Path from 'path'
const fs = require('fs') import fs from 'fs'
import {
const {
fetchString, fetchString,
fetchJson, fetchJson,
RequestFailedError, RequestFailedError,
} = require('@overleaf/fetch-utils') } from '@overleaf/fetch-utils'
import crypto from 'crypto'
import { fileURLToPath } from 'url'
const __dirname = Path.dirname(fileURLToPath(import.meta.url))
const CACHE_IN = Path.join( const CACHE_IN = Path.join(
Path.dirname(Path.dirname(Path.dirname(__dirname))), Path.dirname(Path.dirname(Path.dirname(__dirname))),
'data', 'data',
@ -33,8 +35,6 @@ async function scrape(baseUrl, page) {
} }
} }
const crypto = require('crypto')
function hash(blob) { function hash(blob) {
return crypto.createHash('sha1').update(blob).digest('hex') return crypto.createHash('sha1').update(blob).digest('hex')
} }
@ -124,7 +124,7 @@ async function getAllPagesAndCache(baseUrl) {
} }
} }
module.exports = { export default {
getAllPagesAndCache, getAllPagesAndCache,
scrapeAndCachePage, scrapeAndCachePage,
} }

View file

@ -6,7 +6,7 @@ The scripts will put the output results into the `output` folder.
### Create localized and group plan pricing ### Create localized and group plan pricing
_Command_ `node plans.js -f fileName -o outputdir` - generates three json files: _Command_ `node plans.mjs -f fileName -o outputdir` - generates three json files:
- `localizedPlanPricing.json` for `/services/web/config/settings.overrides.saas.js` - `localizedPlanPricing.json` for `/services/web/config/settings.overrides.saas.js`
- `groups.json` for `/services/web/app/templates/plans/groups.json` - `groups.json` for `/services/web/app/templates/plans/groups.json`
@ -14,4 +14,4 @@ _Command_ `node plans.js -f fileName -o outputdir` - generates three json files:
The input file can be in `.csv` or `.json` format The input file can be in `.csv` or `.json` format
- `.csv` csv format - `.csv` csv format
- `.json` json format from the `recurly_prices.js --download` script output - `.json` json format from the `recurly_prices.mjs --download` script output

View file

@ -1,10 +1,15 @@
// Creates data for localizedPlanPricing object in settings.overrides.saas.js // Creates data for localizedPlanPricing object in settings.overrides.saas.js
// and plans object in main/plans.js // and plans object in main/plans.js
const csv = require('csv/sync') // https://github.com/import-js/eslint-plugin-import/issues/1810
const fs = require('fs') // eslint-disable-next-line import/no-unresolved
const path = require('path') import * as csv from 'csv/sync'
const minimist = require('minimist') import fs from 'fs'
import path from 'path'
import minimist from 'minimist'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
function readCSVFile(fileName) { function readCSVFile(fileName) {
// Pick the csv file // Pick the csv file
@ -156,7 +161,7 @@ if (argv.file) {
console.log('Invalid file type: must be csv or json') console.log('Invalid file type: must be csv or json')
} }
} else { } else {
console.log('usage: node plans.js -f <file.csv|file.json> -o <dir>') console.log('usage: node plans.mjs -f <file.csv|file.json> -o <dir>')
process.exit(1) process.exit(1)
} }
// removes quotes from object keys // removes quotes from object keys

View file

@ -1,9 +1,9 @@
const fs = require('fs') import fs from 'fs'
const { setTimeout } = require('timers/promises') import { setTimeout } from 'timers/promises'
const csv = require('csv') import * as csv from 'csv'
const minimist = require('minimist') import minimist from 'minimist'
const recurly = require('recurly') import recurly from 'recurly'
const Settings = require('@overleaf/settings') import Settings from '@overleaf/settings'
const recurlyClient = new recurly.Client(Settings.apis.recurly.apiKey) const recurlyClient = new recurly.Client(Settings.apis.recurly.apiKey)
@ -206,7 +206,7 @@ function parseArgs() {
} }
function usage() { function usage() {
console.error(`Usage: node scripts/recurly/change_prices_at_renewal.js [OPTS] [INPUT-FILE] console.error(`Usage: node scripts/recurly/change_prices_at_renewal.mjs [OPTS] [INPUT-FILE]
Options: Options:
@ -222,7 +222,9 @@ class ReportError extends Error {
} }
} }
main().catch(err => { try {
console.error(err) await main()
} catch (error) {
console.error(error)
process.exit(1) process.exit(1)
}) }

View file

@ -2,7 +2,7 @@
// //
// Usage: // Usage:
// //
// $ node scripts/recurly/generate_recurly_prices.js -f input.csv -o prices.json // $ node scripts/recurly/generate_recurly_prices.mjs -f input.csv -o prices.json
// //
// The input csv file has the following format: // The input csv file has the following format:
// //
@ -16,10 +16,13 @@
// //
// The output can be used as input for the upload script `recurly_prices.js`. // The output can be used as input for the upload script `recurly_prices.js`.
const minimist = require('minimist') import minimist from 'minimist'
const csv = require('csv/sync')
const _ = require('lodash') // https://github.com/import-js/eslint-plugin-import/issues/1810
const fs = require('fs') // eslint-disable-next-line import/no-unresolved
import * as csv from 'csv/sync'
import _ from 'lodash'
import fs from 'fs'
const argv = minimist(process.argv.slice(2), { const argv = minimist(process.argv.slice(2), {
string: ['output', 'file'], string: ['output', 'file'],

View file

@ -14,11 +14,12 @@
// The idea is to download the current prices to a file, update them locally (e.g. via a script) // The idea is to download the current prices to a file, update them locally (e.g. via a script)
// and then upload them to recurly. // and then upload them to recurly.
const recurly = require('recurly') import recurly from 'recurly'
const Settings = require('@overleaf/settings')
const minimist = require('minimist') import Settings from '@overleaf/settings'
const _ = require('lodash') import minimist from 'minimist'
const fs = require('fs') import _ from 'lodash'
import fs from 'fs'
const recurlySettings = Settings.apis.recurly const recurlySettings = Settings.apis.recurly
const recurlyApiKey = recurlySettings ? recurlySettings.apiKey : undefined const recurlyApiKey = recurlySettings ? recurlySettings.apiKey : undefined
@ -203,23 +204,21 @@ if (argv.upload && DRY_RUN === COMMIT) {
} }
if (argv.download) { if (argv.download) {
download(argv.output) try {
.then(() => { await download(argv.output)
process.exit(0) process.exit(0)
}) } catch (error) {
.catch(error => {
console.error({ error }) console.error({ error })
process.exit(1) process.exit(1)
}) }
} else if (argv.upload) { } else if (argv.upload) {
upload(argv.file) try {
.then(() => { await upload(argv.file)
process.exit(0) process.exit(0)
}) } catch (error) {
.catch(error => {
console.error({ error }) console.error({ error })
process.exit(1) process.exit(1)
}) }
} else { } else {
console.log( console.log(
'usage:\n' + ' --save -o file.json\n' + ' --load -f file.json\n' 'usage:\n' + ' --save -o file.json\n' + ' --load -f file.json\n'

View file

@ -1,13 +1,13 @@
const { Subscription } = require('../../app/src/models/Subscription') import { Subscription } from '../../app/src/models/Subscription.js'
const RecurlyWrapper = require('../../app/src/Features/Subscription/RecurlyWrapper') import RecurlyWrapper from '../../app/src/Features/Subscription/RecurlyWrapper.js'
const SubscriptionUpdater = require('../../app/src/Features/Subscription/SubscriptionUpdater') import SubscriptionUpdater from '../../app/src/Features/Subscription/SubscriptionUpdater.js'
const minimist = require('minimist') import minimist from 'minimist'
const { setTimeout } = require('node:timers/promises') import { setTimeout } from 'node:timers/promises'
// make sure all `allMismatchReasons` are displayed in the output import util from 'util'
const util = require('util')
const pLimit = require('p-limit') import pLimit from 'p-limit'
const { waitForDb } = require('../../app/src/infrastructure/mongodb') import { waitForDb } from '../../app/src/infrastructure/mongodb.js'
util.inspect.defaultOptions.maxArrayLength = null util.inspect.defaultOptions.maxArrayLength = null
@ -16,6 +16,7 @@ const ScriptLogger = {
mismatchSubscriptionsCount: 0, mismatchSubscriptionsCount: 0,
allMismatchReasons: {}, allMismatchReasons: {},
// make sure all `allMismatchReasons` are displayed in the output
recordMismatch: (subscription, recurlySubscription) => { recordMismatch: (subscription, recurlySubscription) => {
const mismatchReasons = {} const mismatchReasons = {}
if (subscription.planCode !== recurlySubscription.plan.plan_code) { if (subscription.planCode !== recurlySubscription.plan.plan_code) {
@ -184,6 +185,5 @@ const setup = () => {
} }
setup() setup()
run().then(() => { await run()
process.exit() process.exit()
})

View file

@ -1,11 +1,8 @@
'use strict' import fs from 'fs'
import minimist from 'minimist'
const fs = require('fs') import InstitutionsAPIModule from '../../app/src/Features/Institutions/InstitutionsAPI.js'
const minimist = require('minimist')
const InstitutionsAPI =
require('../../app/src/Features/Institutions/InstitutionsAPI').promises
const { promises: InstitutionsAPI } = InstitutionsAPIModule
const argv = minimist(process.argv.slice(2)) const argv = minimist(process.argv.slice(2))
const commit = argv.commit !== undefined const commit = argv.commit !== undefined
const ignoreNulls = !!argv['ignore-nulls'] const ignoreNulls = !!argv['ignore-nulls']
@ -17,10 +14,6 @@ if (!commit) {
const userEntitlements = loadUserEntitlements(argv['user-entitlements']) const userEntitlements = loadUserEntitlements(argv['user-entitlements'])
const cachedEntitlements = loadCachedEntitlements(argv['cached-entitlements']) const cachedEntitlements = loadCachedEntitlements(argv['cached-entitlements'])
syncUserEntitlements(userEntitlements, cachedEntitlements)
.catch(err => console.error(err.stack))
.then(() => process.exit())
async function syncUserEntitlements(userEntitlements, cachedEntitlements) { async function syncUserEntitlements(userEntitlements, cachedEntitlements) {
// check for user entitlements in mongo but not in postgres // check for user entitlements in mongo but not in postgres
for (const key of Object.keys(userEntitlements)) { for (const key of Object.keys(userEntitlements)) {
@ -194,3 +187,10 @@ function loadCachedEntitlements(cachedEntitlementsFilename) {
return cachedEntitlements return cachedEntitlements
} }
try {
await syncUserEntitlements(userEntitlements, cachedEntitlements)
} catch (error) {
console.error(error.stack)
}
process.exit()