Skip to content

Commit 408bd6d

Browse files
committed
Added a script to automatically update template repo @trigger.dev packages instead of having to do it manually
1 parent 1789d9d commit 408bd6d

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
},
6767
"packageManager": "pnpm@7.13.5",
6868
"dependencies": {
69-
"@changesets/cli": "^2.26.0"
69+
"@changesets/cli": "^2.26.0",
70+
"node-fetch": "2.6.x"
7071
}
7172
}

pnpm-lock.yaml

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
async function updateTemplate(templateName) {
13+
const templateDir = path.join(templatesDir, templateName);
14+
15+
if (fs.statSync(templateDir).isDirectory()) {
16+
console.log(`Updating dependencies for template '${templateName}'`);
17+
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+
// Find all the dependencies that start with @trigger.dev/
35+
const packageJson = JSON.parse(
36+
fs.readFileSync(path.join(templateDir, "package.json"))
37+
);
38+
39+
const dependencies = Object.keys(packageJson.dependencies).filter((dep) =>
40+
dep.startsWith("@trigger.dev/")
41+
);
42+
43+
if (!dependencies) {
44+
console.error(`No dependencies defined for template '${templateName}'`);
45+
return;
46+
}
47+
48+
const npmInstallCommand = `npm install ${dependencies
49+
.map((dep) => `${dep}@latest`)
50+
.join(" ")}`;
51+
52+
console.log(`Attempting ${npmInstallCommand}`);
53+
54+
await execAsync(
55+
`cd ${templateDir} && npm install ${dependencies
56+
.map((dep) => `${dep}@latest`)
57+
.join(" ")}`
58+
);
59+
60+
// Check if there are any changes
61+
const status = await execAsync(`cd ${templateDir} && git status`);
62+
63+
if (!status.includes("modified:")) {
64+
console.log(`No changes for template '${templateName}'`);
65+
return;
66+
}
67+
68+
console.log(`Changes for template '${templateName}':`, status);
69+
70+
// Create a commit
71+
await execAsync(
72+
`cd ${templateDir} && git add package.json package-lock.json && git commit -m "[triggerbot] Updated packages ${dependencies.join(
73+
", "
74+
)}"`
75+
);
76+
77+
// Push to remote
78+
await execAsync(`cd ${templateDir} && git push`);
79+
80+
console.log(`Updated dependencies for template '${templateName}'`);
81+
} else {
82+
console.log(`Skipping '${templateName}'`);
83+
}
84+
}
85+
86+
async function execAsync(command) {
87+
return new Promise((resolve, reject) => {
88+
exec(command, (error, stdout, stderr) => {
89+
if (error) {
90+
reject(error);
91+
} else {
92+
resolve(stdout);
93+
}
94+
});
95+
});
96+
}
97+
98+
async function main() {
99+
// Make a JSON request to https://app.trigger.dev/api/v1/templates and parse the response as an array of templates
100+
const templates = await fetch(
101+
"https://app.trigger.dev/api/v1/templates"
102+
).then((res) => res.json());
103+
104+
for (const template of templates) {
105+
try {
106+
await updateTemplate(template.slug);
107+
} catch (error) {
108+
console.log(`Failed to update template '${template}'`, error);
109+
}
110+
}
111+
}
112+
113+
main().catch(console.error);

0 commit comments

Comments
 (0)