Skip to content

Commit 3270c1d

Browse files
committed
feat: add template for minimal fetch handler worker
1 parent d069286 commit 3270c1d

File tree

13 files changed

+142
-6
lines changed

13 files changed

+142
-6
lines changed

turbo/generators/config.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
3030

3131
plop.setGenerator('new-worker', {
3232
description: 'Create a new Cloudflare Worker using Hono',
33-
// gather information from the user
3433
prompts: [
3534
{
3635
type: 'input',
@@ -39,7 +38,6 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
3938
validate: nameValidator,
4039
},
4140
],
42-
// perform actions based on the prompts
4341
actions: (data: unknown) => {
4442
const answers = NewWorkerAnswers.parse(data)
4543
process.chdir(answers.turbo.paths.root)
@@ -64,7 +62,6 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
6462

6563
plop.setGenerator('new-worker-vite', {
6664
description: 'Create a new Cloudflare Worker using Hono and Vite',
67-
// gather information from the user
6865
prompts: [
6966
{
7067
type: 'input',
@@ -73,7 +70,6 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
7370
validate: nameValidator,
7471
},
7572
],
76-
// perform actions based on the prompts
7773
actions: (data: unknown) => {
7874
const answers = NewWorkerAnswers.parse(data)
7975
process.chdir(answers.turbo.paths.root)
@@ -96,6 +92,38 @@ export default function generator(plop: PlopTypes.NodePlopAPI): void {
9692
},
9793
})
9894

95+
plop.setGenerator('new-worker-minimal', {
96+
description: 'Create a new Cloudflare Worker with a minimal fetch handler',
97+
prompts: [
98+
{
99+
type: 'input',
100+
name: 'name',
101+
message: 'name of worker',
102+
validate: nameValidator,
103+
},
104+
],
105+
actions: (data: unknown) => {
106+
const answers = NewWorkerAnswers.parse(data)
107+
process.chdir(answers.turbo.paths.root)
108+
const destination = `apps/${slugifyText(answers.name)}`
109+
110+
const actions: PlopTypes.Actions = [
111+
{
112+
type: 'addMany',
113+
base: 'templates/fetch-worker-minimal',
114+
destination,
115+
templateFiles: ['templates/fetch-worker-minimal/**/**.hbs'],
116+
data: answers,
117+
},
118+
{ type: 'pnpmInstall', data: { ...answers, destination } satisfies PnpmInstallData },
119+
{ type: 'fixAll' },
120+
{ type: 'pnpmInstall', data: { ...answers, destination } satisfies PnpmInstallData },
121+
]
122+
123+
return actions
124+
},
125+
})
126+
99127
plop.setGenerator('new-package', {
100128
description: 'Create a new shared package',
101129
prompts: [
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# {{ slug name }}
2+
3+
A Cloudflare Workers application with a minimal fetch handler
4+
5+
## Development
6+
7+
### Run in dev mode
8+
9+
```sh
10+
pnpm dev
11+
```
12+
13+
### Run tests
14+
15+
```sh
16+
pnpm test
17+
```
18+
19+
### Deploy
20+
21+
```sh
22+
pnpm turbo deploy
23+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* eslint-disable @typescript-eslint/consistent-type-imports */
2+
type LocalEnv = import('./src/context').Env
3+
4+
// Add Env to Cloudflare namespace so that we can access it via
5+
// import { env } from 'cloudflare:workers'
6+
declare namespace Cloudflare {
7+
interface Env extends LocalEnv {}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig, getConfig } from '@repo/eslint-config'
2+
3+
import type { Config } from '@repo/eslint-config'
4+
5+
const config = getConfig(import.meta.url)
6+
7+
export default defineConfig([config]) as Config
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "{{ slug name }}",
3+
"version": "0.1.0",
4+
"private": true,
5+
"sideEffects": false,
6+
"type": "module",
7+
"scripts": {
8+
"build:wrangler": "run-wrangler-build",
9+
"check:lint": "run-eslint",
10+
"check:types": "run-tsc",
11+
"deploy": "run-wrangler-deploy",
12+
"dev": "run-wrangler-dev",
13+
"fix:workers-types": "run-wrangler-types",
14+
"test": "run-vitest"
15+
},
16+
"devDependencies": {
17+
"@cloudflare/vitest-pool-workers": "0.9.12",
18+
"@repo/eslint-config": "workspace:*",
19+
"@repo/tools": "workspace:*",
20+
"@repo/typescript-config": "workspace:*",
21+
"@types/node": "22.15.27",
22+
"vitest": "3.2.4",
23+
"wrangler": "4.42.2"
24+
}
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type Env = {
2+
// add bindings here
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { SELF } from 'cloudflare:test'
2+
import { expect, it } from 'vitest'
3+
4+
it('response with hello world', async () => {
5+
const res = await SELF.fetch('https://example.com')
6+
expect(res.status).toBe(200)
7+
expect(await res.text()).toMatchInlineSnapshot(`"hello, world!"`)
8+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Env } from './context'
2+
3+
export default {
4+
fetch: (_request, _env, _ctx) => {
5+
return new Response('hello, world!')
6+
},
7+
} satisfies ExportedHandler<Env>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@repo/typescript-config/workers.json"
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineWorkersProject } from '@cloudflare/vitest-pool-workers/config'
2+
3+
export default defineWorkersProject({
4+
test: {
5+
poolOptions: {
6+
workers: {
7+
wrangler: { configPath: `${__dirname}/wrangler.jsonc` }
8+
},
9+
},
10+
},
11+
})

0 commit comments

Comments
 (0)