Fix ESM error by dynamically importing ESM packages (chalk, terminal-link, etc.)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@trigger.dev/sdk": patch
|
||||
---
|
||||
|
||||
Fix ESM error by dynamically importing ESM packages (chalk, terminal-link, etc.)
|
||||
@@ -15,10 +15,6 @@ import { ContextLogger } from "./logger";
|
||||
import { Trigger, TriggerOptions } from "./trigger";
|
||||
import { TriggerContext, TriggerFetch } from "./types";
|
||||
import { generateErrorMessage, ErrorMessageOptions } from "zod-error";
|
||||
import terminalLink from "terminal-link";
|
||||
import chalk from "chalk";
|
||||
import getRepoInfo from "git-repo-info";
|
||||
import gitRemoteOriginUrl from "git-remote-origin-url";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import {
|
||||
ContextKeyValueStorage,
|
||||
@@ -152,6 +148,9 @@ export class TriggerClient<TSchema extends z.ZodTypeAny> {
|
||||
this.#initializeRPC();
|
||||
await this.#initializeHost();
|
||||
|
||||
// async import terminalLink to avoid ESM error
|
||||
const terminalLink = (await import("terminal-link")).default;
|
||||
|
||||
if (this.#registerResponse?.isNew) {
|
||||
this.#logger.logClean(
|
||||
`🎉 Successfully registered "${
|
||||
@@ -214,6 +213,8 @@ export class TriggerClient<TSchema extends z.ZodTypeAny> {
|
||||
return;
|
||||
}
|
||||
|
||||
const chalk = (await import("chalk")).default;
|
||||
|
||||
this.#logger.error(
|
||||
`${chalk.red("error")} Could not connect to trigger.dev${
|
||||
reason ? `: ${reason}` : `(code ${code})`
|
||||
@@ -774,6 +775,8 @@ export class TriggerClient<TSchema extends z.ZodTypeAny> {
|
||||
|
||||
this.#logger.debug("Parsed event data", eventData);
|
||||
|
||||
const terminalLink = (await import("terminal-link")).default;
|
||||
|
||||
triggerRunLocalStorage.run(
|
||||
{
|
||||
performRequest: async (key, options) => {
|
||||
@@ -928,7 +931,7 @@ export class TriggerClient<TSchema extends z.ZodTypeAny> {
|
||||
throw new Error("Cannot initialize host without an RPC connection");
|
||||
}
|
||||
|
||||
const repoInfo = safeGetRepoInfo();
|
||||
const repoInfo = await safeGetRepoInfo();
|
||||
const remoteUrl = repoInfo
|
||||
? await getRemoteUrl(repoInfo.commonGitDir)
|
||||
: undefined;
|
||||
@@ -1052,15 +1055,17 @@ async function getTriggerPackageEnvVars(
|
||||
|
||||
async function getRemoteUrl(cwd: string) {
|
||||
try {
|
||||
const gitRemoteOriginUrl = (await import("git-remote-origin-url")).default;
|
||||
return await gitRemoteOriginUrl({ cwd });
|
||||
} catch (err) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function safeGetRepoInfo() {
|
||||
async function safeGetRepoInfo() {
|
||||
try {
|
||||
return getRepoInfo();
|
||||
const gitRepoInfo = (await import("git-repo-info")).default;
|
||||
return gitRepoInfo();
|
||||
} catch (err) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { TriggerClient } from "../client";
|
||||
import { LogLevel } from "internal-bridge";
|
||||
import { TriggerEvent } from "../events";
|
||||
import chalk from "chalk";
|
||||
|
||||
import type { TriggerContext } from "../types";
|
||||
import { z } from "zod";
|
||||
import terminalLink from "terminal-link";
|
||||
|
||||
export type TriggerOptions<TSchema extends z.ZodTypeAny> = {
|
||||
id: string;
|
||||
@@ -35,6 +32,9 @@ export class Trigger<TSchema extends z.ZodTypeAny> {
|
||||
async listen() {
|
||||
const apiKey = this.#getApiKey();
|
||||
|
||||
const chalk = (await import("chalk")).default;
|
||||
const terminalLink = (await import("terminal-link")).default;
|
||||
|
||||
if (apiKey.status === "invalid") {
|
||||
console.log(
|
||||
`${chalk.red("Trigger.dev error")}: ${chalk.bold(
|
||||
|
||||
Reference in New Issue
Block a user