From 14cd8f54794be1152e50eabc9346e0c7ebd5b754 Mon Sep 17 00:00:00 2001 From: Liangjun Song <146005915+adai26@users.noreply.github.com> Date: Thu, 24 Oct 2024 12:14:00 +0100 Subject: [PATCH] Merge pull request #21282 from overleaf/ls-scripts-to-esm-5 Migrate rest of the scripts to esm GitOrigin-RevId: 421f3ccd15342d34113be8d22e343d08533177ea --- ...nt_to_csv.js => add_user_count_to_csv.mjs} | 33 +++++++-------- .../{bench_bcrypt.js => bench_bcrypt.mjs} | 27 ++++++------ .../web/scripts/learn/checkSanitize/README.md | 2 +- ...izeOptions.js => checkSanitizeOptions.mjs} | 26 ++++++------ .../checkSanitize/{index.js => index.mjs} | 20 +++++---- .../checkSanitize/{scrape.js => scrape.mjs} | 16 ++++---- services/web/scripts/plan-prices/README.md | 4 +- .../plan-prices/{plans.js => plans.mjs} | 15 ++++--- ...enewal.js => change_prices_at_renewal.mjs} | 22 +++++----- ..._prices.js => generate_recurly_prices.mjs} | 13 +++--- .../{recurly_prices.js => recurly_prices.mjs} | 41 +++++++++---------- ...scriptions.js => resync_subscriptions.mjs} | 24 +++++------ ...tlements.js => sync-user-entitlements.mjs} | 22 +++++----- 13 files changed, 140 insertions(+), 125 deletions(-) rename services/web/scripts/{add_user_count_to_csv.js => add_user_count_to_csv.mjs} (82%) rename services/web/scripts/{bench_bcrypt.js => bench_bcrypt.mjs} (86%) rename services/web/scripts/learn/checkSanitize/{checkSanitizeOptions.js => checkSanitizeOptions.mjs} (85%) rename services/web/scripts/learn/checkSanitize/{index.js => index.mjs} (56%) rename services/web/scripts/learn/checkSanitize/{scrape.js => scrape.mjs} (93%) rename services/web/scripts/plan-prices/{plans.js => plans.mjs} (92%) rename services/web/scripts/recurly/{change_prices_at_renewal.js => change_prices_at_renewal.mjs} (95%) rename services/web/scripts/recurly/{generate_recurly_prices.js => generate_recurly_prices.mjs} (91%) rename services/web/scripts/recurly/{recurly_prices.js => recurly_prices.mjs} (92%) rename services/web/scripts/recurly/{resync_subscriptions.js => resync_subscriptions.mjs} (88%) rename services/web/scripts/sync-user-entitlements/{sync-user-entitlements.js => sync-user-entitlements.mjs} (94%) diff --git a/services/web/scripts/add_user_count_to_csv.js b/services/web/scripts/add_user_count_to_csv.mjs similarity index 82% rename from services/web/scripts/add_user_count_to_csv.js rename to services/web/scripts/add_user_count_to_csv.mjs index ae841e9bcd..494988c3f6 100644 --- a/services/web/scripts/add_user_count_to_csv.js +++ b/services/web/scripts/add_user_count_to_csv.mjs @@ -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 // columns for the number of users in the domain, subdomains, and total. -const fs = require('fs') -const csv = require('csv/sync') -const minimist = require('minimist') -const UserGetter = require('../app/src/Features/User/UserGetter') -const { db, waitForDb } = require('../app/src/infrastructure/mongodb') -const _ = require('lodash') +import fs from 'fs' +// https://github.com/import-js/eslint-plugin-import/issues/1810 +// eslint-disable-next-line import/no-unresolved +import * as csv from 'csv/sync' +import minimist from 'minimist' +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), { string: ['domain', 'output'], @@ -84,12 +86,11 @@ async function getUsersByHostnameWithSubdomain(domain, projection) { return await db.users.find(query, { projection }).toArray() } -main() - .then(() => { - console.error('Done') - process.exit(0) - }) - .catch(err => { - console.error(err) - process.exit(1) - }) +try { + await main() + console.log('Done') + process.exit(0) +} catch (error) { + console.error(error) + process.exit(1) +} diff --git a/services/web/scripts/bench_bcrypt.js b/services/web/scripts/bench_bcrypt.mjs similarity index 86% rename from services/web/scripts/bench_bcrypt.js rename to services/web/scripts/bench_bcrypt.mjs index 5b4fe9c802..79a27224cd 100644 --- a/services/web/scripts/bench_bcrypt.js +++ b/services/web/scripts/bench_bcrypt.mjs @@ -1,8 +1,10 @@ -const minimist = require('minimist') -const { promisify } = require('util') -const bcrypt = require('bcrypt') -const { promiseMapWithLimit } = require('@overleaf/promise-utils') -const csv = require('csv/sync') +import minimist from 'minimist' +import { promisify } from 'util' +import bcrypt from 'bcrypt' +import { promiseMapWithLimit } from '@overleaf/promise-utils' +// 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 bcryptGenSalt = promisify(bcrypt.genSalt) @@ -111,11 +113,10 @@ async function main() { if (argv.csv) console.log(csv.stringify(STATS, { header: true })) } -main() - .then(() => { - process.exit(0) - }) - .catch(err => { - console.error(err) - process.exit(1) - }) +try { + await main() + process.exit(0) +} catch (error) { + console.error(error) + process.exit(1) +} diff --git a/services/web/scripts/learn/checkSanitize/README.md b/services/web/scripts/learn/checkSanitize/README.md index b425c5c576..27c903d96f 100644 --- a/services/web/scripts/learn/checkSanitize/README.md +++ b/services/web/scripts/learn/checkSanitize/README.md @@ -1,7 +1,7 @@ # Usage ``` -node scripts/learn/checkSanitize https://LEARN_WIKI +node scripts/learn/checkSanitize/index.mjs https://LEARN_WIKI ``` ## Bulk export diff --git a/services/web/scripts/learn/checkSanitize/checkSanitizeOptions.js b/services/web/scripts/learn/checkSanitize/checkSanitizeOptions.mjs similarity index 85% rename from services/web/scripts/learn/checkSanitize/checkSanitizeOptions.js rename to services/web/scripts/learn/checkSanitize/checkSanitizeOptions.mjs index da7c221112..6eec5ce845 100644 --- a/services/web/scripts/learn/checkSanitize/checkSanitizeOptions.js +++ b/services/web/scripts/learn/checkSanitize/checkSanitizeOptions.mjs @@ -1,15 +1,15 @@ -const crypto = require('crypto') -const fs = require('fs') -const Path = require('path') - -const cheerio = require('cheerio') -const prettier = require('prettier') -const sanitizeHtml = require('sanitize-html') - -const { - sanitizeOptions, -} = require('../../../modules/learn/app/src/sanitizeOptions') +import crypto from 'crypto' +import fs from 'fs' +import Path from 'path' +import cheerio from 'cheerio' +// checkSanitizeOptions is only used in dev env +// eslint-disable-next-line import/no-extraneous-dependencies +import prettier from 'prettier' +import sanitizeHtml from 'sanitize-html' +import { sanitizeOptions } from '../../../modules/learn/app/src/sanitizeOptions.js' +import { fileURLToPath } from 'url' +const __dirname = Path.dirname(fileURLToPath(import.meta.url)) const EXTRACT_STYLE = process.env.EXTRACT_STYLES === 'true' const OMIT_STYLE = process.env.OMIT_STYLE !== 'false' const DUMP_CSS_IN = Path.join( @@ -115,6 +115,4 @@ function checkSanitizeOptions(page, title, text) { console.error('sanitizedToText:', peak(sanitizedToText, offsetText)) } -module.exports = { - checkSanitizeOptions, -} +export default checkSanitizeOptions diff --git a/services/web/scripts/learn/checkSanitize/index.js b/services/web/scripts/learn/checkSanitize/index.mjs similarity index 56% rename from services/web/scripts/learn/checkSanitize/index.js rename to services/web/scripts/learn/checkSanitize/index.mjs index 340383bc98..6040e831e8 100644 --- a/services/web/scripts/learn/checkSanitize/index.js +++ b/services/web/scripts/learn/checkSanitize/index.mjs @@ -1,11 +1,14 @@ -const { checkSanitizeOptions } = require('./checkSanitizeOptions') -const { getAllPagesAndCache, scrapeAndCachePage } = require('./scrape') +import checkSanitizeOptions from './checkSanitizeOptions.mjs' +import Scrape from './scrape.mjs' +import { fileURLToPath } from 'url' + +const { getAllPagesAndCache, scrapeAndCachePage } = Scrape async function main() { const BASE_URL = process.argv.pop() if (!BASE_URL.startsWith('http')) { 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) { - main().catch(err => { - console.error(err) +if (fileURLToPath(import.meta.url) === process.argv[1]) { + try { + await main() + process.exit(0) + } catch (error) { + console.error(error) process.exit(1) - }) + } } diff --git a/services/web/scripts/learn/checkSanitize/scrape.js b/services/web/scripts/learn/checkSanitize/scrape.mjs similarity index 93% rename from services/web/scripts/learn/checkSanitize/scrape.js rename to services/web/scripts/learn/checkSanitize/scrape.mjs index 7d14d4504f..4052ed2797 100644 --- a/services/web/scripts/learn/checkSanitize/scrape.js +++ b/services/web/scripts/learn/checkSanitize/scrape.mjs @@ -1,12 +1,14 @@ -const Path = require('path') -const fs = require('fs') - -const { +import Path from 'path' +import fs from 'fs' +import { fetchString, fetchJson, 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( Path.dirname(Path.dirname(Path.dirname(__dirname))), 'data', @@ -33,8 +35,6 @@ async function scrape(baseUrl, page) { } } -const crypto = require('crypto') - function hash(blob) { return crypto.createHash('sha1').update(blob).digest('hex') } @@ -124,7 +124,7 @@ async function getAllPagesAndCache(baseUrl) { } } -module.exports = { +export default { getAllPagesAndCache, scrapeAndCachePage, } diff --git a/services/web/scripts/plan-prices/README.md b/services/web/scripts/plan-prices/README.md index 948911548f..d26b288108 100644 --- a/services/web/scripts/plan-prices/README.md +++ b/services/web/scripts/plan-prices/README.md @@ -6,7 +6,7 @@ The scripts will put the output results into the `output` folder. ### 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` - `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 - `.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 diff --git a/services/web/scripts/plan-prices/plans.js b/services/web/scripts/plan-prices/plans.mjs similarity index 92% rename from services/web/scripts/plan-prices/plans.js rename to services/web/scripts/plan-prices/plans.mjs index b1b2719fd8..b648c77cb5 100644 --- a/services/web/scripts/plan-prices/plans.js +++ b/services/web/scripts/plan-prices/plans.mjs @@ -1,10 +1,15 @@ // Creates data for localizedPlanPricing object in settings.overrides.saas.js // and plans object in main/plans.js -const csv = require('csv/sync') -const fs = require('fs') -const path = require('path') -const minimist = require('minimist') +// https://github.com/import-js/eslint-plugin-import/issues/1810 +// eslint-disable-next-line import/no-unresolved +import * as csv from 'csv/sync' +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) { // Pick the csv file @@ -156,7 +161,7 @@ if (argv.file) { console.log('Invalid file type: must be csv or json') } } else { - console.log('usage: node plans.js -f -o ') + console.log('usage: node plans.mjs -f -o ') process.exit(1) } // removes quotes from object keys diff --git a/services/web/scripts/recurly/change_prices_at_renewal.js b/services/web/scripts/recurly/change_prices_at_renewal.mjs similarity index 95% rename from services/web/scripts/recurly/change_prices_at_renewal.js rename to services/web/scripts/recurly/change_prices_at_renewal.mjs index 596b1e8278..05c28ddfe9 100644 --- a/services/web/scripts/recurly/change_prices_at_renewal.js +++ b/services/web/scripts/recurly/change_prices_at_renewal.mjs @@ -1,9 +1,9 @@ -const fs = require('fs') -const { setTimeout } = require('timers/promises') -const csv = require('csv') -const minimist = require('minimist') -const recurly = require('recurly') -const Settings = require('@overleaf/settings') +import fs from 'fs' +import { setTimeout } from 'timers/promises' +import * as csv from 'csv' +import minimist from 'minimist' +import recurly from 'recurly' +import Settings from '@overleaf/settings' const recurlyClient = new recurly.Client(Settings.apis.recurly.apiKey) @@ -206,7 +206,7 @@ function parseArgs() { } 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: @@ -222,7 +222,9 @@ class ReportError extends Error { } } -main().catch(err => { - console.error(err) +try { + await main() +} catch (error) { + console.error(error) process.exit(1) -}) +} diff --git a/services/web/scripts/recurly/generate_recurly_prices.js b/services/web/scripts/recurly/generate_recurly_prices.mjs similarity index 91% rename from services/web/scripts/recurly/generate_recurly_prices.js rename to services/web/scripts/recurly/generate_recurly_prices.mjs index 4279bc921d..f9d6e49e37 100644 --- a/services/web/scripts/recurly/generate_recurly_prices.js +++ b/services/web/scripts/recurly/generate_recurly_prices.mjs @@ -2,7 +2,7 @@ // // 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: // @@ -16,10 +16,13 @@ // // The output can be used as input for the upload script `recurly_prices.js`. -const minimist = require('minimist') -const csv = require('csv/sync') -const _ = require('lodash') -const fs = require('fs') +import minimist from 'minimist' + +// https://github.com/import-js/eslint-plugin-import/issues/1810 +// 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), { string: ['output', 'file'], diff --git a/services/web/scripts/recurly/recurly_prices.js b/services/web/scripts/recurly/recurly_prices.mjs similarity index 92% rename from services/web/scripts/recurly/recurly_prices.js rename to services/web/scripts/recurly/recurly_prices.mjs index 6a7151c613..cf9649da1a 100644 --- a/services/web/scripts/recurly/recurly_prices.js +++ b/services/web/scripts/recurly/recurly_prices.mjs @@ -14,11 +14,12 @@ // 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. -const recurly = require('recurly') -const Settings = require('@overleaf/settings') -const minimist = require('minimist') -const _ = require('lodash') -const fs = require('fs') +import recurly from 'recurly' + +import Settings from '@overleaf/settings' +import minimist from 'minimist' +import _ from 'lodash' +import fs from 'fs' const recurlySettings = Settings.apis.recurly const recurlyApiKey = recurlySettings ? recurlySettings.apiKey : undefined @@ -203,23 +204,21 @@ if (argv.upload && DRY_RUN === COMMIT) { } if (argv.download) { - download(argv.output) - .then(() => { - process.exit(0) - }) - .catch(error => { - console.error({ error }) - process.exit(1) - }) + try { + await download(argv.output) + process.exit(0) + } catch (error) { + console.error({ error }) + process.exit(1) + } } else if (argv.upload) { - upload(argv.file) - .then(() => { - process.exit(0) - }) - .catch(error => { - console.error({ error }) - process.exit(1) - }) + try { + await upload(argv.file) + process.exit(0) + } catch (error) { + console.error({ error }) + process.exit(1) + } } else { console.log( 'usage:\n' + ' --save -o file.json\n' + ' --load -f file.json\n' diff --git a/services/web/scripts/recurly/resync_subscriptions.js b/services/web/scripts/recurly/resync_subscriptions.mjs similarity index 88% rename from services/web/scripts/recurly/resync_subscriptions.js rename to services/web/scripts/recurly/resync_subscriptions.mjs index 29345fb65c..dce61b114d 100644 --- a/services/web/scripts/recurly/resync_subscriptions.js +++ b/services/web/scripts/recurly/resync_subscriptions.mjs @@ -1,13 +1,13 @@ -const { Subscription } = require('../../app/src/models/Subscription') -const RecurlyWrapper = require('../../app/src/Features/Subscription/RecurlyWrapper') -const SubscriptionUpdater = require('../../app/src/Features/Subscription/SubscriptionUpdater') -const minimist = require('minimist') -const { setTimeout } = require('node:timers/promises') +import { Subscription } from '../../app/src/models/Subscription.js' +import RecurlyWrapper from '../../app/src/Features/Subscription/RecurlyWrapper.js' +import SubscriptionUpdater from '../../app/src/Features/Subscription/SubscriptionUpdater.js' +import minimist from 'minimist' +import { setTimeout } from 'node:timers/promises' -// make sure all `allMismatchReasons` are displayed in the output -const util = require('util') -const pLimit = require('p-limit') -const { waitForDb } = require('../../app/src/infrastructure/mongodb') +import util from 'util' + +import pLimit from 'p-limit' +import { waitForDb } from '../../app/src/infrastructure/mongodb.js' util.inspect.defaultOptions.maxArrayLength = null @@ -16,6 +16,7 @@ const ScriptLogger = { mismatchSubscriptionsCount: 0, allMismatchReasons: {}, + // make sure all `allMismatchReasons` are displayed in the output recordMismatch: (subscription, recurlySubscription) => { const mismatchReasons = {} if (subscription.planCode !== recurlySubscription.plan.plan_code) { @@ -184,6 +185,5 @@ const setup = () => { } setup() -run().then(() => { - process.exit() -}) +await run() +process.exit() diff --git a/services/web/scripts/sync-user-entitlements/sync-user-entitlements.js b/services/web/scripts/sync-user-entitlements/sync-user-entitlements.mjs similarity index 94% rename from services/web/scripts/sync-user-entitlements/sync-user-entitlements.js rename to services/web/scripts/sync-user-entitlements/sync-user-entitlements.mjs index a40527e5e6..70f4d856ae 100644 --- a/services/web/scripts/sync-user-entitlements/sync-user-entitlements.js +++ b/services/web/scripts/sync-user-entitlements/sync-user-entitlements.mjs @@ -1,11 +1,8 @@ -'use strict' - -const fs = require('fs') -const minimist = require('minimist') - -const InstitutionsAPI = - require('../../app/src/Features/Institutions/InstitutionsAPI').promises +import fs from 'fs' +import minimist from 'minimist' +import InstitutionsAPIModule from '../../app/src/Features/Institutions/InstitutionsAPI.js' +const { promises: InstitutionsAPI } = InstitutionsAPIModule const argv = minimist(process.argv.slice(2)) const commit = argv.commit !== undefined const ignoreNulls = !!argv['ignore-nulls'] @@ -17,10 +14,6 @@ if (!commit) { const userEntitlements = loadUserEntitlements(argv['user-entitlements']) const cachedEntitlements = loadCachedEntitlements(argv['cached-entitlements']) -syncUserEntitlements(userEntitlements, cachedEntitlements) - .catch(err => console.error(err.stack)) - .then(() => process.exit()) - async function syncUserEntitlements(userEntitlements, cachedEntitlements) { // check for user entitlements in mongo but not in postgres for (const key of Object.keys(userEntitlements)) { @@ -194,3 +187,10 @@ function loadCachedEntitlements(cachedEntitlementsFilename) { return cachedEntitlements } + +try { + await syncUserEntitlements(userEntitlements, cachedEntitlements) +} catch (error) { + console.error(error.stack) +} +process.exit()