|
| 1 | +const { exec } = require("child_process"); |
| 2 | +const fs = require("fs"); |
| 3 | +const path = require("path"); |
| 4 | +const fetch = require("node-fetch"); |
| 5 | + |
| 6 | +const templatesDir = process.argv[2]; |
| 7 | +if (!templatesDir) { |
| 8 | + console.error("Please provide a path to the templates directory"); |
| 9 | + process.exit(1); |
| 10 | +} |
| 11 | + |
| 12 | +// patch is a path to a patch file |
| 13 | +async function patchTemplate(templateName, patch, message) { |
| 14 | + const templateDir = path.join(templatesDir, templateName); |
| 15 | + |
| 16 | + if (fs.statSync(templateDir).isDirectory()) { |
| 17 | + console.log(`Patching template '${templateName}'`); |
| 18 | + // Make sure we're on the main branch and there are no uncommitted changes |
| 19 | + await execAsync(`cd ${templateDir} && git checkout main`); |
| 20 | + |
| 21 | + // Make sure there are no uncommitted changes |
| 22 | + const preStatus = await execAsync(`cd ${templateDir} && git status`); |
| 23 | + |
| 24 | + if (!preStatus.includes("nothing to commit")) { |
| 25 | + console.error( |
| 26 | + `There are uncommitted changes in template '${templateName}'` |
| 27 | + ); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + // Make sure we're up to date with the remote |
| 32 | + await execAsync(`cd ${templateDir} && git pull origin main`); |
| 33 | + |
| 34 | + // Apply the patch to the template repo |
| 35 | + await execAsync(`cd ${templateDir} && git apply ${patch}`); |
| 36 | + |
| 37 | + // Check if there are any changes |
| 38 | + const status = await execAsync(`cd ${templateDir} && git status`); |
| 39 | + |
| 40 | + if (!status.includes("modified:")) { |
| 41 | + console.log(`No changes for template '${templateName}'`); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + console.log(`Changes for template '${templateName}':`, status); |
| 46 | + |
| 47 | + // Create a commit |
| 48 | + await execAsync( |
| 49 | + `cd ${templateDir} && git add -A && git commit -m "[triggerbot] ${message}"` |
| 50 | + ); |
| 51 | + |
| 52 | + // Push to remote |
| 53 | + await execAsync(`cd ${templateDir} && git push origin main`); |
| 54 | + |
| 55 | + console.log(`Applied patch ${patch} to template '${templateName}'`); |
| 56 | + } else { |
| 57 | + console.log(`Skipping '${templateName}'`); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +async function execAsync(command) { |
| 62 | + return new Promise((resolve, reject) => { |
| 63 | + exec(command, (error, stdout, stderr) => { |
| 64 | + if (error) { |
| 65 | + reject(error); |
| 66 | + } else { |
| 67 | + resolve(stdout); |
| 68 | + } |
| 69 | + }); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +// called like so: node scripts/patchTemplates.js /path/to/templates patchFile.patch "Patch message" |
| 74 | +async function main() { |
| 75 | + // Make a JSON request to https://app.trigger.dev/api/v1/templates and parse the response as an array of templates |
| 76 | + const templates = await fetch( |
| 77 | + "https://app.trigger.dev/api/v1/templates" |
| 78 | + ).then((res) => res.json()); |
| 79 | + |
| 80 | + for (const template of templates) { |
| 81 | + try { |
| 82 | + await patchTemplate(template.slug, process.argv[3], process.argv[4]); |
| 83 | + } catch (error) { |
| 84 | + console.log(`Failed to update template '${template.slug}'`, error); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +main().catch(console.error); |
0 commit comments