Skip to content

Commit 0ef6d64

Browse files
committed
Support for js init
1 parent 1d0c069 commit 0ef6d64

File tree

9 files changed

+135
-17
lines changed

9 files changed

+135
-17
lines changed

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,13 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
161161
log.info("Skipping package installation");
162162
}
163163

164+
const language = tsconfigPath ? "typescript" : "javascript";
165+
164166
// Create the trigger dir
165-
const triggerDir = await createTriggerDir(dir, options);
167+
const triggerDir = await createTriggerDir(dir, options, language);
166168

167169
// Create the config file
168-
await writeConfigFile(dir, selectedProject, options, triggerDir);
170+
await writeConfigFile(dir, selectedProject, options, triggerDir, language);
169171

170172
// Add trigger.config.ts to tsconfig.json
171173
if (tsconfigPath) {
@@ -203,7 +205,11 @@ async function _initCommand(dir: string, options: InitCommandOptions) {
203205
outro(`Project initialized successfully. Happy coding!`);
204206
}
205207

206-
async function createTriggerDir(dir: string, options: InitCommandOptions) {
208+
async function createTriggerDir(
209+
dir: string,
210+
options: InitCommandOptions,
211+
language: "typescript" | "javascript"
212+
) {
207213
return await tracer.startActiveSpan("createTriggerDir", async (span) => {
208214
try {
209215
const defaultValue = join(dir, "src", "trigger");
@@ -263,8 +269,11 @@ async function createTriggerDir(dir: string, options: InitCommandOptions) {
263269
return { location, isCustomValue: location !== defaultValue };
264270
}
265271

266-
const templateUrl = generateTemplateUrl(`examples/${example}.ts`, options.gitRef);
267-
const outputPath = join(triggerDir, "example.ts");
272+
const templateUrl = generateTemplateUrl(
273+
`examples/${example}.${language === "typescript" ? "ts" : "mjs"}`,
274+
options.gitRef
275+
);
276+
const outputPath = join(triggerDir, `example.${language === "typescript" ? "ts" : "mjs"}`);
268277

269278
await createFileFromTemplate({
270279
templateUrl,
@@ -448,16 +457,23 @@ async function writeConfigFile(
448457
dir: string,
449458
project: GetProjectResponseBody,
450459
options: InitCommandOptions,
451-
triggerDir: { location: string; isCustomValue: boolean }
460+
triggerDir: { location: string; isCustomValue: boolean },
461+
language: "typescript" | "javascript"
452462
) {
453463
return await tracer.startActiveSpan("writeConfigFile", async (span) => {
454464
try {
455465
const spnnr = spinner();
456466
spnnr.start("Creating config file");
457467

458468
const projectDir = resolve(process.cwd(), dir);
459-
const outputPath = join(projectDir, "trigger.config.ts");
460-
const templateUrl = generateTemplateUrl("trigger.config.ts", options.gitRef);
469+
const outputPath = join(
470+
projectDir,
471+
`trigger.config.${language === "typescript" ? "ts" : "mjs"}`
472+
);
473+
const templateUrl = generateTemplateUrl(
474+
`trigger.config.${language === "typescript" ? "ts" : "mjs"}`,
475+
options.gitRef
476+
);
461477

462478
span.setAttributes({
463479
"cli.projectDir": projectDir,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export async function createFileFromTemplate(params: {
2525
}
2626
const template = await response.text();
2727

28+
let $outputPath = params.outputPath;
29+
2830
if ((await pathExists(params.outputPath)) && !params.override) {
2931
return {
3032
success: true,
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+
});

packages/cli-v3/src/templates/examples/simple.ts.template renamed to packages/cli-v3/templates/examples/simple.mjs.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { logger, task, wait } from "@trigger.dev/sdk/v3";
22

33
export const helloWorldTask = task({
44
id: "hello-world",
5-
run: async (payload: any, { ctx }) => {
5+
run: async (payload, { ctx }) => {
66
logger.log("Hello, world!", { payload, ctx });
77

88
await wait.for({ seconds: 5 });

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { TriggerConfig } from "@trigger.dev/sdk/v3";
1+
import { defineConfig } from "@trigger.dev/sdk/v3";
22

3-
export const config: TriggerConfig = {
3+
export default defineConfig({
44
project: "${projectRef}",
55
logLevel: "log",
66
retries: {
@@ -13,4 +13,4 @@ export const config: TriggerConfig = {
1313
randomize: true,
1414
},
1515
},${triggerDirectoriesOption}
16-
};
16+
});

pnpm-lock.yaml

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

references/init-shell-js/.gitignore

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

references/init-shell-js/package.json

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

references/init-shell-js/src/index.js

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

0 commit comments

Comments
 (0)