Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

Improve CLI command validation by implementing checks for required parameters and enhancing error messages for better user guidance.

vholikdef904bc5bb9
diff --git a/packages/cli/src/commands/search.ts b/packages/cli/src/commands/search.ts
new file mode 100644
index 000000000..5d8bfffd0
--- /dev/null
+++ b/packages/cli/src/commands/search.ts
@@ -0,0 +1,68 @@
+import { Command } from "commander";
+import path from "path";
+import { z } from "zod";
+import { clearRegistryContext } from "../registry/context";
+import { createConfig, getConfig } from "../utils/get-config";
+import { handleError } from "../utils/handle-error";
+import { getRegistry } from "../registry/api";
+
+const searchOptionsSchema = z.object({
+ cwd: z.string(),
+ query: z.string().optional(),
+ registry: z.string().optional(),
+});
+
+export const search = new Command()
+ .name("search")
+ .description("search blocks from registries")
+ .option(
+ "-c, --cwd <cwd>",
+ "the working directory. defaults to the current directory.",
+ process.cwd()
+ )
+ .option("-q, --query <query>", "query string")
+ .option(
+ "-r, --registry <registry>",
+ "the registry to search from (default: @mercurjs)",
+ "@mercurjs"
+ )
+ .action(async (opts) => {
+ try {
+ const options = searchOptionsSchema.parse({
+ cwd: path.resolve(opts.cwd),
+ query: opts.query,
+ type: opts.type,
+ registry: opts.registry,
+ });
+
+ let config = await getConfig(options.cwd);
+ if (!config) {
+ config = createConfig({
+ resolvedPaths: {
+ cwd: options.cwd,
+ },
+ });
+ }
+
+ const registry = await getRegistry(options.registry ?? "@mercurjs", {
+ config,
+ });
+
+ let items = registry.items;
+ if (options.query) {
+ const query = options.query.toLowerCase();
+ items = items.filter(
+ (item) =>
+ item.name.toLowerCase().includes(query) ||
+ item.description?.toLowerCase().includes(query)
+ );
+ }
+
+ console.log(JSON.stringify(items, null, 2));
+ process.exit(0);
+ } catch (error) {
+ handleError(error);
+ } finally {
+ clearRegistryContext();
+ }
+ });
diff --git a/packages/cli/src/registry/api.ts b/packages/cli/src/registry/api.ts
new file mode 100644
index 000000000..737d6ede9
--- /dev/null
+++ b/packages/cli/src/registry/api.ts
@@ -0,0 +1,82 @@
+import type { Config } from "../schema";
+import { buildUrlAndHeadersForRegistryItem } from "./builder";
+import { configWithDefaults } from "./config";
+import { clearRegistryContext } from "./context";
+import {
+ RegistryInvalidNamespaceError,
+ RegistryNotFoundError,
+ RegistryParseError,
+} from "./errors";
+import { fetchRegistry } from "./fetcher";
+import { fetchRegistryItems, resolveRegistryTree } from "./resolver";
+import { registrySchema } from "./schema";
+import { isUrl } from "./utils";
+
+export async function getRegistry(
+ name: string,
+ options?: {
+ config?: Partial<Config>;
+ }
+) {
+ const { config } = options || {};
+
+ if (isUrl(name)) {
+ const [result] = await fetchRegistry([name]);
+ try {
+ return registrySchema.parse(result);
+ } catch (error) {
+ throw new RegistryParseError(name, error);
+ }
+ }
+
+ if (!name.startsWith("@")) {
+ throw new RegistryInvalidNamespaceError(name);
+ }
+
+ let registryName = name;
+ if (!registryName.endsWith("/registry")) {
+ registryName = `${registryName}/registry`;
+ }
+
+ const urlAndHeaders = buildUrlAndHeadersForRegistryItem(
+ registryName as `@${string}`,
+ configWithDefaults(config)
+ );
+
+ if (!urlAndHeaders?.url) {
+ throw new RegistryNotFoundError(registryName);
+ }
+
+ const [result] = await fetchRegistry([urlAndHeaders.url]);
+
+ try {
+ return registrySchema.parse(result);
+ } catch (error) {
+ throw new RegistryParseError(registryName, error);
+ }
+}
+
+export async function getRegistryItems(
+ items: string[],
+ options?: {
+ config?: Partial<Config>;
+ }
+) {
+ const { config } = options || {};
+
+ clearRegistryContext();
+
+ return fetchRegistryItems(items, configWithDefaults(config));
+}
+
+export async function resolveRegistryItems(
+ items: string[],
+ options?: {
+ config?: Partial<Config>;
+ }
+) {
+ const { config } = options || {};
+
+ clearRegistryContext();
+ return resolveRegistryTree(items, configWithDefaults(config));
+}