Skip to content

Commit 1d0c069

Browse files
committed
Adding init schedule example
1 parent 852e034 commit 1d0c069

File tree

4 files changed

+67
-34
lines changed

4 files changed

+67
-34
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { context, trace } from "@opentelemetry/api";
33
import { GetProjectResponseBody, flattenAttributes } from "@trigger.dev/core/v3";
44
import { recordSpanException } from "@trigger.dev/core/v3/workers";
55
import chalk from "chalk";
6-
import { Command } from "commander";
6+
import { Command, Option as CommandOption } from "commander";
77
import { applyEdits, findNodeAtLocation, getNodeValue, modify, parseTree } from "jsonc-parser";
88
import { writeFile } from "node:fs/promises";
99
import { join, relative, resolve } from "node:path";
1010
import { addDependency, detectPackageManager } from "nypm";
11+
import { resolveTSConfig } from "pkg-types";
1112
import { z } from "zod";
1213
import { CliApiClient } from "../apiClient.js";
1314
import {
@@ -22,25 +23,24 @@ import {
2223
} from "../cli/common.js";
2324
import { loadConfig } from "../config.js";
2425
import { CLOUD_API_URL } from "../consts.js";
25-
import { cliLink, prettyError } from "../utilities/cliOutput.js";
26+
import { cliLink } from "../utilities/cliOutput.js";
2627
import {
2728
createFileFromTemplate,
2829
generateTemplateUrl,
2930
} from "../utilities/createFileFromTemplate.js";
3031
import { createFile, pathExists, readFile } from "../utilities/fileSystem.js";
3132
import { printStandloneInitialBanner } from "../utilities/initialBanner.js";
3233
import { logger } from "../utilities/logger.js";
33-
import { cliRootPath } from "../utilities/resolveInternalFilePath.js";
3434
import { spinner } from "../utilities/windows.js";
3535
import { login } from "./login.js";
36-
import { resolveTSConfig } from "pkg-types";
3736

3837
const InitCommandOptions = CommonCommandOptions.extend({
3938
projectRef: z.string().optional(),
4039
overrideConfig: z.boolean().default(false),
4140
tag: z.string().default("beta"),
4241
skipPackageInstall: z.boolean().default(false),
4342
pkgArgs: z.string().optional(),
43+
gitRef: z.string().default("main"),
4444
});
4545

4646
type InitCommandOptions = z.infer<typeof InitCommandOptions>;
@@ -66,12 +66,19 @@ export function configureInitCommand(program: Command) {
6666
"--pkg-args <args>",
6767
"Additional arguments to pass to the package manager, accepts CSV for multiple args"
6868
)
69-
).action(async (path, options) => {
70-
await handleTelemetry(async () => {
71-
await printStandloneInitialBanner(true);
72-
await initCommand(path, options);
69+
)
70+
.addOption(
71+
new CommandOption(
72+
"--git-ref <git ref>",
73+
"The git ref to use when fetching templates from GitHub"
74+
).hideHelp()
75+
)
76+
.action(async (path, options) => {
77+
await handleTelemetry(async () => {
78+
await printStandloneInitialBanner(true);
79+
await initCommand(path, options);
80+
});
7381
});
74-
});
7582
}
7683

7784
export async function initCommand(dir: string, options: unknown) {
@@ -227,6 +234,7 @@ async function createTriggerDir(dir: string, options: InitCommandOptions) {
227234
message: `Choose an example to create in the ${location} directory`,
228235
options: [
229236
{ value: "simple", label: "Simple (Hello World)" },
237+
{ value: "schedule", label: "Scheduled Task" },
230238
{
231239
value: "none",
232240
label: "None",
@@ -255,7 +263,7 @@ async function createTriggerDir(dir: string, options: InitCommandOptions) {
255263
return { location, isCustomValue: location !== defaultValue };
256264
}
257265

258-
const templateUrl = generateTemplateUrl(`examples/${example}.ts`);
266+
const templateUrl = generateTemplateUrl(`examples/${example}.ts`, options.gitRef);
259267
const outputPath = join(triggerDir, "example.ts");
260268

261269
await createFileFromTemplate({
@@ -449,7 +457,7 @@ async function writeConfigFile(
449457

450458
const projectDir = resolve(process.cwd(), dir);
451459
const outputPath = join(projectDir, "trigger.config.ts");
452-
const templateUrl = generateTemplateUrl("trigger.config.ts");
460+
const templateUrl = generateTemplateUrl("trigger.config.ts", options.gitRef);
453461

454462
span.setAttributes({
455463
"cli.projectDir": projectDir,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ export function replaceAll(input: string, replacements: Record<string, string>)
6565
return output;
6666
}
6767

68-
export function generateTemplateUrl(templateName: string) {
69-
return `https://raw.githubusercontent.com/triggerdotdev/trigger.dev/main/packages/cli-v3/templates/${templateName}.template`;
68+
export function generateTemplateUrl(templateName: string, ref: string = "main") {
69+
return `https://raw.githubusercontent.com/triggerdotdev/trigger.dev/${ref}/packages/cli-v3/templates/${templateName}.template`;
7070
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { logger, schedules, wait } from "@trigger.dev/sdk/v3";
2+
3+
export const firstScheduledTask = schedules.task({
4+
id: "first-scheduled-task",
5+
//every hour
6+
cron: "0 * * * *",
7+
run: async (payload, { ctx }) => {
8+
// The payload contains the last run timestamp that you can use to check if this is the first run
9+
// And calculate the time since the last run
10+
const distanceInMs =
11+
payload.timestamp.getTime() - (payload.lastTimestamp ?? new Date()).getTime();
12+
13+
logger.log("First scheduled tasks", { payload, distanceInMs });
14+
15+
// Wait for 5 seconds
16+
await wait.for({ seconds: 5 });
17+
18+
// Format the timestamp using the timezone from the payload
19+
const formatted = payload.timestamp.toLocaleString("en-US", {
20+
timeZone: payload.timezone,
21+
});
22+
23+
logger.log(formatted);
24+
},
25+
});

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)