Skip to content

Commit 3804df8

Browse files
committed
init.ts at the root of the trigger dir is now automatically loaded
1 parent 9ab734d commit 3804df8

File tree

5 files changed

+50
-34
lines changed

5 files changed

+50
-34
lines changed

packages/cli-v3/src/build/bundle.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ export async function getBundleResultFromBuild(
236236
? relative(resolvedConfig.workingDir, resolvedConfig.configFile)
237237
: "trigger.config.ts";
238238

239+
// Check if the entry point is an init.ts file at the root of a trigger directory
240+
function isInitEntryPoint(entryPoint: string): boolean {
241+
const normalizedEntryPoint = entryPoint.replace(/\\/g, "/"); // Normalize path separators
242+
const initFileName = "init.ts";
243+
244+
// Check if it's directly in one of the trigger directories
245+
return resolvedConfig.dirs.some((dir) => {
246+
const normalizedDir = dir.replace(/\\/g, "/");
247+
return normalizedEntryPoint === `${normalizedDir}/${initFileName}`;
248+
});
249+
}
250+
239251
for (const [outputPath, outputMeta] of Object.entries(result.metafile.outputs)) {
240252
if (outputPath.endsWith(".mjs")) {
241253
const $outputPath = resolve(workingDir, outputPath);
@@ -256,7 +268,7 @@ export async function getBundleResultFromBuild(
256268
indexControllerEntryPoint = $outputPath;
257269
} else if (isIndexWorkerForTarget(outputMeta.entryPoint, target)) {
258270
indexWorkerEntryPoint = $outputPath;
259-
} else if (outputMeta.entryPoint.endsWith("init.ts")) {
271+
} else if (isInitEntryPoint(outputMeta.entryPoint)) {
260272
initEntryPoint = $outputPath;
261273
} else {
262274
if (
@@ -362,6 +374,7 @@ export async function createBuildManifestFromBundle({
362374
runControllerEntryPoint: bundle.runControllerEntryPoint ?? getRunControllerForTarget(target),
363375
runWorkerEntryPoint: bundle.runWorkerEntryPoint ?? getRunWorkerForTarget(target),
364376
loaderEntryPoint: bundle.loaderEntryPoint,
377+
initEntryPoint: bundle.initEntryPoint,
365378
configPath: bundle.configPath,
366379
customConditions: resolvedConfig.build.conditions ?? [],
367380
deploy: {

packages/cli-v3/src/entryPoints/dev-index-worker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ await sendMessageInCatalog(
141141
controllerEntryPoint: buildManifest.runControllerEntryPoint,
142142
loaderEntryPoint: buildManifest.loaderEntryPoint,
143143
customConditions: buildManifest.customConditions,
144+
initEntryPoint: buildManifest.initEntryPoint,
144145
},
145146
importErrors,
146147
},

packages/cli-v3/src/entryPoints/managed-index-worker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ await sendMessageInCatalog(
141141
controllerEntryPoint: buildManifest.runControllerEntryPoint,
142142
loaderEntryPoint: buildManifest.loaderEntryPoint,
143143
customConditions: buildManifest.customConditions,
144+
initEntryPoint: buildManifest.initEntryPoint,
144145
},
145146
importErrors,
146147
},

references/hello-world/src/trigger/example.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,6 @@
1-
import { batch, logger, task, timeout, wait, tasks } from "@trigger.dev/sdk";
1+
import { batch, logger, task, timeout, wait } from "@trigger.dev/sdk";
22
import { setTimeout } from "timers/promises";
33

4-
tasks.onInit("logging", ({ ctx, payload, task }) => {
5-
logger.info("Hello, world from the init", { ctx, payload, task });
6-
});
7-
8-
// tasks.onSuccess(({ ctx, payload, output }) => {
9-
// logger.info("Hello, world from the success", { ctx, payload });
10-
// });
11-
12-
// tasks.onComplete(({ ctx, payload, output, error }) => {
13-
// logger.info("Hello, world from the success", { ctx, payload });
14-
// });
15-
16-
// tasks.handleError(({ ctx, payload, error, retry, retryAt, retryDelayInMs }) => {
17-
// logger.info("Hello, world from the success", { ctx, payload });
18-
// });
19-
20-
// tasks.onFailure(({ ctx, payload }) => {
21-
// logger.info("Hello, world from the failure", { ctx, payload });
22-
// });
23-
24-
// tasks.onStart(({ ctx, payload }) => {
25-
// logger.info("Hello, world from the start", { ctx, payload });
26-
// });
27-
28-
// tasks.onWait(({ ctx, payload }) => {
29-
// logger.info("Hello, world from the start", { ctx, payload });
30-
// });
31-
32-
// tasks.onResume(({ ctx, payload }) => {
33-
// logger.info("Hello, world from the start", { ctx, payload });
34-
// });
35-
364
export const helloWorldTask = task({
375
id: "hello-world",
386
run: async (payload: any, { ctx }) => {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { logger, tasks } from "@trigger.dev/sdk";
2+
3+
// tasks.onSuccess(({ ctx, payload, output }) => {
4+
// logger.info("Hello, world from the success", { ctx, payload });
5+
// });
6+
7+
// tasks.onComplete(({ ctx, payload, output, error }) => {
8+
// logger.info("Hello, world from the success", { ctx, payload });
9+
// });
10+
11+
// tasks.handleError(({ ctx, payload, error, retry, retryAt, retryDelayInMs }) => {
12+
// logger.info("Hello, world from the success", { ctx, payload });
13+
// });
14+
15+
// tasks.onFailure(({ ctx, payload }) => {
16+
// logger.info("Hello, world from the failure", { ctx, payload });
17+
// });
18+
19+
// tasks.onStart(({ ctx, payload }) => {
20+
// logger.info("Hello, world from the start", { ctx, payload });
21+
// });
22+
23+
// tasks.onWait(({ ctx, payload }) => {
24+
// logger.info("Hello, world from the start", { ctx, payload });
25+
// });
26+
27+
// tasks.onResume(({ ctx, payload }) => {
28+
// logger.info("Hello, world from the start", { ctx, payload });
29+
// });
30+
31+
tasks.onInit("logging", ({ ctx, payload, task }) => {
32+
logger.info("Hello, world from the init", { ctx, payload, task });
33+
});

0 commit comments

Comments
 (0)