Skip to content

Commit 9499bd2

Browse files
carderneTrigger.dev RepoOps
authored andcommitted
fix(cli): require package.json in init target and fix custom trigger dir path
Mono-RevId: 9f1e338d7431641fb77e4cd13be6800438b2ca91
1 parent f7a8781 commit 9499bd2

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"trigger.dev": patch
3+
---
4+
5+
`trigger.dev init` now stops with a clear error when the target directory has no `package.json`, instead of installing packages into a parent project. A custom task directory entered during interactive `init <path>` is now written to `trigger.config.ts` relative to the project directory.

‎packages/cli-v3/src/commands/init.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Command } from "commander";
88
import { Option as CommandOption } from "commander";
99
import { applyEdits, findNodeAtLocation, getNodeValue, modify, parseTree } from "jsonc-parser";
1010
import { writeFile } from "node:fs/promises";
11-
import { join, relative, resolve } from "node:path";
11+
import { join, posix, relative, resolve, sep } from "node:path";
1212
import { addDependency, addDevDependency } from "nypm";
1313
import { resolveTSConfig } from "pkg-types";
1414
import { z } from "zod";
@@ -260,6 +260,12 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
260260

261261
const cwd = resolve(process.cwd(), dir);
262262

263+
if (!(await pathExists(join(cwd, "package.json")))) {
264+
throw new Error(
265+
`No package.json found in ${cwd}. Run init from the root of an existing JavaScript or TypeScript project.`
266+
);
267+
}
268+
263269
const authorization = await login({
264270
embedded: true,
265271
defaultApiUrl: options.apiUrl,
@@ -439,6 +445,10 @@ async function createTriggerDir(
439445
const relativeLocation = location.replace(/^\//, "");
440446

441447
const triggerDir = resolve(process.cwd(), relativeLocation);
448+
// The prompt takes a cwd-relative path, but trigger.config dirs are relative to the project dir.
449+
const configLocation = relative(resolve(process.cwd(), dir), triggerDir)
450+
.split(sep)
451+
.join(posix.sep);
442452

443453
logger.debug({ triggerDir });
444454

@@ -461,7 +471,7 @@ async function createTriggerDir(
461471
log.step(`Created directory at ${location}`);
462472

463473
span.end();
464-
return { location, isCustomValue: location !== defaultValue };
474+
return { location: configLocation, isCustomValue: location !== defaultValue };
465475
}
466476

467477
const templateUrl = generateTemplateUrl(
@@ -482,7 +492,7 @@ async function createTriggerDir(
482492

483493
span.end();
484494

485-
return { location, isCustomValue: location !== defaultValue };
495+
return { location: configLocation, isCustomValue: location !== defaultValue };
486496
} catch (e) {
487497
if (!(e instanceof SkipCommandError)) {
488498
recordSpanException(span, e);

0 commit comments

Comments
 (0)