first commit
This commit is contained in:
30
services/web/scripts/example/script_for_migration.mjs
Normal file
30
services/web/scripts/example/script_for_migration.mjs
Normal file
@@ -0,0 +1,30 @@
|
||||
/* subscription.freeTrialExpiresAt
|
||||
* Example script for a migration:
|
||||
*
|
||||
* This script demonstrates how to write a script that is runnable either via
|
||||
* the CLI, or via a migration. The related migration is `script_example`
|
||||
* in the migrations directory.
|
||||
*/
|
||||
|
||||
import { User } from '../../app/src/models/User.js'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
// const somePackage = require('some-package')
|
||||
|
||||
const runScript = async () => {
|
||||
const user = await User.findOne({}, { first_name: 1 }).exec()
|
||||
const name = user ? user.first_name : 'World'
|
||||
console.log(`Hello ${name}!`)
|
||||
}
|
||||
|
||||
if (fileURLToPath(import.meta.url) === process.argv[1]) {
|
||||
try {
|
||||
await runScript()
|
||||
process.exit()
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
export default runScript
|
31
services/web/scripts/example/track_progress.mjs
Normal file
31
services/web/scripts/example/track_progress.mjs
Normal file
@@ -0,0 +1,31 @@
|
||||
// Import the script runner utility (adjust the path as needed)
|
||||
import { scriptRunner } from '../lib/ScriptRunner.mjs'
|
||||
|
||||
const subJobs = 30
|
||||
|
||||
/**
|
||||
* Your script's main work goes here.
|
||||
* It must be an async function and accept `trackProgress`.
|
||||
* @param {(message: string) => Promise<void>} trackProgress - Call this to log progress.
|
||||
*/
|
||||
async function main(trackProgress) {
|
||||
for (let i = 0; i < subJobs; i++) {
|
||||
await new Promise(resolve => setTimeout(() => resolve(), 1000))
|
||||
await trackProgress(`Job in progress ${i + 1}/${subJobs}`)
|
||||
}
|
||||
await trackProgress('Job finished')
|
||||
}
|
||||
|
||||
// Define any variables your script needs (optional)
|
||||
const scriptVariables = {
|
||||
subJobs,
|
||||
}
|
||||
|
||||
// --- Execute the script using the runner with async/await ---
|
||||
try {
|
||||
await scriptRunner(main, scriptVariables)
|
||||
process.exit()
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
process.exit(1)
|
||||
}
|
Reference in New Issue
Block a user