Skip to content

Commit 852e034

Browse files
committed
init using templates again but downloaded from the repo this time
1 parent 25ae06a commit 852e034

File tree

10 files changed

+128
-72
lines changed

10 files changed

+128
-72
lines changed

.vscode/launch.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
"cwd": "${workspaceFolder}/references/job-catalog",
3030
"sourceMaps": true
3131
},
32+
{
33+
"type": "node-terminal",
34+
"request": "launch",
35+
"name": "Debug V3 init CLI",
36+
"command": "pnpm exec triggerdev init",
37+
"cwd": "${workspaceFolder}/references/init-shell",
38+
"sourceMaps": true
39+
},
3240
{
3341
"type": "node-terminal",
3442
"request": "launch",

packages/build/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,4 @@
149149
"main": "./dist/commonjs/index.js",
150150
"types": "./dist/commonjs/index.d.ts",
151151
"module": "./dist/esm/index.js"
152-
}
152+
}

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

Lines changed: 38 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ import {
2323
import { loadConfig } from "../config.js";
2424
import { CLOUD_API_URL } from "../consts.js";
2525
import { cliLink, prettyError } from "../utilities/cliOutput.js";
26-
import { createFileFromTemplate } from "../utilities/createFileFromTemplate.js";
26+
import {
27+
createFileFromTemplate,
28+
generateTemplateUrl,
29+
} from "../utilities/createFileFromTemplate.js";
2730
import { createFile, pathExists, readFile } from "../utilities/fileSystem.js";
2831
import { printStandloneInitialBanner } from "../utilities/initialBanner.js";
2932
import { logger } from "../utilities/logger.js";
3033
import { cliRootPath } from "../utilities/resolveInternalFilePath.js";
3134
import { spinner } from "../utilities/windows.js";
3235
import { login } from "./login.js";
36+
import { resolveTSConfig } from "pkg-types";
3337

3438
const InitCommandOptions = CommonCommandOptions.extend({
3539
projectRef: z.string().optional(),
@@ -54,7 +58,7 @@ export function configureInitCommand(program: Command) {
5458
.option(
5559
"-t, --tag <package tag>",
5660
"The version of the @trigger.dev/sdk package to install",
57-
"latest"
61+
"beta"
5862
)
5963
.option("--skip-package-install", "Skip installing the @trigger.dev/sdk package")
6064
.option("--override-config", "Override the existing config file if it exists")
@@ -81,8 +85,7 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
8185

8286
intro("Initializing project");
8387

84-
// Detect tsconfig.json and exit if not found
85-
await detectTsConfig(dir, options);
88+
const cwd = resolve(process.cwd(), dir);
8689

8790
const authorization = await login({
8891
embedded: true,
@@ -107,18 +110,22 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
107110
"cli.config.profile": authorization.profile,
108111
});
109112

113+
const tsconfigPath = await tryResolveTsConfig(cwd);
114+
110115
if (!options.overrideConfig) {
111116
try {
112117
// check to see if there is an existing trigger.dev config file in the project directory
113-
const result = await loadConfig({ cwd: dir });
118+
const result = await loadConfig({ cwd });
114119

115-
outro(
116-
result.configFile
117-
? `Project already initialized: Found config file at ${result.configFile}. Pass --override-config to override`
118-
: "Project already initialized"
119-
);
120+
if (result.configFile && result.configFile !== "trigger.config") {
121+
outro(
122+
result.configFile
123+
? `Project already initialized: Found config file at ${result.configFile}. Pass --override-config to override`
124+
: "Project already initialized"
125+
);
120126

121-
return;
127+
return;
128+
}
122129
} catch (e) {
123130
// continue
124131
}
@@ -154,7 +161,9 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
154161
await writeConfigFile(dir, selectedProject, options, triggerDir);
155162

156163
// Add trigger.config.ts to tsconfig.json
157-
await addConfigFileToTsConfig(dir, options);
164+
if (tsconfigPath) {
165+
await addConfigFileToTsConfig(tsconfigPath, options);
166+
}
158167

159168
// Ignore .trigger dir
160169
await gitIgnoreDotTriggerDir(dir, options);
@@ -175,7 +184,7 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
175184
);
176185
log.info(` 2. Visit your ${projectDashboard} to view your newly created tasks.`);
177186
log.info(
178-
` 3. Head over to our ${cliLink("v3 docs", "https://trigger.dev/docs/v3")} to learn more.`
187+
` 3. Head over to our ${cliLink("v3 docs", "https://trigger.dev/docs")} to learn more.`
179188
);
180189
log.info(
181190
` 4. Need help? Join our ${cliLink(
@@ -246,11 +255,11 @@ async function createTriggerDir(dir: string, options: InitCommandOptions) {
246255
return { location, isCustomValue: location !== defaultValue };
247256
}
248257

249-
const templatePath = join(cliRootPath(), "templates", "examples", `${example}.ts.template`);
258+
const templateUrl = generateTemplateUrl(`examples/${example}.ts`);
250259
const outputPath = join(triggerDir, "example.ts");
251260

252261
await createFileFromTemplate({
253-
templatePath,
262+
templateUrl,
254263
outputPath,
255264
replacements: {},
256265
});
@@ -324,52 +333,10 @@ async function gitIgnoreDotTriggerDir(dir: string, options: InitCommandOptions)
324333
});
325334
}
326335

327-
async function detectTsConfig(dir: string, options: InitCommandOptions) {
328-
return await tracer.startActiveSpan("detectTsConfig", async (span) => {
329-
try {
330-
const projectDir = resolve(process.cwd(), dir);
331-
const tsconfigPath = join(projectDir, "tsconfig.json");
332-
333-
span.setAttributes({
334-
"cli.projectDir": projectDir,
335-
"cli.tsconfigPath": tsconfigPath,
336-
});
337-
338-
const tsconfigExists = await pathExists(tsconfigPath);
339-
340-
if (!tsconfigExists) {
341-
prettyError(
342-
"No tsconfig.json found",
343-
`The init command needs to be run in a TypeScript project. You can create one like this:`,
344-
`npm install typescript --save-dev\nnpx tsc --init\n`
345-
);
346-
347-
throw new Error("TypeScript required");
348-
}
349-
350-
logger.debug("tsconfig.json exists", { tsconfigPath });
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-
365-
async function addConfigFileToTsConfig(dir: string, options: InitCommandOptions) {
336+
async function addConfigFileToTsConfig(tsconfigPath: string, options: InitCommandOptions) {
366337
return await tracer.startActiveSpan("addConfigFileToTsConfig", async (span) => {
367338
try {
368-
const projectDir = resolve(process.cwd(), dir);
369-
const tsconfigPath = join(projectDir, "tsconfig.json");
370-
371339
span.setAttributes({
372-
"cli.projectDir": projectDir,
373340
"cli.tsconfigPath": tsconfigPath,
374341
});
375342

@@ -481,21 +448,21 @@ async function writeConfigFile(
481448
spnnr.start("Creating config file");
482449

483450
const projectDir = resolve(process.cwd(), dir);
484-
const templatePath = join(cliRootPath(), "templates", "trigger.config.ts.template");
485451
const outputPath = join(projectDir, "trigger.config.ts");
452+
const templateUrl = generateTemplateUrl("trigger.config.ts");
486453

487454
span.setAttributes({
488455
"cli.projectDir": projectDir,
489-
"cli.templatePath": templatePath,
456+
"cli.templatePath": templateUrl,
490457
"cli.outputPath": outputPath,
491458
});
492459

493460
const result = await createFileFromTemplate({
494-
templatePath,
461+
templateUrl,
495462
replacements: {
496463
projectRef: project.externalRef,
497464
triggerDirectoriesOption: triggerDir.isCustomValue
498-
? `\n triggerDirectories: ["${triggerDir.location}"],`
465+
? `\n dirs: ["${triggerDir.location}"],`
499466
: "",
500467
},
501468
outputPath,
@@ -608,3 +575,12 @@ async function selectProject(apiClient: CliApiClient, dashboardUrl: string, proj
608575
}
609576
});
610577
}
578+
579+
async function tryResolveTsConfig(cwd: string) {
580+
try {
581+
const tsconfigPath = await resolveTSConfig(cwd);
582+
return tsconfigPath;
583+
} catch (e) {
584+
return;
585+
}
586+
}

packages/cli-v3/src/utilities/createFileFromTemplate.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,25 @@ type Result =
1313
};
1414

1515
export async function createFileFromTemplate(params: {
16-
templatePath: string;
16+
templateUrl: string;
1717
replacements: Record<string, string>;
1818
outputPath: string;
1919
override?: boolean;
2020
}): Promise<Result> {
21-
let template = await readFile(params.templatePath);
21+
try {
22+
const response = await fetch(params.templateUrl);
23+
if (!response.ok) {
24+
throw new Error(`Failed to fetch template: ${response.statusText}`);
25+
}
26+
const template = await response.text();
2227

23-
if ((await pathExists(params.outputPath)) && !params.override) {
24-
return {
25-
success: true,
26-
alreadyExisted: true,
27-
};
28-
}
28+
if ((await pathExists(params.outputPath)) && !params.override) {
29+
return {
30+
success: true,
31+
alreadyExisted: true,
32+
};
33+
}
2934

30-
try {
3135
const output = replaceAll(template, params.replacements);
3236

3337
const directoryName = path.dirname(params.outputPath);
@@ -60,3 +64,7 @@ export function replaceAll(input: string, replacements: Record<string, string>)
6064
}
6165
return output;
6266
}
67+
68+
export function generateTemplateUrl(templateName: string) {
69+
return `https://raw.githubusercontent.com/triggerdotdev/trigger.dev/main/packages/cli-v3/templates/${templateName}.template`;
70+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { logger, task, wait } from "@trigger.dev/sdk/v3";
2+
3+
export const helloWorldTask = task({
4+
id: "hello-world",
5+
run: async (payload: any, { ctx }) => {
6+
logger.log("Hello, world!", { payload, ctx });
7+
8+
await wait.for({ seconds: 5 });
9+
10+
return {
11+
message: "Hello, world!",
12+
}
13+
},
14+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { defineConfig } from "@trigger.dev/sdk/v3";
2+
3+
export default defineConfig({
4+
project: "${projectRef}",
5+
logLevel: "log",
6+
retries: {
7+
enabledInDev: true,
8+
default: {
9+
maxAttempts: 3,
10+
minTimeoutInMs: 1000,
11+
maxTimeoutInMs: 10000,
12+
factor: 2,
13+
randomize: true,
14+
},
15+
},${triggerDirectoriesOption}
16+
});

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

references/init-shell/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "references-init-shell",
3+
"private": true,
4+
"type": "module",
5+
"devDependencies": {
6+
"trigger.dev": "workspace:*",
7+
"typescript": "^5.5.4"
8+
}
9+
}

references/init-shell/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};

references/init-shell/tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2023",
4+
"module": "Node16",
5+
"moduleResolution": "Node16",
6+
"esModuleInterop": true,
7+
"strict": true,
8+
"skipLibCheck": true,
9+
"customConditions": ["@triggerdotdev/source"],
10+
"jsx": "preserve",
11+
"lib": ["DOM", "DOM.Iterable"],
12+
"noEmit": true
13+
},
14+
"include": ["./src/**/*.ts"]
15+
}

0 commit comments

Comments
 (0)