From 408bd6ddc844a146265487135eca7006777a8e85 Mon Sep 17 00:00:00 2001 From: Eric Allam Date: Thu, 2 Mar 2023 15:34:53 +0000 Subject: [PATCH] Added a script to automatically update template repo @trigger.dev packages instead of having to do it manually --- package.json | 3 +- pnpm-lock.yaml | 6 +- scripts/updateTemplateDependencies.js | 113 ++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 scripts/updateTemplateDependencies.js diff --git a/package.json b/package.json index e69136abc..857ef122a 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ }, "packageManager": "pnpm@7.13.5", "dependencies": { - "@changesets/cli": "^2.26.0" + "@changesets/cli": "^2.26.0", + "node-fetch": "2.6.x" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c2ef2f018..fa33d1d85 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -10,6 +10,7 @@ importers: '@tailwindcss/typography': ^0.5.8 autoprefixer: ^10.4.12 eslint-config-custom: workspace:* + node-fetch: 2.6.x postcss: ^8.4.17 prettier: ^2.5.1 tailwindcss: 3.1.8 @@ -20,6 +21,7 @@ importers: vitest: ^0.28.4 dependencies: '@changesets/cli': 2.26.0 + node-fetch: 2.6.7 devDependencies: '@manypkg/cli': 0.19.2 '@tailwindcss/forms': 0.5.3_tailwindcss@3.1.8 @@ -6276,7 +6278,7 @@ packages: tailwindcss: '>=3.0.0 || >= 3.0.0-alpha.1' dependencies: mini-svg-data-uri: 1.4.4 - tailwindcss: 3.1.8_aesdjsunmf4wiehhujt67my7tu + tailwindcss: 3.1.8_postcss@8.4.21 dev: true /@tailwindcss/typography/0.5.9_tailwindcss@3.1.8: @@ -6288,7 +6290,7 @@ packages: lodash.isplainobject: 4.0.6 lodash.merge: 4.6.2 postcss-selector-parser: 6.0.10 - tailwindcss: 3.1.8_aesdjsunmf4wiehhujt67my7tu + tailwindcss: 3.1.8_postcss@8.4.21 dev: true /@tanstack/react-table/8.7.6_biqbaboplfbrettd7655fr4n2y: diff --git a/scripts/updateTemplateDependencies.js b/scripts/updateTemplateDependencies.js new file mode 100644 index 000000000..3a876e2a8 --- /dev/null +++ b/scripts/updateTemplateDependencies.js @@ -0,0 +1,113 @@ +const { exec } = require("child_process"); +const fs = require("fs"); +const path = require("path"); +const fetch = require("node-fetch"); + +const templatesDir = process.argv[2]; +if (!templatesDir) { + console.error("Please provide a path to the templates directory"); + process.exit(1); +} + +async function updateTemplate(templateName) { + const templateDir = path.join(templatesDir, templateName); + + if (fs.statSync(templateDir).isDirectory()) { + console.log(`Updating dependencies for template '${templateName}'`); + + // Make sure we're on the main branch and there are no uncommitted changes + await execAsync(`cd ${templateDir} && git checkout main`); + + // Make sure there are no uncommitted changes + const preStatus = await execAsync(`cd ${templateDir} && git status`); + + if (!preStatus.includes("nothing to commit")) { + console.error( + `There are uncommitted changes in template '${templateName}'` + ); + return; + } + + // Make sure we're up to date with the remote + await execAsync(`cd ${templateDir} && git pull origin main`); + + // Find all the dependencies that start with @trigger.dev/ + const packageJson = JSON.parse( + fs.readFileSync(path.join(templateDir, "package.json")) + ); + + const dependencies = Object.keys(packageJson.dependencies).filter((dep) => + dep.startsWith("@trigger.dev/") + ); + + if (!dependencies) { + console.error(`No dependencies defined for template '${templateName}'`); + return; + } + + const npmInstallCommand = `npm install ${dependencies + .map((dep) => `${dep}@latest`) + .join(" ")}`; + + console.log(`Attempting ${npmInstallCommand}`); + + await execAsync( + `cd ${templateDir} && npm install ${dependencies + .map((dep) => `${dep}@latest`) + .join(" ")}` + ); + + // Check if there are any changes + const status = await execAsync(`cd ${templateDir} && git status`); + + if (!status.includes("modified:")) { + console.log(`No changes for template '${templateName}'`); + return; + } + + console.log(`Changes for template '${templateName}':`, status); + + // Create a commit + await execAsync( + `cd ${templateDir} && git add package.json package-lock.json && git commit -m "[triggerbot] Updated packages ${dependencies.join( + ", " + )}"` + ); + + // Push to remote + await execAsync(`cd ${templateDir} && git push`); + + console.log(`Updated dependencies for template '${templateName}'`); + } else { + console.log(`Skipping '${templateName}'`); + } +} + +async function execAsync(command) { + return new Promise((resolve, reject) => { + exec(command, (error, stdout, stderr) => { + if (error) { + reject(error); + } else { + resolve(stdout); + } + }); + }); +} + +async function main() { + // Make a JSON request to https://app.trigger.dev/api/v1/templates and parse the response as an array of templates + const templates = await fetch( + "https://app.trigger.dev/api/v1/templates" + ).then((res) => res.json()); + + for (const template of templates) { + try { + await updateTemplate(template.slug); + } catch (error) { + console.log(`Failed to update template '${template}'`, error); + } + } +} + +main().catch(console.error);