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
// 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')
try {
await main()
console.log('Done')
process.exit(0)
})
.catch(err => {
console.error(err)
} catch (error) {
console.error(error)
process.exit(1)
})
}

View file

@ -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(() => {
try {
await main()
process.exit(0)
})
.catch(err => {
console.error(err)
} catch (error) {
console.error(error)
process.exit(1)
})
}

View file

@ -1,7 +1,7 @@
# Usage
```
node scripts/learn/checkSanitize https://LEARN_WIKI
node scripts/learn/checkSanitize/index.mjs https://LEARN_WIKI
```
## Bulk export

View file

@ -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

View file

@ -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)
})
}
}

View file

@ -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,
}

View file

@ -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

View file

@ -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 <file.csv|file.json> -o <dir>')
console.log('usage: node plans.mjs -f <file.csv|file.json> -o <dir>')
process.exit(1)
}
// removes quotes from object keys

View file

@ -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)
})
}

View file

@ -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'],

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)
// 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(() => {
try {
await download(argv.output)
process.exit(0)
})
.catch(error => {
} catch (error) {
console.error({ error })
process.exit(1)
})
}
} else if (argv.upload) {
upload(argv.file)
.then(() => {
try {
await upload(argv.file)
process.exit(0)
})
.catch(error => {
} catch (error) {
console.error({ error })
process.exit(1)
})
}
} else {
console.log(
'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')
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()

View file

@ -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()