Skip to content

Commit 8c27236

Browse files
committed
Merge branch 'rishi-raj-jain-main'
2 parents fef1670 + b8d1910 commit 8c27236

File tree

6 files changed

+1266
-47
lines changed

6 files changed

+1266
-47
lines changed

.changeset/swift-eagles-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/cli": patch
3+
---
4+
5+
fix: Add an update sub-command the @trigger.dev/cli that updates all @trigger.dev/* packages

docs/documentation/guides/cli.mdx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,43 @@ yarn dlx @trigger.dev/cli@latest dev
109109
You can optionally pass the hostname if you're not running on localhost by adding
110110
`--hostname <host>`. Example, in case your Next.js is running on 0.0.0.0: `--hostname 0.0.0.0`.
111111
</Note>
112+
113+
## update Command
114+
115+
The `update` command will update all Trigger.dev packages to the latest version.
116+
117+
<CodeGroup>
118+
119+
```bash npm
120+
npx @trigger.dev/cli@latest update
121+
```
122+
123+
```bash pnpm
124+
pnpm dlx @trigger.dev/cli@latest update
125+
```
126+
127+
```bash yarn
128+
yarn dlx @trigger.dev/cli@latest update
129+
```
130+
131+
</CodeGroup>
132+
133+
## whoami Command
134+
135+
The `whoami` command will print out information about your current Trigger.dev project and environment, based on the API key found in your `.env` or `.env.local` file
136+
137+
<CodeGroup>
138+
139+
```bash npm
140+
npx @trigger.dev/cli@latest whoami
141+
```
142+
143+
```bash pnpm
144+
pnpm dlx @trigger.dev/cli@latest whoami
145+
```
146+
147+
```bash yarn
148+
yarn dlx @trigger.dev/cli@latest whoami
149+
```
150+
151+
</CodeGroup>

packages/cli/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
"trigger-cli": "./dist/index.js"
3636
},
3737
"devDependencies": {
38-
"@trigger.dev/tsconfig": "workspace:*",
3938
"@gmrchk/cli-testing-library": "^0.1.2",
39+
"@trigger.dev/tsconfig": "workspace:*",
4040
"@types/gradient-string": "^1.1.2",
4141
"@types/inquirer": "^9.0.3",
4242
"@types/jest": "^29.5.3",
@@ -71,6 +71,7 @@
7171
"nanoid": "^4.0.2",
7272
"ngrok": "5.0.0-beta.2",
7373
"node-fetch": "^3.3.0",
74+
"npm-check-updates": "^16.12.2",
7475
"openai": "^3.3.0",
7576
"ora": "^6.1.2",
7677
"path-to-regexp": "^6.2.1",

packages/cli/src/cli/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { initCommand } from "../commands/init";
88
import { CLOUD_TRIGGER_URL, COMMAND_NAME } from "../consts";
99
import { telemetryClient } from "../telemetry/telemetry";
1010
import { getVersion } from "../utils/getVersion";
11+
import { updateCommand } from "../commands/update";
1112

1213
export const program = new Command();
1314

@@ -80,6 +81,14 @@ program
8081
await createIntegrationCommand(path, options);
8182
});
8283

84+
program
85+
.command("update")
86+
.description("Updates all @trigger.dev/* packages to their latest compatible versions")
87+
.argument("[path]", "The path to the directory that contains the package.json file", ".")
88+
.action(async (path) => {
89+
await updateCommand(path);
90+
});
91+
8392
program
8493
.command("whoami")
8594
.description("display the current logged in user and project details")

packages/cli/src/commands/update.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import path from "path";
2+
import inquirer from "inquirer";
3+
import { run, RunOptions } from "npm-check-updates";
4+
import { installDependencies } from "../utils/installDependencies.js";
5+
import { readJSONFileSync, writeJSONFile } from "../utils/fileSystem.js";
6+
import { logger } from "../utils/logger.js";
7+
8+
export async function updateCommand(projectPath: string) {
9+
const triggerDevPackage = "@trigger.dev";
10+
const packageJSONPath = path.join(projectPath, "package.json");
11+
const packageData = readJSONFileSync(packageJSONPath);
12+
13+
if (!packageData) {
14+
return;
15+
}
16+
17+
const packageMaps: { [k: string]: { type: string; version: string } } = {};
18+
const packageDependencies = packageData.dependencies || {};
19+
const packageDevDependencies = packageData.devDependencies || {};
20+
Object.keys(packageDependencies).forEach((i) => {
21+
packageMaps[i] = { type: "dependencies", version: packageDependencies[i] };
22+
});
23+
Object.keys(packageDevDependencies).forEach((i) => {
24+
packageMaps[i] = {
25+
type: "devDependencies",
26+
version: packageDevDependencies[i],
27+
};
28+
});
29+
30+
// Use npm-check-updates to get updated dependency versions
31+
const ncuOptions: RunOptions = {
32+
packageData,
33+
upgrade: true,
34+
jsonUpgraded: true,
35+
target: "latest",
36+
};
37+
38+
// Can either give a json like package.json or just with deps and their new versions
39+
const updatedDependencies: { [k: string]: any } | void = await run(ncuOptions);
40+
41+
if (!updatedDependencies) return;
42+
43+
const ifUpdatedDependenciesIsPackageJSON =
44+
updatedDependencies.hasOwnProperty("dependencies") ||
45+
updatedDependencies.hasOwnProperty("devDependencies");
46+
47+
const dependencies = updatedDependencies.dependencies || {};
48+
const devDependencies = updatedDependencies.devDependencies || {};
49+
50+
const allDependencies = ifUpdatedDependenciesIsPackageJSON
51+
? Object.keys({ ...dependencies, ...devDependencies })
52+
: Object.keys(updatedDependencies);
53+
54+
const triggerPackages = allDependencies.filter((pkg) => pkg.startsWith(triggerDevPackage));
55+
56+
// If there are no @trigger.dev packages
57+
if (triggerPackages.length === 0) {
58+
logger.success(`✅ All @trigger.dev/* packages are up to date.`);
59+
return;
60+
}
61+
62+
// Filter the packages with null and what don't match what
63+
// they are installed with so that they can be updated
64+
const packagesToUpdate = triggerPackages.filter((pkg: string) => updatedDependencies[pkg]);
65+
66+
// If no packages require any updation
67+
if (packagesToUpdate.length === 0) {
68+
logger.success(`✅ All @trigger.dev/* packages are up to date.`);
69+
return;
70+
}
71+
72+
// Inform the user of the dependencies that can be updated
73+
console.log("\nNewer versions found for the following packages:");
74+
console.table(
75+
packagesToUpdate.map((i) => ({
76+
name: i,
77+
old: packageMaps[i]?.version,
78+
new: updatedDependencies[i],
79+
}))
80+
);
81+
82+
// Ask the user if they want to update the dependencies
83+
const { confirm } = await inquirer.prompt({
84+
type: "confirm",
85+
name: "confirm",
86+
message: "Do you want to update these packages in package.json and re-install dependencies?",
87+
});
88+
89+
if (confirm) {
90+
const newPackageJSON = packageData;
91+
packagesToUpdate.forEach((packageName) => {
92+
const tmp = packageMaps[packageName];
93+
if (tmp) {
94+
newPackageJSON[tmp.type][packageName] = updatedDependencies[packageName];
95+
}
96+
});
97+
await writeJSONFile(packageJSONPath, newPackageJSON);
98+
await installDependencies(projectPath);
99+
}
100+
}

0 commit comments

Comments
 (0)