From 7d2453b4cf07f0c26dcfb8b5d91348d889a59c97 Mon Sep 17 00:00:00 2001
From: p-sw <shinwoo.park@psw.kr>
Date: Sat, 15 Jun 2024 01:45:00 +0900
Subject: [PATCH] feat(cli): make getRegistry take custom branch

---
 packages/cli/src/commands/add.tsx    | 6 +++---
 packages/cli/src/commands/search.tsx | 8 ++++----
 packages/cli/src/const.ts            | 2 +-
 packages/cli/src/helpers/registry.ts | 9 ++++++---
 4 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/packages/cli/src/commands/add.tsx b/packages/cli/src/commands/add.tsx
index bc2f139..62a101f 100644
--- a/packages/cli/src/commands/add.tsx
+++ b/packages/cli/src/commands/add.tsx
@@ -85,7 +85,7 @@ export default class Add extends Command {
   static override examples = ['<%= config.bin %> <%= command.id %>']
 
   static override flags = {
-    registry: Flags.string({char: 'r', description: 'override registry url'}),
+    branch: Flags.string({char: 'r', description: 'use other branch instead of main'}),
     force: Flags.boolean({char: 'f', description: 'override the existing file'}),
     config: Flags.string({char: 'p', description: 'path to config'}),
     shared: Flags.string({char: 's', description: 'place for installation of shared.ts'}),
@@ -110,9 +110,9 @@ export default class Add extends Command {
 
     const loadRegistryOra = ora('Fetching registry...').start()
     if (flags.registry) {
-      this.log(`Using ${flags.registry} for registry.`)
+      this.log(`Using ${flags.branch} for branch.`)
     }
-    const unsafeRegistry = await getRegistry(flags.registry)
+    const unsafeRegistry = await getRegistry(flags.branch)
     if (!unsafeRegistry.ok) {
       loadRegistryOra.fail(unsafeRegistry.message)
       return
diff --git a/packages/cli/src/commands/search.tsx b/packages/cli/src/commands/search.tsx
index a339125..141b87c 100644
--- a/packages/cli/src/commands/search.tsx
+++ b/packages/cli/src/commands/search.tsx
@@ -10,7 +10,7 @@ export default class Search extends Command {
   }
 
   static override flags = {
-    registry: Flags.string({char: 'r', description: 'override registry url'})
+    branch: Flags.string({char: 'r', description: 'use other branch instead of main'}),
   }
 
   static override description = 'Search components.'
@@ -20,10 +20,10 @@ export default class Search extends Command {
   public async run(): Promise<void> {
     const {args, flags} = await this.parse(Search)
 
-    if (flags.registry) {
-      this.log(`Using ${flags.registry} for registry.`)
+    if (flags.branch) {
+      this.log(`Using ${flags.branch} for registry.`)
     }
-    const registryResult = await getRegistry(flags.registry)
+    const registryResult = await getRegistry(flags.branch)
     if (!registryResult.ok) {
       this.error(registryResult.message)
     }
diff --git a/packages/cli/src/const.ts b/packages/cli/src/const.ts
index 350924d..95911d4 100644
--- a/packages/cli/src/const.ts
+++ b/packages/cli/src/const.ts
@@ -1,6 +1,6 @@
 import {z} from 'zod'
 
-export const REGISTRY_URL = 'https://raw.githubusercontent.com/pswui/ui/main/registry.json'
+export const REGISTRY_URL = (branch: string) => `https://raw.githubusercontent.com/pswui/ui/${branch}/registry.json`
 export const CONFIG_DEFAULT_PATH = 'pswui.config.js'
 
 type RegistryComponent =
diff --git a/packages/cli/src/helpers/registry.ts b/packages/cli/src/helpers/registry.ts
index 56e265a..498e252 100644
--- a/packages/cli/src/helpers/registry.ts
+++ b/packages/cli/src/helpers/registry.ts
@@ -3,14 +3,17 @@ import fetch from 'node-fetch'
 import {REGISTRY_URL, Registry} from '../const.js'
 
 export async function getRegistry(
-  REGISTRY_OVERRIDE_URL?: string,
+  branch?: string,
 ): Promise<{message: string; ok: false} | {ok: true; registry: Registry}> {
-  const registryResponse = await fetch(REGISTRY_OVERRIDE_URL ?? REGISTRY_URL)
+  const registryResponse = await fetch(REGISTRY_URL(branch ?? 'main'))
 
   if (registryResponse.ok) {
+    const registryJson = (await registryResponse.json()) as Registry
+    registryJson.base = registryJson.base.replace('{branch}', branch ?? 'main')
+
     return {
       ok: true,
-      registry: (await registryResponse.json()) as Registry,
+      registry: registryJson,
     }
   }