Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@types/jest": "^29.5.3",
"@types/node": "16",
"@types/node-fetch": "^2.6.2",
"@types/semver": "^7.3.13",
"rimraf": "^3.0.2",
"ts-jest": "^29.1.1",
"tsup": "^6.5.0",
Expand All @@ -58,6 +59,7 @@
"test": "vitest"
},
"dependencies": {
"@skarab/detect-package-manager": "^1.0.0",
"@types/degit": "^2.8.3",
"chalk": "^5.2.0",
"chokidar": "^3.5.3",
Expand All @@ -77,6 +79,7 @@
"path-to-regexp": "^6.2.1",
"posthog-node": "^3.1.1",
"proxy-agent": "^6.3.0",
"semver": "^7.5.0",
"simple-git": "^3.19.0",
"terminal-link": "^3.0.0",
"tsconfck": "^2.1.2",
Expand Down
77 changes: 76 additions & 1 deletion packages/cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import childProcess from "child_process";
import childProcess, { exec, spawnSync } from "child_process";
import chokidar from "chokidar";
import fs from "fs/promises";
import ngrok from "ngrok";
Expand All @@ -14,6 +14,12 @@ import { resolvePath } from "../utils/parseNameAndPath";
import { TriggerApi } from "../utils/triggerApi";
import { run as ncuRun } from 'npm-check-updates'
import chalk from "chalk";
import path from 'path';
import { createRequire } from 'node:module';
import { PackageFile } from "npm-check-updates/build/src/types/PackageFile";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not exported from the main index file? When you build the package I think this will fail

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matt-aitken
Built fine, since it's a type only import

image

import semver from 'semver'
import { detectAgent, Agent, AgentName } from '@skarab/detect-package-manager';
import inquirer from "inquirer";

const asyncExecFile = util.promisify(childProcess.execFile);

Expand Down Expand Up @@ -48,6 +54,7 @@ export async function devCommand(path: string, anyOptions: any) {

const resolvedPath = resolvePath(path);
await checkForOutdatedPackages(resolvedPath)
await detectProjectZodVersion(path)

// Read from package.json to get the endpointId
const endpointId = await getEndpointIdFromPackageJson(resolvedPath, options);
Expand Down Expand Up @@ -343,6 +350,74 @@ async function refreshEndpoint(apiClient: TriggerApi, endpointId: string, endpoi
}
}

const detectProjectZodVersion = async (projectPath: string) => {
try {
const require = createRequire(import.meta.url);
const sdkPath = require.resolve('@trigger.dev/sdk');

const sdkPackageJsonPath = path.join(sdkPath, '../../', 'package.json');
const sdkPackageJsonData = JSON.parse(await fs.readFile(sdkPackageJsonPath, 'utf8')) as PackageFile;

const sdkZodVersion = sdkPackageJsonData.dependencies!['zod']!

const projectPackageFilePath = `${projectPath}/package.json`;
const projectPackageFileData = JSON.parse(await fs.readFile(projectPackageFilePath, 'utf-8')) as PackageFile;

const projectZodVersion = projectPackageFileData.dependencies?.['zod']

if (!projectZodVersion) return

const agent = await detectAgent();

if (!semver.eq(sdkZodVersion, projectZodVersion!)) {
console.log(
chalk.bgYellow('Found zod version mismatch between project and @trigger.dev/sdk')
);

const { shouldContinue } = await inquirer.prompt<{
shouldContinue: boolean;
}>({
name: "shouldContinue",
type: "confirm",
message: `Would you like to automatically correct the version mismatch?`,
default: false,
});

if (!shouldContinue) return

projectPackageFileData.dependencies!['zod'] = sdkZodVersion;
await fs.writeFile(projectPackageFilePath, JSON.stringify(projectPackageFileData, null, 2));

let installCommand;
switch (agent?.name) {
case 'npm':
installCommand = 'npm install';
break;
case 'yarn':
installCommand = 'yarn';
break;
case 'pnpm':
installCommand = 'pnpm install';
break;
case 'bun':
installCommand = 'bun install';
break;
default:
console.error(`Unsupported package manager: ${agent?.name}`);
return;
}

console.log(
chalk.bgBlue(`Completed. Please execute '${installCommand}' to apply changes.`)
);

}

} catch (error) {
return null;
}
}

//wait function
async function wait(ms: number) {
return new Promise((resolve) => {
Expand Down
Loading