|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import axios from "axios"; |
| 4 | +import inquirer from "inquirer"; |
| 5 | +import { installDependencies } from "../utils/installDependencies.js"; |
| 6 | + |
| 7 | +export async function updateCommand(projectPath: string) { |
| 8 | + const packageJsonPath = path.join(projectPath, "package.json"); |
| 9 | + |
| 10 | + if (!fs.existsSync(packageJsonPath)) { |
| 11 | + console.error("package.json not found in the current directory."); |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + const packageJsonData = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); |
| 16 | + const dependencies = packageJsonData.dependencies || {}; |
| 17 | + const devDependencies = packageJsonData.devDependencies || {}; |
| 18 | + |
| 19 | + const triggerPackages = Object.keys({ ...dependencies, ...devDependencies }).filter( |
| 20 | + (packageName) => packageName.startsWith("@trigger.dev/") |
| 21 | + ); |
| 22 | + |
| 23 | + if (triggerPackages.length === 0) { |
| 24 | + console.log("No @trigger.dev/* packages found in package.json."); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + const newVersions = await Promise.all( |
| 29 | + triggerPackages.map(async (packageName) => { |
| 30 | + try { |
| 31 | + const response = await axios.get(`https://registry.npmjs.org/${packageName}`); |
| 32 | + const latestVersion = response.data["dist-tags"].latest; |
| 33 | + return { packageName, latestVersion }; |
| 34 | + } catch (error) { |
| 35 | + // @ts-ignore |
| 36 | + console.error(`Error fetching version for ${packageName}: ${error.message}`); |
| 37 | + return null; |
| 38 | + } |
| 39 | + }) |
| 40 | + ); |
| 41 | + const packagesToUpdate = newVersions.filter( |
| 42 | + (entry) => entry !== null && entry.latestVersion !== dependencies[entry.packageName] |
| 43 | + ); |
| 44 | + |
| 45 | + if (packagesToUpdate.length === 0) { |
| 46 | + console.log("All @trigger.dev/* packages are up to date."); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + console.log("Newer versions found for the following packages:"); |
| 51 | + packagesToUpdate.forEach((entry) => { |
| 52 | + console.log( |
| 53 | + `- ${entry.packageName}: current ${dependencies[entry.packageName]} -> latest ${ |
| 54 | + entry.latestVersion |
| 55 | + }` |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + const { confirm } = await inquirer.prompt({ |
| 60 | + type: "confirm", |
| 61 | + name: "confirm", |
| 62 | + message: "Do you want to update these packages in package.json and re-install dependencies?", |
| 63 | + }); |
| 64 | + |
| 65 | + if (confirm) { |
| 66 | + packagesToUpdate.forEach((entry) => { |
| 67 | + dependencies[entry.packageName] = entry.latestVersion; |
| 68 | + }); |
| 69 | + |
| 70 | + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJsonData, null, 2)); |
| 71 | + |
| 72 | + console.log("package.json updated. Reinstalling dependencies..."); |
| 73 | + await installDependencies(projectPath); |
| 74 | + } else { |
| 75 | + console.log("Operation canceled. No changes were made."); |
| 76 | + } |
| 77 | +} |
0 commit comments