feat(cli): make getRegistry take custom branch

This commit is contained in:
p-sw 2024-06-15 01:45:00 +09:00
parent c1d5c5d06b
commit 7d2453b4cf
4 changed files with 14 additions and 11 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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 =

View File

@ -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,
}
}