Skip to content

Commit

Permalink
Fix ESM error by dynamically importing ESM packages (chalk, terminal-…
Browse files Browse the repository at this point in the history
…link, etc.)
  • Loading branch information
ericallam committed Mar 12, 2023
1 parent 1673d45 commit 0b67b51
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-ligers-arrive.md
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---

Fix ESM error by dynamically importing ESM packages (chalk, terminal-link, etc.)
19 changes: 12 additions & 7 deletions packages/trigger-sdk/src/client.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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 "${
Expand Down Expand Up @@ -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})`
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/trigger-sdk/src/trigger/index.ts
@@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 0b67b51

Please sign in to comment.