Skip to content

Commit 859425f

Browse files
author
Rishi Raj Jain
authored
Update update.ts
1 parent 3159619 commit 859425f

File tree

1 file changed

+67
-25
lines changed

1 file changed

+67
-25
lines changed

packages/cli/src/commands/update.ts

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,117 @@
1+
import z from "zod";
12
import fs from "fs";
23
import path from "path";
34
import fetch from "node-fetch";
45
import inquirer from "inquirer";
6+
import { spawnSync } from "child_process";
57
import { installDependencies } from "../utils/installDependencies.js";
8+
import { getUserPackageManager } from "../utils/getUserPkgManager.js";
9+
10+
interface YarnListOutput {
11+
data: {
12+
trees: Array<{ name: string; version: string }>;
13+
};
14+
}
15+
16+
function getInstalledVersion(packageName: string, packageManager: string, projectPath: string) {
17+
let installedVersion = null;
18+
if (packageManager === "npm" || packageManager === "pnpm") {
19+
const { stdout } = spawnSync(packageManager, ["list", packageName, "--json", "--depth=0"], {
20+
cwd: projectPath,
21+
});
22+
installedVersion = JSON.parse(stdout.toString()).dependencies[packageName].version;
23+
} else if (packageManager === "yarn") {
24+
const { stdout } = spawnSync(packageManager, ["list", "--json", "--depth=0"], {
25+
cwd: projectPath,
26+
});
27+
const parsedOutput: YarnListOutput = JSON.parse(stdout.toString());
28+
if (Array.isArray(parsedOutput.data.trees)) {
29+
const tree = parsedOutput.data.trees.find((tree) => tree.name === packageName);
30+
if (tree) {
31+
installedVersion = tree.version;
32+
}
33+
}
34+
}
35+
return installedVersion;
36+
}
637

738
export async function updateCommand(projectPath: string) {
39+
const triggerDevPackage = "@trigger.dev";
40+
const packageManager = await getUserPackageManager(projectPath);
841
const packageJsonPath = path.join(projectPath, "package.json");
9-
42+
// In case no package.json found
1043
if (!fs.existsSync(packageJsonPath)) {
11-
console.error("package.json not found in the current directory.");
44+
console.error(`package.json not found in the ${projectPath} directory.`);
1245
return;
1346
}
14-
1547
const packageJsonData = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
1648
const dependencies = packageJsonData.dependencies || {};
1749
const devDependencies = packageJsonData.devDependencies || {};
18-
19-
const triggerPackages = Object.keys({ ...dependencies, ...devDependencies }).filter(
20-
(packageName) => packageName.startsWith("@trigger.dev/")
21-
);
22-
50+
const allDependencies = Object.keys({ ...dependencies, ...devDependencies });
51+
const triggerPackages = allDependencies.filter((pkg) => pkg.startsWith(triggerDevPackage));
52+
// If there are no @trigger.dev packages
2353
if (triggerPackages.length === 0) {
2454
console.log("No @trigger.dev/* packages found in package.json.");
2555
return;
2656
}
27-
57+
// Get an array of the latest versions of @trigger.dev packages
2858
const newVersions = await Promise.all(
2959
triggerPackages.map(async (packageName) => {
3060
try {
61+
const installedVersion = getInstalledVersion(packageName, packageManager, projectPath);
3162
const response = await fetch(`https://registry.npmjs.org/${packageName}`);
32-
const data = await response.json();
33-
const latestVersion = data["dist-tags"].latest;
34-
return { packageName, latestVersion };
63+
if (response.ok) {
64+
const data = await response.json();
65+
const schema = z.object({
66+
"dist-tags": z.object({
67+
latest: z.string(),
68+
}),
69+
});
70+
const parsed = schema.parse(data);
71+
return { packageName, installedVersion, latestVersion: parsed["dist-tags"].latest };
72+
}
73+
return null;
3574
} catch (error) {
3675
// @ts-ignore
3776
console.error(`Error fetching version for ${packageName}: ${error.message}`);
3877
return null;
3978
}
4079
})
4180
);
81+
// Filter the packages with null and what don't match what
82+
// they are installed with so that they can be updated
4283
const packagesToUpdate = newVersions.filter(
43-
(entry) => entry !== null && entry.latestVersion !== dependencies[entry.packageName]
84+
(pkg) => pkg && pkg.latestVersion !== pkg.installedVersion
4485
);
45-
86+
// If no packages require any updation
4687
if (packagesToUpdate.length === 0) {
4788
console.log("All @trigger.dev/* packages are up to date.");
4889
return;
4990
}
50-
91+
// Inform the user of the dependencies that can be updated
5192
console.log("Newer versions found for the following packages:");
5293
packagesToUpdate.forEach((entry) => {
53-
console.log(
54-
`- ${entry.packageName}: current ${dependencies[entry.packageName]} -> latest ${
55-
entry.latestVersion
56-
}`
57-
);
94+
if (entry) {
95+
console.log(
96+
`- ${entry.packageName}: current ${dependencies[entry.packageName]} -> latest ${
97+
entry.latestVersion
98+
}`
99+
);
100+
}
58101
});
59-
102+
// Ask the user if they want to update the dependencies
60103
const { confirm } = await inquirer.prompt({
61104
type: "confirm",
62105
name: "confirm",
63106
message: "Do you want to update these packages in package.json and re-install dependencies?",
64107
});
65-
66108
if (confirm) {
67109
packagesToUpdate.forEach((entry) => {
68-
dependencies[entry.packageName] = entry.latestVersion;
110+
if (entry) {
111+
dependencies[entry.packageName] = entry.latestVersion;
112+
}
69113
});
70-
71114
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJsonData, null, 2));
72-
73115
console.log("package.json updated. Reinstalling dependencies...");
74116
await installDependencies(projectPath);
75117
} else {

0 commit comments

Comments
 (0)