Skip to content

Commit 1a49527

Browse files
authored
CLI now supports multiple frameworks (with tests) (#480)
* Early work defining CLI framework support * WIP moving CLI init logic to the Framework class * Installing files should now work for Nextjs * Some fixes * WIP creating unit tests for Next.js project detection * Delete old jest config * Latest lockfile * Detect use of src directory test * Tests for detection pages/app directory * Correct detection of Next.js project * Renamed test file * Create install files from template files with replacements. With tests * Added multiple uses of the same replacement * Created a test for the install step (it fails right now with JS) * Removed unused import * Another test that should pass but currently fails… * Path alias fixed and now has tests * New pathAlias function used * Nextjs page install tests * Fixed app directory install (with tests) * Removed e2e CLI test, switched to unit testing strategy instead * Latest lockfile * The install files are now actual files that are copied and transformed * Got the template files working correctly after building * Next steps are now framework specific * createFileFromTemplate now works with a path again. Uses mock if specified. * Renamed apiRoute.js to pagesApiRoute.js * Simplified pages file generation * Next.js app API route template * Next.js App routing support, with common files logic shared * Dev command now uses framework default values if they exist and aren’t overridden * Unused import * pathAlias now works for all frameworks * Added a test to detect Next from the next.config.js * WIP on Remix framework support * Tests for Remix install * Replaced references to Next.js * Use a green ✔️ instead of ✅ in the CLI * Support for multiple hostnames * Tunneling can now use the hostname and port * Work on multiple ports * Improved the error messages. Added some extra pots to Next.js * Update the Remix templates to have .server in the imports * Remix updated to use server-runtime instead of node. Node v18+ * Frameworks can specify the watch paths and ignore paths * Define the watch variables above, so we can easily log them for debugging * Don’t wait for outdated package checking when running the dev command * Improved the Remix manual setup guide * Rewriting docs for quickstart * Updated the Next.js quickstart * Remix quick start * Added a changeset * Improved the Next.js manual setup
1 parent c0dfa80 commit 1a49527

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1901
-974
lines changed

.changeset/four-apes-sleep.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@trigger.dev/remix": patch
3+
"@trigger.dev/cli": patch
4+
---
5+
6+
CLI now supports multiple frameworks (starting with Next.js and Remix)

docs/_snippets/manual-setup-remix.mdx

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,50 +32,30 @@ Create a `.env` file at the root of your project and include your Trigger API ke
3232

3333
```bash
3434
TRIGGER_API_KEY=ENTER_YOUR_DEVELOPMENT_API_KEY_HERE
35-
TRIGGER_API_URL=https://cloud.trigger.dev
35+
TRIGGER_API_URL=https://api.trigger.dev # this is only necessary if you are self-hosting
3636
```
3737

3838
Replace `ENTER_YOUR_DEVELOPMENT_API_KEY_HERE` with the actual API key obtained from the previous step.
3939

4040
## Configuring the Trigger Client
4141

42-
To set up the Trigger Client for your project, follow these steps:
42+
Create a file at `<root>/app/trigger.ts`, where `<root>` represents the root directory of your project.
4343

44-
1. **Create Configuration File:**
44+
Next, add the following code to the file which creates and exports a new `TriggerClient`:
4545

46-
In your project directory, create a configuration file named `trigger.ts` or `trigger.js`, depending on whether your project uses TypeScript (`.ts`) or JavaScript (`.js`).
46+
```typescript app/trigger.(ts/js)
47+
// trigger.ts (for TypeScript) or trigger.js (for JavaScript)
4748

48-
2. **Choose Directory:**
49+
import { TriggerClient } from "@trigger.dev/sdk";
4950

50-
Create the configuration file inside the **app** directory of your project.
51-
52-
3. **Add Configuration Code:**
53-
54-
Open the configuration file you created and add the following code:
55-
56-
```typescript app/trigger.(ts/js)
57-
// trigger.ts (for TypeScript) or trigger.js (for JavaScript)
58-
59-
import { TriggerClient } from "@trigger.dev/sdk";
60-
61-
export const client = new TriggerClient({
62-
id: "my-app",
63-
apiKey: process.env.TRIGGER_API_KEY,
64-
apiUrl: process.env.TRIGGER_API_URL,
65-
});
66-
```
67-
68-
Replace **"my-app"** with an appropriate identifier for your project. The **apiKey** and **apiUrl** are obtained from the environment variables you set earlier.
69-
70-
4. **Example Directory Structure :**
51+
export const client = new TriggerClient({
52+
id: "my-app",
53+
apiKey: process.env.TRIGGER_API_KEY,
54+
apiUrl: process.env.TRIGGER_API_URL,
55+
});
56+
```
7157

72-
```
73-
project-root/
74-
├── app/
75-
├── routes/
76-
├── trigger.ts
77-
├── other files...
78-
```
58+
Replace **"my-app"** with an appropriate identifier for your project.
7959

8060
## Creating the API Route
8161

docs/_snippets/quickstart-cli-dev.mdx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
The CLI `dev` command allows the Trigger.dev service to send messages to your site. This is required for registering Jobs, triggering them and running tasks. To achieve this it creates a tunnel (using [ngrok](https://ngrok.com/)) so Trigger.dev can send messages to your machine.
2+
3+
You should leave the `dev` command running when you're developing.
4+
5+
In a **new terminal window or tab** run:
6+
7+
<CodeGroup>
8+
9+
```bash npm
10+
npx @trigger.dev/cli@latest dev
11+
```
12+
13+
```bash pnpm
14+
pnpm dlx @trigger.dev/cli@latest dev
15+
```
16+
17+
```bash yarn
18+
yarn dlx @trigger.dev/cli@latest dev
19+
```
20+
21+
</CodeGroup>
22+
<br />
23+
<Note>
24+
You can optionally pass the port if you're not running on the default port by adding
25+
`--port 3001` to the end.
26+
</Note>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
```typescript
2+
// Your first job
3+
// This Job will be triggered by an event, log a joke to the console, and then wait 5 seconds before logging the punchline
4+
client.defineJob({
5+
// This is the unique identifier for your Job, it must be unique across all Jobs in your project
6+
id: "example-job",
7+
name: "Example Job: a joke with a delay",
8+
version: "0.0.1",
9+
// This is triggered by an event using eventTrigger. You can also trigger Jobs with webhooks, on schedules, and more: https://trigger.dev/docs/documentation/concepts/triggers/introduction
10+
trigger: eventTrigger({
11+
name: "example.event",
12+
}),
13+
run: async (payload, io, ctx) => {
14+
// This logs a message to the console
15+
await io.logger.info("🧪 Example Job: a joke with a delay");
16+
await io.logger.info("How do you comfort a JavaScript bug?");
17+
// This waits for 5 seconds, the second parameter is the number of seconds to wait, you can add delays of up to a year
18+
await io.wait("Wait 5 seconds for the punchline...", 5);
19+
await io.logger.info("You console it! 🤦");
20+
await io.logger.info(
21+
"✨ Congratulations, You just ran your first successful Trigger.dev Job! ✨"
22+
);
23+
// To learn how to write much more complex (and probably funnier) Jobs, check out our docs: https://trigger.dev/docs/documentation/guides/create-a-job
24+
},
25+
});
26+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Step title="Triggering the Job">
2+
3+
There are two way to trigger this Job.
4+
5+
1. Use the "Test" functionality in the dashboard.
6+
2. Use the Trigger.dev API (either via our SDK or a web request)
7+
8+
#### "Testing" from the dashboard
9+
10+
Click into the Job and then open the "Test" tab. You should see this page:
11+
12+
![Test Job](/images/test-job.png)
13+
14+
This Job doesn't have a payload schema (meaning it takes an empty object), so you can simple click the "Run test" button.
15+
16+
**Congratulations, you should get redirected so you can see your first Run!**
17+
18+
</Step>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<Step title="Create a Trigger.dev account">
2+
3+
You can either:
4+
5+
- Use the [Trigger.dev Cloud](https://cloud.trigger.dev).
6+
- Or [self-host](/documentation/guides/self-hosting) the service.
7+
8+
</Step>
9+
10+
<Step title="Create your first project">
11+
12+
Once you've created an account, follow the steps in the app to:
13+
14+
1. Complete your account details.
15+
2. Create your first Organization and Project.
16+
17+
</Step>
18+
19+
<Step title="Getting an API key">
20+
21+
1. Go to the "Environments & API Keys" page in your project.
22+
![Go to the Environments & API Keys page ](/images/environments-link.png)
23+
24+
2. Copy the `DEV` **SERVER** API key.
25+
![API Keys](/images/api-keys.png)
26+
27+
</Step>
28+
29+
<Step title="Run the CLI `init` command">
30+
31+
The easiest way to get started it to use the CLI. It will add Trigger.dev to your existing project, setup a route and give you an example file.
32+
33+
In a terminal window run:
34+
35+
<CodeGroup>
36+
37+
```bash npm
38+
npx @trigger.dev/cli@latest init
39+
```
40+
41+
```bash pnpm
42+
pnpm dlx @trigger.dev/cli@latest init
43+
```
44+
45+
```bash yarn
46+
yarn dlx @trigger.dev/cli@latest init
47+
```
48+
49+
</CodeGroup>
50+
51+
It will ask you a couple of questions
52+
53+
1. Are you using the [Trigger.dev Cloud](https://cloud.trigger.dev) or [self-hosting](/documentation/guides/self-hosting)?
54+
2. Enter your development API key. Enter the key you copied earlier.
55+
56+
</Step>
57+
58+
<Step title="Run your site">
59+
60+
Make sure your site is running locally, we will connect to it to register your Jobs.
61+
62+
<Warning>You must leave this running for the rest of the steps.</Warning>
63+
64+
<CodeGroup>
65+
66+
```bash npm
67+
npm run dev
68+
```
69+
70+
```bash pnpm
71+
pnpm run dev
72+
```
73+
74+
```bash yarn
75+
yarn run dev
76+
```
77+
78+
</CodeGroup>
79+
80+
</Step>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## What's next?
2+
3+
<CardGroup cols={2}>
4+
<Card title="Write your first Job" icon="hexagon-plus" href="/documentation/guides/create-a-job">
5+
A Guide for how to create your first real Job
6+
</Card>
7+
<Card
8+
title="What is Trigger.dev"
9+
icon="wand-magic-sparkles"
10+
href="/documentation/concepts/what-is-triggerdotdev"
11+
>
12+
Learn more about how Trigger.dev works and how it can help you.
13+
</Card>
14+
<Card title="Examples" icon="slot-machine" href="/examples">
15+
One of the quickest ways to learn how Trigger.dev works is to view some example Jobs.
16+
</Card>
17+
<Card title="Get help" icon="hire-a-helper" href="/documentation/get-help">
18+
Struggling getting setup or have a question? We're here to help.
19+
</Card>
20+
</CardGroup>

docs/documentation/guides/manual/nextjs.mdx

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,15 @@ To begin, install the necessary packages in your Next.js project directory. You
2222
<CodeGroup>
2323

2424
```bash npm
25-
2625
npm i @trigger.dev/sdk @trigger-dev/nextjs
27-
2826
```
2927

3028
```bash pnpm
3129
pnpm install @trigger.dev/sdk @trigger-dev/nextjs
32-
3330
```
3431

3532
```bash yarn
3633
yarn add @trigger.dev/sdk @trigger-dev/nextjs
37-
3834
```
3935

4036
</CodeGroup>
@@ -58,67 +54,31 @@ Create a `.env.local` file at the root of your project and include your Trigger
5854
```bash
5955

6056
TRIGGER_API_KEY=ENTER_YOUR_DEVELOPMENT_API_KEY_HERE
61-
TRIGGER_API_URL=https://cloud.trigger.dev
57+
TRIGGER_API_URL=https://api.trigger.dev # this is only necessary if you are self-hosting
6258

6359
```
6460

6561
Replace `ENTER_YOUR_DEVELOPMENT_API_KEY_HERE` with the actual API key obtained from the previous step.
6662

6763
## Configuring the Trigger Client
6864

69-
To set up the Trigger Client for your project, follow these steps:
70-
71-
1. **Create Configuration File:**
72-
73-
In your project directory, create a configuration file named `trigger.ts` or `trigger.js`, depending on whether your project uses TypeScript (`.ts`) or JavaScript (`.js`).
74-
75-
2. **Choose Directory:**
76-
77-
Depending on your project structure, choose the appropriate directory for the configuration file. If your project uses a `src` directory, create the file within it. Otherwise, create it directly in the project root.
78-
79-
3. **Add Configuration Code:**
65+
Create a file at `<root>/src/trigger.ts` or `<root>/trigger.ts` depending on whether you're using the `src` directory or not. `<root>` represents the root directory of your project.
8066

81-
Open the configuration file you created and add the following code:
67+
Next, add the following code to the file which creates and exports a new `TriggerClient`:
8268

83-
```typescript
84-
// trigger.ts (for TypeScript) or trigger.js (for JavaScript)
69+
```typescript src/trigger.(ts/js)
70+
// trigger.ts (for TypeScript) or trigger.js (for JavaScript)
8571

86-
import { TriggerClient } from "@trigger.dev/sdk";
72+
import { TriggerClient } from "@trigger.dev/sdk";
8773

88-
export const client = new TriggerClient({
89-
id: "my-app",
90-
apiKey: process.env.TRIGGER_API_KEY,
91-
apiUrl: process.env.TRIGGER_API_URL,
92-
});
93-
```
94-
95-
Replace **"my-app"** with an appropriate identifier for your project. The **apiKey** and **apiUrl** are obtained from the environment variables you set earlier.
96-
97-
4. **File Location:**
98-
99-
Depending on your project structure, save the configuration file in the appropriate location:
100-
101-
- If your project uses a **src** directory, save the file within the **src** directory.
102-
- If your project does not use a **src** directory, save the file in the project root.
103-
104-
**Example Directory Structure with src:**
105-
106-
```
107-
project-root/
108-
├── src/
109-
├── trigger.ts
110-
├── other files...
111-
```
112-
113-
**Example Directory Structure without src:**
114-
115-
```
116-
project-root/
117-
├── trigger.ts
118-
├── other files...
119-
```
74+
export const client = new TriggerClient({
75+
id: "my-app",
76+
apiKey: process.env.TRIGGER_API_KEY,
77+
apiUrl: process.env.TRIGGER_API_URL,
78+
});
79+
```
12080

121-
By following these steps, you'll configure the Trigger Client to work with your project, regardless of whether you have a separate **src** directory and whether you're using TypeScript or JavaScript files.
81+
Replace **"my-app"** with an appropriate identifier for your project.
12282

12383
## Creating the API Route
12484

@@ -238,18 +198,31 @@ Your `package.json` file might look something like this:
238198

239199
Replace **"my-app"** with the appropriate identifier you used during the step for creating the Trigger Client.
240200

241-
## Next Steps
201+
## Running
242202

243-
Start your Next.js project locally, and then execute the `dev` CLI command to run Trigger.dev locally. You should run this command every time you want to use Trigger.dev locally.
203+
### Run your Next.js app
244204

245-
![Your first Job](/images/cli-dev.gif)
205+
Run your Next.js app locally, like you normally would. For example:
246206

247-
<Warning>
248-
Make sure your Next.js site is running locally before continuing. You must also leave this `dev`
249-
terminal command running while you develop.
250-
</Warning>
207+
<CodeGroup>
251208

252-
In a **new terminal window or tab** run:
209+
```bash npm
210+
npm run dev
211+
```
212+
213+
```bash pnpm
214+
pnpm run dev
215+
```
216+
217+
```bash yarn
218+
yarn run dev
219+
```
220+
221+
</CodeGroup>
222+
223+
### Run the CLI 'dev' command
224+
225+
In a **_separate terminal window or tab_** run:
253226

254227
<CodeGroup>
255228

@@ -271,9 +244,10 @@ yarn dlx @trigger.dev/cli@latest dev
271244
You can optionally pass the port if you're not running on 3000 by adding
272245
`--port 3001` to the end
273246
</Note>
247+
274248
<Note>
275249
You can optionally pass the hostname if you're not running on localhost by adding
276-
`--hostname <host>`. Example, in case your Next.js is running on 0.0.0.0: `--hostname 0.0.0.0`.
250+
`--hostname <host>`. Example, in case your Remix is running on 0.0.0.0: `--hostname 0.0.0.0`.
277251
</Note>
278252

279253
<Tip>

0 commit comments

Comments
 (0)