Skip to content

Commit 8de50d2

Browse files
committed
refactor: dynamically build wrangler.jsonc and inject project uuid
1 parent 25bc118 commit 8de50d2

4 files changed

Lines changed: 83 additions & 58 deletions

File tree

packages/cli/src/commands/build.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Command, Option } from 'commander';
22
import consola from 'consola';
33
import { execa } from 'execa';
4-
import { copyFile, cp, rm } from 'node:fs/promises';
4+
import { copyFile, cp, rm, writeFile } from 'node:fs/promises';
55
import { join } from 'node:path';
66

77
import { getModuleCliPath } from '../lib/get-module-cli-path';
8+
import { getProjectConfig } from '../lib/project-config';
9+
import { getWranglerConfig } from '../lib/wrangler-config';
810

911
const WRANGLER_VERSION = '4.24.3';
1012

@@ -16,22 +18,31 @@ export const build = new Command('build')
1618
'catalyst',
1719
]),
1820
)
19-
.action(async () => {
21+
.action(async (options) => {
2022
const coreDir = process.cwd();
2123

2224
try {
2325
const openNextOutDir = join(coreDir, '.open-next');
2426
const bigcommerceDistDir = join(coreDir, '.bigcommerce', 'dist');
2527

28+
const config = getProjectConfig();
29+
const projectUuid = options.projectUuid ?? config.get('projectUuid');
30+
31+
if (!projectUuid) {
32+
throw new Error('Project UUID is required. Please run `link` or provide `--project-uuid`');
33+
}
34+
35+
const wranglerConfig = getWranglerConfig(projectUuid, 'PLACEHOLDER_KV_ID');
36+
2637
consola.start('Copying templates...');
2738

2839
await copyFile(
2940
join(getModuleCliPath(), 'templates', 'open-next.config.ts'),
3041
join(coreDir, 'open-next.config.ts'),
3142
);
32-
await copyFile(
33-
join(getModuleCliPath(), 'templates', 'wrangler.jsonc'),
43+
await writeFile(
3444
join(coreDir, '.bigcommerce', 'wrangler.jsonc'),
45+
JSON.stringify(wranglerConfig, null, 2),
3546
);
3647

3748
consola.success('Templates copied');
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
export function getWranglerConfig(projectUuid: string, kvNamespaceId: string) {
2+
return {
3+
$schema: 'node_modules/wrangler/config-schema.json',
4+
main: '.open-next/worker.js',
5+
name: `project-${projectUuid}`,
6+
compatibility_date: '2025-07-15',
7+
compatibility_flags: ['nodejs_compat', 'global_fetch_strictly_public'],
8+
observability: {
9+
enabled: true,
10+
head_sampling_rate: 0.05,
11+
logs: {
12+
enabled: true,
13+
head_sampling_rate: 1,
14+
invocation_logs: false,
15+
},
16+
},
17+
assets: {
18+
directory: '.open-next/assets',
19+
binding: 'ASSETS',
20+
},
21+
services: [
22+
{
23+
binding: 'WORKER_SELF_REFERENCE',
24+
service: `project-${projectUuid}`,
25+
},
26+
],
27+
kv_namespaces: [
28+
{
29+
binding: 'NEXT_INC_CACHE_KV',
30+
id: kvNamespaceId,
31+
},
32+
],
33+
durable_objects: {
34+
bindings: [
35+
{
36+
name: 'NEXT_CACHE_DO_QUEUE',
37+
class_name: 'DOQueueHandler',
38+
},
39+
{
40+
name: 'NEXT_TAG_CACHE_DO_SHARDED',
41+
class_name: 'DOShardedTagCache',
42+
},
43+
{
44+
name: 'NEXT_CACHE_DO_PURGE',
45+
class_name: 'BucketCachePurge',
46+
},
47+
],
48+
},
49+
migrations: [
50+
{
51+
tag: 'v1',
52+
new_sqlite_classes: ['DOQueueHandler', 'DOShardedTagCache', 'BucketCachePurge'],
53+
},
54+
],
55+
};
56+
}

packages/cli/templates/wrangler.jsonc

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expect, test } from 'vitest';
2+
3+
import { getWranglerConfig } from '../../src/lib/wrangler-config';
4+
5+
test('returns a config with name identical to worker self reference service', () => {
6+
const config = getWranglerConfig('uuid', 'kv-namespace-id');
7+
8+
expect(config.name).toBe(`project-uuid`);
9+
expect(
10+
config.services.find((service) => service.binding === 'WORKER_SELF_REFERENCE')?.service,
11+
).toBe(`project-uuid`);
12+
});

0 commit comments

Comments
 (0)