trigger.dev 0.0.0-v3-canary-20240322152839 → 0.0.0-v3-canary-20240322220011

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -791,7 +791,7 @@ import { build as build2 } from "esbuild";
791
791
  import { execa as execa2 } from "execa";
792
792
  import { resolve as importResolve } from "import-meta-resolve";
793
793
  import { createHash } from "node:crypto";
794
- import { readFileSync } from "node:fs";
794
+ import { readFileSync as readFileSync2 } from "node:fs";
795
795
  import { copyFile, mkdir, readFile as readFile2, writeFile as writeFile2 } from "node:fs/promises";
796
796
  import { dirname, join as join4, relative as relative2 } from "node:path";
797
797
  import { setTimeout as setTimeout2 } from "node:timers/promises";
@@ -800,7 +800,7 @@ import invariant from "tiny-invariant";
800
800
  import { z as z4 } from "zod";
801
801
 
802
802
  // package.json
803
- var version = "0.0.0-v3-canary-20240322152839";
803
+ var version = "0.0.0-v3-canary-20240322220011";
804
804
  var dependencies = {
805
805
  "@baselime/node-opentelemetry": "^0.4.6",
806
806
  "@clack/prompts": "^0.7.0",
@@ -820,8 +820,7 @@ var dependencies = {
820
820
  "@opentelemetry/sdk-trace-base": "^1.21.0",
821
821
  "@opentelemetry/sdk-trace-node": "^1.21.0",
822
822
  "@opentelemetry/semantic-conventions": "^1.21.0",
823
- "@traceloop/instrumentation-openai": "^0.3.9",
824
- "@trigger.dev/core": "workspace:0.0.0-v3-canary-20240322152839",
823
+ "@trigger.dev/core": "workspace:0.0.0-v3-canary-20240322220011",
825
824
  "@types/degit": "^2.8.3",
826
825
  chalk: "^5.2.0",
827
826
  chokidar: "^3.5.3",
@@ -1247,6 +1246,8 @@ var Logger = class {
1247
1246
  loggerLevel = getLoggerLevel();
1248
1247
  columns = process.stdout.columns;
1249
1248
  debug = (...args) => this.doLog("debug", args);
1249
+ ignore = (...args) => {
1250
+ };
1250
1251
  debugWithSanitization = (label, ...args) => {
1251
1252
  this.doLog("debug", [label, ...args]);
1252
1253
  };
@@ -1445,7 +1446,7 @@ async function gatherTaskFiles(config) {
1445
1446
  const fullPath = join(triggerDir, file.name);
1446
1447
  const filePath = relative(config.projectDir, fullPath);
1447
1448
  const importPath = filePath.replace(/\.(js|ts)$/, "");
1448
- const importName = importPath.replace(/\//g, "_");
1449
+ const importName = importPath.replace(/\//g, "_").replace(/\./g, "_").replace(/-/g, "_");
1449
1450
  taskFiles.push({ triggerDir, importPath, importName, filePath });
1450
1451
  }
1451
1452
  }
@@ -1549,7 +1550,7 @@ async function readConfig(dir, options) {
1549
1550
  }
1550
1551
  }
1551
1552
  const tempDir = await createTempDir();
1552
- const builtConfigFilePath = join2(tempDir, "config.mjs");
1553
+ const builtConfigFilePath = join2(tempDir, "config.js");
1553
1554
  const builtConfigFileHref = pathToFileURL(builtConfigFilePath).href;
1554
1555
  logger.debug("Building config file", {
1555
1556
  configPath,
@@ -1562,7 +1563,7 @@ async function readConfig(dir, options) {
1562
1563
  metafile: true,
1563
1564
  minify: false,
1564
1565
  write: true,
1565
- format: "esm",
1566
+ format: "cjs",
1566
1567
  platform: "node",
1567
1568
  target: ["es2018", "node18"],
1568
1569
  outfile: builtConfigFilePath,
@@ -1612,6 +1613,9 @@ import checkForUpdate from "update-check";
1612
1613
  import chalk2 from "chalk";
1613
1614
  var green = "#4FFF54";
1614
1615
  var purple = "#735BF3";
1616
+ function chalkPurple(text2) {
1617
+ return chalk2.hex(purple)(text2);
1618
+ }
1615
1619
  function chalkGrey(text2) {
1616
1620
  return chalk2.hex("#666")(text2);
1617
1621
  }
@@ -2633,6 +2637,33 @@ ${authorizationCodeResult.error}`
2633
2637
  // src/utilities/build.ts
2634
2638
  import { extname, isAbsolute } from "node:path";
2635
2639
  import tsConfigPaths from "tsconfig-paths";
2640
+ import { readFileSync } from "node:fs";
2641
+ function workerSetupImportConfigPlugin(configPath) {
2642
+ return {
2643
+ name: "trigger-worker-setup",
2644
+ setup(build3) {
2645
+ if (!configPath) {
2646
+ return;
2647
+ }
2648
+ build3.onLoad({ filter: /worker-setup\.js$/ }, async (args) => {
2649
+ let workerSetupContents = readFileSync(args.path, "utf-8");
2650
+ workerSetupContents = workerSetupContents.replace(
2651
+ "__SETUP_IMPORTED_PROJECT_CONFIG__",
2652
+ `import * as setupImportedConfigExports from "${configPath}"; const setupImportedConfig = setupImportedConfigExports.config;`
2653
+ );
2654
+ logger.debug("Loading worker setup", {
2655
+ args,
2656
+ workerSetupContents,
2657
+ configPath
2658
+ });
2659
+ return {
2660
+ contents: workerSetupContents,
2661
+ loader: "js"
2662
+ };
2663
+ });
2664
+ }
2665
+ };
2666
+ }
2636
2667
  function bundleDependenciesPlugin(config) {
2637
2668
  const matchPath = config.tsconfigPath ? createMatchPath(config.tsconfigPath) : void 0;
2638
2669
  function resolvePath(id) {
@@ -2642,22 +2673,22 @@ function bundleDependenciesPlugin(config) {
2642
2673
  return matchPath(id, void 0, void 0, [".ts", ".tsx", ".js", ".jsx"]) || id;
2643
2674
  }
2644
2675
  return {
2645
- name: "bundle-dependencies",
2676
+ name: "trigger-bundle-dependencies",
2646
2677
  setup(build3) {
2647
2678
  build3.onResolve({ filter: /.*/ }, (args) => {
2648
2679
  const resolvedPath = resolvePath(args.path);
2649
- logger.debug(`Checking if ${args.path} should be bundled or external`, {
2680
+ logger.ignore(`Checking if ${args.path} should be bundled or external`, {
2650
2681
  ...args,
2651
2682
  resolvedPath
2652
2683
  });
2653
2684
  if (!isBareModuleId(resolvedPath)) {
2654
- logger.debug(`Bundling ${args.path} because its not a bareModuleId`, {
2685
+ logger.ignore(`Bundling ${args.path} because its not a bareModuleId`, {
2655
2686
  ...args
2656
2687
  });
2657
2688
  return void 0;
2658
2689
  }
2659
2690
  if (args.path.startsWith("@trigger.dev/")) {
2660
- logger.debug(`Bundling ${args.path} because its a trigger.dev package`, {
2691
+ logger.ignore(`Bundling ${args.path} because its a trigger.dev package`, {
2661
2692
  ...args
2662
2693
  });
2663
2694
  return void 0;
@@ -2678,7 +2709,7 @@ function bundleDependenciesPlugin(config) {
2678
2709
  return void 0;
2679
2710
  }
2680
2711
  }
2681
- logger.debug(`Externalizing ${args.path}`, {
2712
+ logger.ignore(`Externalizing ${args.path}`, {
2682
2713
  ...args
2683
2714
  });
2684
2715
  return {
@@ -3285,7 +3316,7 @@ async function compileProject(config, options, configPath) {
3285
3316
  const compileSpinner = spinner4();
3286
3317
  compileSpinner.start(`Building project in ${config.projectDir}`);
3287
3318
  const taskFiles = await gatherTaskFiles(config);
3288
- const workerFacade = readFileSync(
3319
+ const workerFacade = readFileSync2(
3289
3320
  new URL(importResolve("./workers/prod/worker-facade.js", import.meta.url)).href.replace(
3290
3321
  "file://",
3291
3322
  ""
@@ -3330,7 +3361,7 @@ async function compileProject(config, options, configPath) {
3330
3361
  TRIGGER_API_URL: `"${config.triggerUrl}"`,
3331
3362
  __PROJECT_CONFIG__: JSON.stringify(config)
3332
3363
  },
3333
- plugins: [bundleDependenciesPlugin(config)]
3364
+ plugins: [bundleDependenciesPlugin(config), workerSetupImportConfigPlugin(configPath)]
3334
3365
  });
3335
3366
  if (result.errors.length > 0) {
3336
3367
  compileSpinner.stop("Build failed, aborting deployment");
@@ -3344,7 +3375,7 @@ async function compileProject(config, options, configPath) {
3344
3375
  if (options.outputMetafile) {
3345
3376
  await writeJSONFile(join4(options.outputMetafile, "worker.json"), result.metafile);
3346
3377
  }
3347
- const entryPointContents = readFileSync(
3378
+ const entryPointContents = readFileSync2(
3348
3379
  new URL(importResolve("./workers/prod/entry-point.js", import.meta.url)).href.replace(
3349
3380
  "file://",
3350
3381
  ""
@@ -3701,9 +3732,9 @@ import chalk6 from "chalk";
3701
3732
  import { watch } from "chokidar";
3702
3733
  import { context as context2 } from "esbuild";
3703
3734
  import { resolve as importResolve2 } from "import-meta-resolve";
3704
- import { Box, Text, render, useApp, useInput } from "ink";
3735
+ import { render, useInput } from "ink";
3705
3736
  import { createHash as createHash2 } from "node:crypto";
3706
- import fs7, { readFileSync as readFileSync2 } from "node:fs";
3737
+ import fs7, { readFileSync as readFileSync3 } from "node:fs";
3707
3738
  import { basename, dirname as dirname3, join as join5 } from "node:path";
3708
3739
  import pThrottle from "p-throttle";
3709
3740
  import { WebSocket } from "partysocket";
@@ -4333,7 +4364,16 @@ async function startDev(dir, options, authorization) {
4333
4364
  env: "dev"
4334
4365
  });
4335
4366
  if (!devEnv.success) {
4336
- throw new Error(devEnv.error);
4367
+ if (devEnv.error === "Project not found") {
4368
+ logger.error(
4369
+ `Project not found: ${config.config.project}. Ensure you are using the correct project ref and CLI profile (use --profile). Currently using the "${options.profile}" profile, which points to ${authorization2.apiUrl}`
4370
+ );
4371
+ } else {
4372
+ logger.error(
4373
+ `Failed to initialize dev environment: ${devEnv.error}. Using project ref ${config.config.project}`
4374
+ );
4375
+ }
4376
+ process.exit(1);
4337
4377
  }
4338
4378
  const environmentClient = new CliApiClient(apiUrl, devEnv.data.apiKey);
4339
4379
  return /* @__PURE__ */ React.createElement(
@@ -4467,12 +4507,11 @@ function useDev({
4467
4507
  let ctx;
4468
4508
  async function runBuild() {
4469
4509
  if (ctx) {
4470
- await ctx.cancel();
4471
4510
  await ctx.dispose();
4472
4511
  }
4473
4512
  let latestWorkerContentHash;
4474
4513
  const taskFiles = await gatherTaskFiles(config);
4475
- const workerFacade = readFileSync2(
4514
+ const workerFacade = readFileSync3(
4476
4515
  new URL(importResolve2("./workers/dev/worker-facade.js", import.meta.url)).href.replace(
4477
4516
  "file://",
4478
4517
  ""
@@ -4521,6 +4560,7 @@ function useDev({
4521
4560
  },
4522
4561
  plugins: [
4523
4562
  bundleDependenciesPlugin(config),
4563
+ workerSetupImportConfigPlugin(configPath),
4524
4564
  {
4525
4565
  name: "trigger.dev v3",
4526
4566
  setup(build3) {
@@ -4599,6 +4639,16 @@ function useDev({
4599
4639
  if (!packageVersion) {
4600
4640
  throw new Error(`Background Worker started without package version`);
4601
4641
  }
4642
+ const taskIds = taskResources.map((task) => task.id);
4643
+ const duplicateTaskIds = taskIds.filter(
4644
+ (id, index) => taskIds.indexOf(id) !== index
4645
+ );
4646
+ if (duplicateTaskIds.length > 0) {
4647
+ logger.error(
4648
+ createDuplicateTaskIdOutputErrorMessage(duplicateTaskIds, taskResources)
4649
+ );
4650
+ return;
4651
+ }
4602
4652
  const backgroundWorkerBody = {
4603
4653
  localOnly: true,
4604
4654
  metadata: {
@@ -4655,8 +4705,9 @@ function useDev({
4655
4705
  await ctx.watch();
4656
4706
  }
4657
4707
  const throttle = pThrottle({
4658
- limit: 2,
4659
- interval: 1e3
4708
+ limit: 1,
4709
+ interval: 1e3,
4710
+ strict: true
4660
4711
  });
4661
4712
  const throttledRebuild = throttle(runBuild);
4662
4713
  const taskFileWatcher = watch(
@@ -4665,7 +4716,7 @@ function useDev({
4665
4716
  ignoreInitial: true
4666
4717
  }
4667
4718
  );
4668
- taskFileWatcher.on("add", async (path6) => {
4719
+ taskFileWatcher.on("change", async (path6) => {
4669
4720
  throttledRebuild().catch((error) => {
4670
4721
  logger.error(error);
4671
4722
  });
@@ -4697,32 +4748,12 @@ function DevUIImp(props) {
4697
4748
  return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(HotKeys, null));
4698
4749
  }
4699
4750
  function useHotkeys() {
4700
- const { exit } = useApp();
4701
4751
  useInput(async (input, key) => {
4702
- if (key.return) {
4703
- console.log("");
4704
- return;
4705
- }
4706
- switch (input.toLowerCase()) {
4707
- case "c":
4708
- console.clear();
4709
- console.log();
4710
- break;
4711
- case "b": {
4712
- break;
4713
- }
4714
- case "q":
4715
- case "x":
4716
- exit();
4717
- break;
4718
- default:
4719
- break;
4720
- }
4721
4752
  });
4722
4753
  }
4723
4754
  function HotKeys() {
4724
4755
  useHotkeys();
4725
- return /* @__PURE__ */ React.createElement(Box, { borderStyle: "round", paddingLeft: 1, paddingRight: 1 }, /* @__PURE__ */ React.createElement(Text, { bold: true }, "[b]"), /* @__PURE__ */ React.createElement(Text, null, " open a browser, "), /* @__PURE__ */ React.createElement(Text, { bold: true }, "[c]"), /* @__PURE__ */ React.createElement(Text, null, " clear console, "), /* @__PURE__ */ React.createElement(Text, { bold: true }, "[x]"), /* @__PURE__ */ React.createElement(Text, null, " to exit"));
4756
+ return /* @__PURE__ */ React.createElement(React.Fragment, null);
4726
4757
  }
4727
4758
  function WebsocketFactory(apiKey) {
4728
4759
  return class extends wsWebSocket {
@@ -4774,6 +4805,18 @@ async function gatherRequiredDependencies2(outputMeta, config) {
4774
4805
  }
4775
4806
  return dependencies2;
4776
4807
  }
4808
+ function createDuplicateTaskIdOutputErrorMessage(duplicateTaskIds, taskResources) {
4809
+ const duplicateTable = duplicateTaskIds.map((id) => {
4810
+ const tasks = taskResources.filter((task) => task.id === id);
4811
+ return `id "${chalkPurple(id)}" was found in:
4812
+ ${tasks.map((task) => `${task.filePath} -> ${task.exportName}`).join("\n")}`;
4813
+ }).join("\n\n");
4814
+ return `Duplicate task ids detected:
4815
+
4816
+ ${duplicateTable}
4817
+
4818
+ `;
4819
+ }
4777
4820
  function gatherProcessEnv() {
4778
4821
  const env = {
4779
4822
  NODE_ENV: process.env.NODE_ENV ?? "development",
@@ -4905,9 +4948,6 @@ function configureInitCommand(program2) {
4905
4948
  program2.command("init").description("Initialize your existing project for development with Trigger.dev").argument("[path]", "The path to the project", ".").option(
4906
4949
  "-p, --project-ref <project ref>",
4907
4950
  "The project ref to use when initializing the project"
4908
- ).option(
4909
- "-p, --project-ref <project ref>",
4910
- "The project ref to use when initializing the project"
4911
4951
  ).option(
4912
4952
  "-t, --tag <package tag>",
4913
4953
  "The version of the @trigger.dev/sdk package to install",
@@ -4974,8 +5014,8 @@ async function _initCommand(dir, options) {
4974
5014
  } else {
4975
5015
  log3.info("Skipping package installation");
4976
5016
  }
4977
- await writeConfigFile(dir, selectedProject, options);
4978
- await createTriggerDir(dir, options);
5017
+ const triggerDir = await createTriggerDir(dir, options);
5018
+ await writeConfigFile(dir, selectedProject, options, triggerDir);
4979
5019
  await addConfigFileToTsConfig(dir, options);
4980
5020
  await gitIgnoreDotTriggerDir(dir, options);
4981
5021
  const projectDashboard = terminalLink3(
@@ -4986,7 +5026,7 @@ async function _initCommand(dir, options) {
4986
5026
  log3.info("Next steps:");
4987
5027
  log3.info(
4988
5028
  ` 1. To start developing, run ${chalk7.green(
4989
- "npx trigger.dev@latest dev"
5029
+ `npx trigger.dev@${options.tag} dev`
4990
5030
  )} in your project directory`
4991
5031
  );
4992
5032
  log3.info(` 2. Visit your ${projectDashboard} to view your newly created tasks.`);
@@ -5007,10 +5047,11 @@ async function _initCommand(dir, options) {
5007
5047
  async function createTriggerDir(dir, options) {
5008
5048
  return await tracer.startActiveSpan("createTriggerDir", async (span) => {
5009
5049
  try {
5050
+ const defaultValue = `${dir}/src/trigger`;
5010
5051
  const location = await text({
5011
5052
  message: "Where would you like to create the Trigger.dev directory?",
5012
- defaultValue: `${dir}/src/trigger`,
5013
- placeholder: `${dir}/src/trigger`
5053
+ defaultValue,
5054
+ placeholder: defaultValue
5014
5055
  });
5015
5056
  if (isCancel(location)) {
5016
5057
  throw new OutroCommandError();
@@ -5044,7 +5085,7 @@ async function createTriggerDir(dir, options) {
5044
5085
  await createFile(join6(triggerDir, ".gitkeep"), "");
5045
5086
  log3.step(`Created directory at ${location}`);
5046
5087
  span.end();
5047
- return;
5088
+ return { location, isCustomValue: location !== defaultValue };
5048
5089
  }
5049
5090
  const exampleFile = resolveInternalFilePath(`./templates/examples/${example}.ts.template`);
5050
5091
  const outputPath = join6(triggerDir, "example.ts");
@@ -5056,6 +5097,7 @@ async function createTriggerDir(dir, options) {
5056
5097
  const relativeOutputPath = relative3(process.cwd(), outputPath);
5057
5098
  log3.step(`Created example file at ${relativeOutputPath}`);
5058
5099
  span.end();
5100
+ return { location, isCustomValue: location !== defaultValue };
5059
5101
  } catch (e) {
5060
5102
  if (!(e instanceof SkipCommandError)) {
5061
5103
  recordSpanException5(span, e);
@@ -5183,7 +5225,7 @@ async function installPackages2(dir, options) {
5183
5225
  }
5184
5226
  });
5185
5227
  }
5186
- async function writeConfigFile(dir, project, options) {
5228
+ async function writeConfigFile(dir, project, options, triggerDir) {
5187
5229
  return await tracer.startActiveSpan("writeConfigFile", async (span) => {
5188
5230
  try {
5189
5231
  const spnnr = spinner5();
@@ -5199,7 +5241,9 @@ async function writeConfigFile(dir, project, options) {
5199
5241
  const result = await createFileFromTemplate({
5200
5242
  templatePath,
5201
5243
  replacements: {
5202
- projectRef: project.externalRef
5244
+ projectRef: project.externalRef,
5245
+ triggerDirectoriesOption: triggerDir.isCustomValue ? `
5246
+ triggerDirectories: ["${triggerDir.location}"],` : ""
5203
5247
  },
5204
5248
  outputPath,
5205
5249
  override: options.overrideConfig