Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/desk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"@playwright/test": "^1.62.0",
"@types/node": "^24.0.0",
"esbuild": "^0.28.1",
"jsonc-parser": "3.3.1",
"typescript": "^7.0.0",
"vitest": "^4.1.0",
"wrangler": "^4.112.0"
Expand Down
13 changes: 12 additions & 1 deletion apps/desk/scripts/production-config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { readFile, writeFile } from 'node:fs/promises'
import { dirname, resolve } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'

import { parse as parseJsonc } from 'jsonc-parser'

const repositoryRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const sourcePath = resolve(repositoryRoot, 'wrangler.jsonc')
const outputPath = resolve(repositoryRoot, 'wrangler.production.generated.json')
Expand All @@ -12,6 +14,15 @@ function required(env, name, pattern) {
return value
}

export function parseWranglerSource(text) {
const errors = []
const source = parseJsonc(text, errors, { allowTrailingComma: true })
if (errors.length > 0 || typeof source !== 'object' || source === null || Array.isArray(source)) {
throw new Error('wrangler.jsonc is not valid JSONC')
}
return source
}

export function createProductionConfig(source, env) {
const databaseId = required(env, 'ABLE_D1_DATABASE_ID', /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i)
const databaseName = required(env, 'ABLE_D1_DATABASE_NAME', /^[a-z0-9][a-z0-9_-]{1,62}$/)
Expand Down Expand Up @@ -50,7 +61,7 @@ export function createProductionConfig(source, env) {
}

if (process.argv[1] && pathToFileURL(resolve(process.argv[1])).href === import.meta.url) {
const source = JSON.parse(await readFile(sourcePath, 'utf8'))
const source = parseWranglerSource(await readFile(sourcePath, 'utf8'))
const production = createProductionConfig(source, process.env)
await writeFile(outputPath, `${JSON.stringify(production, null, 2)}\n`, { encoding: 'utf8', mode: 0o600 })
console.log('Prepared ephemeral production Wrangler configuration')
Expand Down
26 changes: 25 additions & 1 deletion apps/desk/test/production-config.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { readFile } from 'node:fs/promises'

import { describe, expect, it } from 'vitest'

import { createProductionConfig } from '../scripts/production-config.mjs'
import { createProductionConfig, parseWranglerSource } from '../scripts/production-config.mjs'

const source = {
name: 'able',
Expand Down Expand Up @@ -55,4 +57,26 @@ describe('production Wrangler configuration', () => {
.toThrow('ABLE_WORKER_NAME is invalid')
}
})

it('projects the committed wrangler.jsonc, comments included', async () => {
// The committed configuration is JSONC, so the deploy path must accept
// comments rather than assume plain JSON.
const committed = parseWranglerSource(await readFile(new URL('../wrangler.jsonc', import.meta.url), 'utf8'))
const configured = createProductionConfig(committed, { ...variables, ABLE_WORKER_NAME: 'predecessor' })

expect(configured.name).toBe('predecessor')
expect(configured.d1_databases[0]).toMatchObject({
binding: 'DB',
database_name: variables.ABLE_D1_DATABASE_NAME,
database_id: variables.ABLE_D1_DATABASE_ID,
})
expect(configured.r2_buckets[0]).toMatchObject({ bucket_name: variables.ABLE_R2_BUCKET_NAME })
expect(configured.queues.producers[0].queue).toBe(variables.ABLE_MEDIA_QUEUE_NAME)
expect(configured.queues.consumers[0].queue).toBe(variables.ABLE_MEDIA_QUEUE_NAME)
})

it('rejects source text that is not valid JSONC', () => {
expect(() => parseWranglerSource('{ "name": ')).toThrow('wrangler.jsonc is not valid JSONC')
expect(() => parseWranglerSource('[]')).toThrow('wrangler.jsonc is not valid JSONC')
})
})
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading