Skip to content

Commit 469a2eb

Browse files
committed
fix: more fixes for the registry, starters and add-ons
1 parent 293bd7f commit 469a2eb

File tree

7 files changed

+59
-14
lines changed

7 files changed

+59
-14
lines changed

packages/cta-engine/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export {
4141
relativePath,
4242
} from './file-helpers.js'
4343

44-
export { formatCommand } from './utils.js'
44+
export { formatCommand, handleSpecialURL } from './utils.js'
4545

4646
export { initStarter, compileStarter } from './custom-add-ons/starter.js'
4747
export { initAddOn, compileAddOn } from './custom-add-ons/add-on.js'

packages/cta-engine/src/registry.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from 'zod'
22
import { loadRemoteAddOn } from './custom-add-ons/add-on.js'
33
import { loadStarter } from './custom-add-ons/starter.js'
4+
import { handleSpecialURL } from './utils.js'
45

56
import type { AddOn, Starter } from './types'
67

@@ -45,15 +46,16 @@ export async function getRawRegistry(
4546
): Promise<Registry | undefined> {
4647
const regUrl = registryUrl || process.env.CTA_REGISTRY
4748
if (regUrl) {
48-
const registry = (await fetch(regUrl).then((res) => res.json())) as Registry
49+
const url = handleSpecialURL(regUrl)
50+
const registry = (await fetch(url).then((res) => res.json())) as Registry
4951
const parsedRegistry = registrySchema.parse(registry)
5052
for (const addOn of parsedRegistry['add-ons'] || []) {
51-
addOn.url = absolutizeUrl(regUrl, addOn.url)
53+
addOn.url = absolutizeUrl(url, addOn.url)
5254
}
5355
for (const starter of parsedRegistry.starters || []) {
54-
starter.url = absolutizeUrl(regUrl, starter.url)
56+
starter.url = absolutizeUrl(url, starter.url)
5557
if (starter.banner) {
56-
starter.banner = absolutizeUrl(regUrl, starter.banner)
58+
starter.banner = absolutizeUrl(url, starter.banner)
5759
}
5860
}
5961
return parsedRegistry

packages/cta-engine/src/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,18 @@ export function formatCommand({
2525
}) {
2626
return `${command} ${args.join(' ')}`.trim()
2727
}
28+
29+
// Turn GitHub URLs into raw URLs
30+
export function handleSpecialURL(url: string) {
31+
if (url.startsWith('https://github.com/') && url.includes('blob')) {
32+
return url
33+
.replace('https://github.com/', 'https://raw.githubusercontent.com/')
34+
.replace('/blob/', '/refs/heads/')
35+
}
36+
if (url.startsWith('https://github.com/') && url.includes('tree')) {
37+
return url
38+
.replace('https://github.com/', 'https://raw.githubusercontent.com/')
39+
.replace('/tree/', '/refs/heads/')
40+
}
41+
return url
42+
}

packages/cta-engine/tests/utils.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { describe, expect, it } from 'vitest'
22

3-
import { formatCommand, jsSafeName, sortObject } from '../src/utils.js'
3+
import {
4+
formatCommand,
5+
handleSpecialURL,
6+
jsSafeName,
7+
sortObject,
8+
} from '../src/utils.js'
49

510
describe('formatCommand', () => {
611
it('should format a command', () => {
@@ -21,3 +26,23 @@ describe('sortObject', () => {
2126
expect(sortObject({ b: 'b', a: 'a' })).toEqual({ a: 'a', b: 'b' })
2227
})
2328
})
29+
30+
describe('handleSpecialURL', () => {
31+
it('should handle special URLs', () => {
32+
expect(
33+
handleSpecialURL(
34+
'https://github.com/TanStack/create-tsrouter-app/blob/main/examples/react-cra/registry.json',
35+
),
36+
).toEqual(
37+
'https://raw.githubusercontent.com/TanStack/create-tsrouter-app/refs/heads/main/examples/react-cra/registry.json',
38+
)
39+
40+
expect(
41+
handleSpecialURL(
42+
'https://github.com/TanStack/create-tsrouter-app/tree/alpha/packages/cta-cli/tsconfig.json',
43+
),
44+
).toEqual(
45+
'https://raw.githubusercontent.com/TanStack/create-tsrouter-app/refs/heads/alpha/packages/cta-cli/tsconfig.json',
46+
)
47+
})
48+
})

packages/cta-ui/lib/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import chalk from 'chalk'
77
import {
88
AddOnCompiledSchema,
99
StarterCompiledSchema,
10+
handleSpecialURL,
1011
} from '@tanstack/cta-engine'
1112

1213
import { addToAppWrapper } from './engine-handling/add-to-app-wrapper.js'
@@ -81,14 +82,15 @@ export function launchUI(
8182
return
8283
}
8384
try {
84-
const response = await fetch(url as string)
85+
const fixedUrl = handleSpecialURL(url as string)
86+
const response = await fetch(fixedUrl)
8587
const data = await response.json()
8688
const parsed = AddOnCompiledSchema.safeParse(data)
8789
if (!parsed.success) {
8890
res.status(400).json({ error: 'Invalid add-on data' })
8991
} else {
9092
res.json({
91-
id: url,
93+
id: fixedUrl,
9294
name: parsed.data.name,
9395
description: parsed.data.description,
9496
version: parsed.data.version,
@@ -113,14 +115,15 @@ export function launchUI(
113115
return
114116
}
115117
try {
116-
const response = await fetch(url as string)
118+
const fixedUrl = handleSpecialURL(url as string)
119+
const response = await fetch(fixedUrl)
117120
const data = await response.json()
118121
const parsed = StarterCompiledSchema.safeParse(data)
119122
if (!parsed.success) {
120123
res.status(400).json({ error: 'Invalid starter data' })
121124
} else {
122125
res.json({
123-
url,
126+
url: fixedUrl,
124127
id: parsed.data.id,
125128
name: parsed.data.name,
126129
description: parsed.data.description,
@@ -132,7 +135,7 @@ export function launchUI(
132135
typescript: parsed.data.typescript,
133136
tailwind: parsed.data.tailwind,
134137
banner: parsed.data.banner
135-
? (url as string).replace('starter.json', parsed.data.banner)
138+
? fixedUrl.replace('starter.json', parsed.data.banner)
136139
: undefined,
137140
})
138141
}

packages/cta-ui/src/components/custom-add-on-dialog.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default function CustomAddOnDialog() {
5151
<TicketPlusIcon className="w-4 h-4" />
5252
Import Custom Add-On
5353
</Button>
54-
<Dialog modal open={open}>
54+
<Dialog modal open={open} onOpenChange={setOpen}>
5555
<DialogContent className="sm:min-w-[425px] sm:max-w-fit">
5656
<DialogHeader>
5757
<DialogTitle>Import Custom Add-On</DialogTitle>

packages/cta-ui/src/components/sidebar-items/starter.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export default function Starter() {
9696
</DialogHeader>
9797
{registry?.starters && (
9898
<div>
99-
<StartersCarousel onImport={onImport} />
99+
<StartersCarousel onImport={(url) => onImport(url)} />
100100
</div>
101101
)}
102102
<div>
@@ -113,7 +113,7 @@ export default function Starter() {
113113
/>
114114
</div>
115115
<DialogFooter>
116-
<Button onClick={onImport}>Load</Button>
116+
<Button onClick={() => onImport()}>Load</Button>
117117
</DialogFooter>
118118
</DialogContent>
119119
</Dialog>

0 commit comments

Comments
 (0)