Skip to content

Commit 1312b86

Browse files
committed
Update to trigger.config.ts in trigger.dev init
1 parent 63a728d commit 1312b86

File tree

5 files changed

+121
-8
lines changed

5 files changed

+121
-8
lines changed

packages/cli-v3/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"gradient-string": "^2.0.2",
111111
"import-meta-resolve": "^4.0.0",
112112
"ink": "^4.4.1",
113+
"jsonc-parser": "^3.2.1",
113114
"jsonlines": "^0.1.1",
114115
"liquidjs": "^10.9.2",
115116
"mock-fs": "^5.2.0",

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

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
import chalk from "chalk";
99
import { Command } from "commander";
1010
import { execa } from "execa";
11+
import { applyEdits, modify } from "jsonc-parser";
12+
import { writeFile } from "node:fs/promises";
1113
import { join, relative, resolve } from "node:path";
1214
import terminalLink from "terminal-link";
1315
import { z } from "zod";
@@ -24,7 +26,7 @@ import {
2426
} from "../cli/common.js";
2527
import { readConfig } from "../utilities/configFiles.js";
2628
import { createFileFromTemplate } from "../utilities/createFileFromTemplate";
27-
import { createFile, pathExists } from "../utilities/fileSystem";
29+
import { createFile, pathExists, readFile } from "../utilities/fileSystem";
2830
import { getUserPackageManager } from "../utilities/getUserPackageManager";
2931
import { printStandloneInitialBanner } from "../utilities/initialBanner.js";
3032
import { logger } from "../utilities/logger";
@@ -80,7 +82,11 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
8082

8183
intro("Initializing project");
8284

83-
const authorization = await login({ embedded: true, defaultApiUrl: options.apiUrl, profile: options.profile });
85+
const authorization = await login({
86+
embedded: true,
87+
defaultApiUrl: options.apiUrl,
88+
profile: options.profile,
89+
});
8490

8591
if (!authorization.ok) {
8692
if (authorization.error === "fetch failed") {
@@ -145,6 +151,12 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
145151
// Create the trigger dir
146152
await createTriggerDir(dir, options);
147153

154+
// Add trigger.config.ts to tsconfig.json
155+
await addConfigFileToTsConfig(dir, options);
156+
157+
// Ignore .trigger dir
158+
await gitIgnoreDotTriggerDir(dir, options);
159+
148160
const projectDashboard = terminalLink(
149161
"project dashboard",
150162
`${authorization.dashboardUrl}/projects/v3/${selectedProject.externalRef}`
@@ -255,6 +267,101 @@ async function createTriggerDir(dir: string, options: InitCommandOptions) {
255267
});
256268
}
257269

270+
async function gitIgnoreDotTriggerDir(dir: string, options: InitCommandOptions) {
271+
return await tracer.startActiveSpan("gitIgnoreDotTriggerDir", async (span) => {
272+
try {
273+
const projectDir = resolve(process.cwd(), dir);
274+
const gitIgnorePath = join(projectDir, ".gitignore");
275+
276+
span.setAttributes({
277+
"cli.projectDir": projectDir,
278+
"cli.gitIgnorePath": gitIgnorePath,
279+
});
280+
281+
if (!(await pathExists(gitIgnorePath))) {
282+
// Create .gitignore file
283+
await createFile(gitIgnorePath, ".trigger");
284+
285+
log.step(`Added .trigger to .gitignore`);
286+
287+
span.end();
288+
289+
return;
290+
}
291+
292+
// Check if .gitignore already contains .trigger
293+
const gitIgnoreContent = await readFile(gitIgnorePath);
294+
295+
if (gitIgnoreContent.includes(".trigger")) {
296+
span.end();
297+
298+
return;
299+
}
300+
301+
const newGitIgnoreContent = `${gitIgnoreContent}\n.trigger`;
302+
303+
await writeFile(gitIgnorePath, newGitIgnoreContent, "utf-8");
304+
305+
log.step(`Added .trigger to .gitignore`);
306+
307+
span.end();
308+
} catch (e) {
309+
if (!(e instanceof SkipCommandError)) {
310+
recordSpanException(span, e);
311+
}
312+
313+
span.end();
314+
315+
throw e;
316+
}
317+
});
318+
}
319+
320+
async function addConfigFileToTsConfig(dir: string, options: InitCommandOptions) {
321+
return await tracer.startActiveSpan("createTriggerDir", async (span) => {
322+
try {
323+
const projectDir = resolve(process.cwd(), dir);
324+
const tsconfigPath = join(projectDir, "tsconfig.json");
325+
326+
span.setAttributes({
327+
"cli.projectDir": projectDir,
328+
"cli.tsconfigPath": tsconfigPath,
329+
});
330+
331+
const tsconfigContent = await readFile(tsconfigPath);
332+
333+
const edits = modify(tsconfigContent, ["include", -1], "trigger.config.ts", {
334+
isArrayInsertion: true,
335+
formattingOptions: {
336+
tabSize: 2,
337+
insertSpaces: true,
338+
eol: "\n",
339+
},
340+
});
341+
342+
logger.debug("tsconfig.json edits", { edits });
343+
344+
const newTsconfigContent = applyEdits(tsconfigContent, edits);
345+
346+
logger.debug("new tsconfig.json content", { newTsconfigContent });
347+
348+
await writeFile(tsconfigPath, newTsconfigContent, "utf-8");
349+
350+
log.step(`Added trigger.config.ts to tsconfig.json`);
351+
352+
span.end();
353+
} catch (e) {
354+
if (!(e instanceof SkipCommandError)) {
355+
recordSpanException(span, e);
356+
}
357+
358+
span.end();
359+
360+
throw e;
361+
}
362+
});
363+
}
364+
258365
async function installPackages(dir: string, options: InitCommandOptions) {
259366
return await tracer.startActiveSpan("installPackages", async (span) => {
260367
const installSpinner = spinner();
@@ -332,8 +439,8 @@ async function writeConfigFile(
332439
spnnr.start("Creating config file");
333440

334441
const projectDir = resolve(process.cwd(), dir);
335-
const templatePath = resolveInternalFilePath("./templates/trigger.config.mjs");
336-
const outputPath = join(projectDir, "trigger.config.mjs");
442+
const templatePath = resolveInternalFilePath("./templates/trigger.config.ts");
443+
const outputPath = join(projectDir, "trigger.config.ts");
337444

338445
span.setAttributes({
339446
"cli.projectDir": projectDir,

packages/cli-v3/src/templates/trigger.config.mjs renamed to packages/cli-v3/src/templates/trigger.config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// @ts-check
2-
/** @type {import('@trigger.dev/sdk/v3').Config} */
1+
import type { ProjectConfig } from "@trigger.dev/core/v3";
32

4-
export default {
3+
export const config: ProjectConfig = {
54
project: "${projectRef}",
65
retries: {
76
enabledInDev: false,

pnpm-lock.yaml

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

references/v3-catalog/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.trigger

0 commit comments

Comments
 (0)