From fb2b4a23eb4f3f35b3064e7356ca8e1b7e0ddb34 Mon Sep 17 00:00:00 2001 From: Amen <2668333+amenophis1er@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:02:39 -0500 Subject: [PATCH] fix: generated Workers projects typecheck; smoke typechecks them and starts Node with npm start (#11, #7) --- files/site/conference/src/trokky/site.ts | 2 +- files/site/magazine/src/trokky/site.ts | 2 +- scripts/smoke.mjs | 20 +++++++++++++++++++- src/config.ts | 2 +- src/emit.ts | 5 +++-- test/generate.test.ts | 5 +++-- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/files/site/conference/src/trokky/site.ts b/files/site/conference/src/trokky/site.ts index a919947..3fe0006 100644 --- a/files/site/conference/src/trokky/site.ts +++ b/files/site/conference/src/trokky/site.ts @@ -79,7 +79,7 @@ export async function site(core: TrokkyCore): Promise { ...s, day: dayOf(s.day), track: tracksById.get(refId(s.track) ?? '') ?? null, - speakers: (s.speakers ?? []).map((v: unknown) => speakersById.get(refId(v) ?? '')).filter((sp): sp is Doc => Boolean(sp)), + speakers: ((s.speakers ?? []) as unknown[]).map(v => speakersById.get(refId(v) ?? '')).filter((sp): sp is Doc => Boolean(sp)), }) const sessions = sessionRows diff --git a/files/site/magazine/src/trokky/site.ts b/files/site/magazine/src/trokky/site.ts index 556785a..d7fd8cb 100644 --- a/files/site/magazine/src/trokky/site.ts +++ b/files/site/magazine/src/trokky/site.ts @@ -55,7 +55,7 @@ export async function site(core: TrokkyCore): Promise { categoryBySlug: (slug) => one('category', { slug }), categories: () => list('category'), articlesBy: async (field, id) => (await list('article', { [field]: id })).sort(byDateDesc), - articlesIn: async (ids) => (await Promise.all(ids.map(id => core.getDocument('article', id)))).filter((d): d is Doc => !!d && d._status === 'published'), + articlesIn: async (ids) => (await Promise.all(ids.map(id => core.getDocument('article', id) as Promise))).filter((d): d is Doc => !!d && d._status === 'published'), documents: async () => (await list('document')).sort(byDateDesc), page: (slug) => one('page', { slug }), } diff --git a/scripts/smoke.mjs b/scripts/smoke.mjs index 179ebc6..666f53e 100644 --- a/scripts/smoke.mjs +++ b/scripts/smoke.mjs @@ -150,6 +150,22 @@ async function smoke(testCase) { run('npm', ['run', 'build'], dir) notes.push('built') + // A Workers build is wrangler's bundler, which never typechecks. Without this, a project that + // opens in the editor full of type errors passes. (Node API and Studio builds run tsc; Node + // full-site builds run astro build, which does not typecheck either: Trokky/generator#12.) + if (workers) { + try { + run('npx', ['tsc', '--noEmit', '-p', 'tsconfig.json'], dir) + } catch (error) { + // The reporter prints the first line of the message and `stderr`: tsc writes its + // diagnostics to stdout, so carry them across or the failure says nothing. + throw Object.assign(new Error('the generated project does not typecheck'), { + stderr: `${error.stdout ?? ''}${error.stderr ?? ''}`, + }) + } + notes.push('typechecked') + } + if (testCase.skipBoot) return `${notes.join(', ')}, boot skipped (${testCase.skipBoot})` const port = workers ? 8876 : 8877 @@ -160,7 +176,9 @@ async function smoke(testCase) { // tail so the failure says what happened. const child = workers ? spawn('npx', ['wrangler', 'dev', '--port', String(port), '--ip', '127.0.0.1'], { cwd: dir, stdio: ['ignore', 'pipe', 'pipe'] }) - : spawn('npx', ['tsx', 'src/server.ts'], { cwd: dir, stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, PORT: String(port) } }) + // `npm start`, as the README says to run it in production: a wrong start path used to ship + // with this matrix green, because it booted src/server.ts through tsx instead. + : spawn('npm', ['start'], { cwd: dir, stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, PORT: String(port), NODE_ENV: 'production' } }) let tail = '' const keepTail = chunk => { tail = (tail + chunk).slice(-4000) } diff --git a/src/config.ts b/src/config.ts index 34261d1..4df9f64 100644 --- a/src/config.ts +++ b/src/config.ts @@ -40,7 +40,7 @@ export function withDefaults(input: PartialConfig): ProjectConfig { parts: pick('parts') as ProjectConfig['parts'], content: pick('content') as ProjectConfig['content'], // 3.6: TrokkyCore takes `env`, which the Workers entry passes - trokkyVersion: input.trokkyVersion ?? '^3.6.0', + trokkyVersion: input.trokkyVersion ?? '^3.6.2', } } diff --git a/src/emit.ts b/src/emit.ts index f1bdc1c..75be439 100644 --- a/src/emit.ts +++ b/src/emit.ts @@ -332,7 +332,7 @@ ${hasStudio(config) ? ` return studio(request) } ` : ''}${hasFrontend(config) ? ` - return handle(request, env as unknown as Env, ctx)` : ` + return handle(request, env as unknown as Parameters[1], ctx)` : ` return new Response('Not Found', { status: 404 })`} }, } satisfies ExportedHandler @@ -557,7 +557,8 @@ export function tsconfig(config: ProjectConfig): string { extends: hasFrontend(config) ? 'astro/tsconfigs/strict' : undefined, compilerOptions: { target: 'ES2022', module: 'ESNext', moduleResolution: 'bundler', - types: ['@cloudflare/workers-types/2023-07-01'], + // The bare package: workers-types 5 dropped the dated entry points (2023-07-01, …). + types: ['@cloudflare/workers-types'], strict: true, skipLibCheck: true, noEmit: true, }, include: ['src/**/*', 'scripts/**/*'], diff --git a/test/generate.test.ts b/test/generate.test.ts index 12c269c..80edfad 100644 --- a/test/generate.test.ts +++ b/test/generate.test.ts @@ -61,8 +61,9 @@ describe('the full edge site', () => { // Not in the example: the deploy button would offer it for the deployed Worker expect(text(tree, '.dev.vars.example')).not.toMatch(/^TROKKY_ENV=/m) expect(text(tree, 'README.md')).toContain('echo TROKKY_ENV=development >> .dev.vars') - // 3.6 is the first release whose TrokkyCore takes env - expect(JSON.parse(text(tree, 'package.json')).dependencies['@trokky/trokky']).toBe('^3.6.0') + // 3.6 is the first release whose TrokkyCore takes env; 3.6.2 the first whose R2 adapter + // accepts env.MEDIA as the globals type it + expect(JSON.parse(text(tree, 'package.json')).dependencies['@trokky/trokky']).toBe('^3.6.2') }) it('never makes deploy rebuild, because Workers Builds already built', () => {