Skip to content

Commit

Permalink
feat: add 커맨드에 baseURL 옵션 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
junghyeonsu committed Dec 4, 2024
1 parent 69fd13f commit f90a7df
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
12 changes: 11 additions & 1 deletion packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { z } from "zod";

import type { CAC } from "cac";
import { addRelativeComponents } from "../utils/add-relative-components";
import { BASE_URL } from "../constants";

const addOptionsSchema = z.object({
components: z.array(z.string()).optional(),
cwd: z.string(),
all: z.boolean(),
baseUrl: z.string().optional(),
// yes: z.boolean(),
// overwrite: z.boolean(),
// path: z.string().optional(),
Expand All @@ -30,6 +32,13 @@ export const addCommand = (cli: CAC) => {
.option("-c, --cwd <cwd>", "the working directory. defaults to the current directory.", {
default: process.cwd(),
})
.option(
"-u, --baseUrl <baseUrl>",
"the base url of the registry. defaults to the current directory.",
{
default: BASE_URL,
},
)
.example("seed-design add box-button")
.example("seed-design add alert-dialog")
.action(async (components, opts) => {
Expand All @@ -39,13 +48,14 @@ export const addCommand = (cli: CAC) => {
});
const highlight = (text: string) => color.cyan(text);
const cwd = options.cwd;
const baseUrl = options.baseUrl;

if (!fs.existsSync(cwd)) {
p.log.error(`The path ${cwd} does not exist. Please try again.`);
process.exit(1);
}

const registryComponentIndex = await getRegistryUIIndex();
const registryComponentIndex = await getRegistryUIIndex(baseUrl);

let selectedComponents: string[] = options.all
? registryComponentIndex.map((registry) => registry.name)
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const BASE_URL =
process.env.NODE_ENV === "prod" ? "https://v3.seed-design.io" : "http://localhost:3000";
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node

import { addCommand } from "@/src/commands/add";
import { initCommand } from "@/src/commands/init";
import { getPackageInfo } from "@/src/utils/get-package-info";
import { cac } from "cac";
import { initCommand } from "./commands/init";

const NAME = "seed-design";
const CLI = cac(NAME);
Expand Down
14 changes: 6 additions & 8 deletions packages/cli/src/utils/get-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
import { registryUISchema, type RegistryUIMachineGenerated } from "@/src/schema";

const BASE_URL =
process.env.NODE_ENV === "prod" ? "https://v3.seed-design.io" : "http://localhost:3000";

export async function fetchRegistryUIItem(
fileNames?: string[],
baseUrl?: string,
): Promise<RegistryUIMachineGenerated> {
try {
const results = await Promise.all(
fileNames.map(async (fileName) => {
const response = await fetch(`${BASE_URL}/__registry__/ui/${fileName}.json`);
const response = await fetch(`${baseUrl}/__registry__/ui/${fileName}.json`);
return await response.json();
}),
);

return results;
} catch (error) {
console.log(error);
throw new Error(`Failed to fetch registry from ${BASE_URL}.`);
throw new Error(`Failed to fetch registry from ${baseUrl}.`);
}
}

export async function getRegistryUIIndex() {
export async function getRegistryUIIndex(baseUrl?: string) {
try {
const [result] = await fetchRegistryUIItem(["index"]);
const [result] = await fetchRegistryUIItem(["index"], baseUrl);

return registryUISchema.parse(result);
} catch (error) {
console.log(error);
throw new Error(`Failed to fetch components from ${BASE_URL}.`);
throw new Error(`Failed to fetch components from ${baseUrl}.`);
}
}

0 comments on commit f90a7df

Please sign in to comment.