From a42056e4f57cf0f8b8f79bb9ce22adebb454d7be Mon Sep 17 00:00:00 2001 From: yu-i-i Date: Sat, 14 Mar 2026 05:07:40 +0100 Subject: [PATCH] Sandboxed compiles: add scripts to add/strip repo prefix from texlive images --- .../scripts/add-image-repo-prefix.mjs | 88 +++++++++++++++++++ .../scripts/strip-image-repo-prefix.mjs | 85 ++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 services/web/modules/server-ce-scripts/scripts/add-image-repo-prefix.mjs create mode 100644 services/web/modules/server-ce-scripts/scripts/strip-image-repo-prefix.mjs diff --git a/services/web/modules/server-ce-scripts/scripts/add-image-repo-prefix.mjs b/services/web/modules/server-ce-scripts/scripts/add-image-repo-prefix.mjs new file mode 100644 index 0000000000..c13a3a67e1 --- /dev/null +++ b/services/web/modules/server-ce-scripts/scripts/add-image-repo-prefix.mjs @@ -0,0 +1,88 @@ +import minimist from 'minimist' +import { db } from '../../../app/src/infrastructure/mongodb.mjs' +import { fileURLToPath } from 'url' + +const filename = fileURLToPath(import.meta.url) + +export default async function main() { + const argv = minimist(process.argv.slice(2), { + boolean: ['dry-run'], + default: { 'dry-run': true }, + string: ['prefix'], + }) + + const dryRun = argv['dry-run'] + const prefix = argv.prefix + + if (!prefix) { + console.error(`Usage: node ${filename} --prefix= [--no-dry-run]`) + process.exit(1) + } + + const normalizedPrefix = prefix.replace(/\/+$/, '') + const collections = ['projects', 'templates'] + + for (const collectionName of collections) { + const collection = db[collectionName] + + console.log(`\nProcessing collection: ${collectionName}`) + + const rows = [] + const cursor = collection.find({}) + + for await (const doc of cursor) { + const oldName = doc.imageName + if (typeof oldName !== 'string') continue + if (oldName.includes('/')) continue + + const newName = `${normalizedPrefix}/${oldName}` + + rows.push({ + id: String(doc._id), + old: oldName, + new: newName, + doc, + }) + } + + const idWidth = Math.max(...rows.map(r => r.id.length), 2) + const oldWidth = Math.max(...rows.map(r => r.old.length), 3) + + let count = 0 + + for (const r of rows) { + count++ + + console.log( + `_id: ${r.id.padEnd(idWidth)} ${r.old.padEnd(oldWidth)} -> ${r.new}` + ) + + if (!dryRun) { + await collection.updateOne( + { _id: r.doc._id }, + { $set: { imageName: r.new } } + ) + } + } + + console.log( + `Total entries ${dryRun ? 'to be changed' : 'changed'} in ${collectionName}: ${count}` + ) + } + + console.log( + dryRun + ? '\nDry-run complete. No changes made. Use --no-dry-run to apply updates.' + : '\nUpdate complete.' + ) +} + +if (filename === process.argv[1]) { + try { + await main() + process.exit(0) + } catch (error) { + console.error({ error }) + process.exit(1) + } +} diff --git a/services/web/modules/server-ce-scripts/scripts/strip-image-repo-prefix.mjs b/services/web/modules/server-ce-scripts/scripts/strip-image-repo-prefix.mjs new file mode 100644 index 0000000000..d4f6808185 --- /dev/null +++ b/services/web/modules/server-ce-scripts/scripts/strip-image-repo-prefix.mjs @@ -0,0 +1,85 @@ +import minimist from 'minimist' +import { db } from '../../../app/src/infrastructure/mongodb.mjs' +import { fileURLToPath } from 'url' + +const filename = fileURLToPath(import.meta.url) + +function stripRepoPrefix(name) { + if (typeof name !== 'string') return '' + const parts = name.split('/') + return parts[parts.length - 1] +} + +export default async function main() { + const argv = minimist(process.argv.slice(2), { + boolean: ['dry-run'], + default: { 'dry-run': true }, + }) + + const dryRun = argv['dry-run'] + const collections = ['projects', 'templates'] + + for (const collectionName of collections) { + const collection = db[collectionName] + + console.log(`\nProcessing collection: ${collectionName}`) + + const rows = [] + const cursor = collection.find({ imageName: { $regex: '/' } }) + + for await (const doc of cursor) { + const oldName = doc.imageName + if (typeof oldName !== 'string') continue + + const newName = stripRepoPrefix(oldName) + if (oldName === newName) continue + + rows.push({ + id: String(doc._id), + old: oldName, + new: newName, + doc, + }) + } + + const idWidth = Math.max(...rows.map(r => r.id.length), 2) + const oldWidth = Math.max(...rows.map(r => r.old.length), 3) + + let count = 0 + + for (const r of rows) { + count++ + + console.log( + `_id: ${r.id.padEnd(idWidth)} ${r.old.padEnd(oldWidth)} -> ${r.new}` + ) + + if (!dryRun) { + await collection.updateOne( + { _id: r.doc._id }, + { $set: { imageName: r.new } } + ) + } + } + + console.log( + `Total entries ${dryRun ? 'to be changed' : 'changed'} in ${collectionName}: ${count}` + ) + } + + console.log( + dryRun + ? '\nDry-run complete. No changes made. Use --no-dry-run to apply updates.' + : '\nUpdate complete.' + ) +} + +if (filename === process.argv[1]) { + try { + await main() + process.exit(0) + } catch (error) { + console.error({ error }) + process.exit(1) + } +}