|
| 1 | +import { Args, Command, Options } from "@effect/cli" |
| 2 | +import { Command as RawCommand } from "@effect/platform" |
| 3 | +import { Console, Effect, pipe } from "effect" |
| 4 | +import { BLOCKS_REGISTRY_URL } from "~/consts" |
| 5 | +import { getApiKey } from "./login.command" |
| 6 | + |
| 7 | +export const blockName = Args.text({ name: "blockName" }) |
| 8 | +export const blockType = Args.text({ name: "blockType" }) |
| 9 | +export const blockCategory = Args.text({ name: "blockCategory" }) |
| 10 | + |
| 11 | +export const overwrite = Options.boolean("overwrite").pipe( |
| 12 | + Options.withAlias("o"), |
| 13 | + Options.withDefault(false), |
| 14 | +) |
| 15 | + |
| 16 | +export const addBlockCommand = Command.make( |
| 17 | + "block", |
| 18 | + { blockCategory, blockType, blockName, overwrite }, |
| 19 | + ({ blockCategory, blockType, blockName, overwrite }) => |
| 20 | + Effect.gen(function* () { |
| 21 | + const apikey = yield* getApiKey |
| 22 | + |
| 23 | + if (!apikey) { |
| 24 | + yield* Console.error("No API key found. Please login first.") |
| 25 | + |
| 26 | + return yield* Effect.fail(new Error("No API key found. Please login first.")) |
| 27 | + } |
| 28 | + |
| 29 | + const componentPath = yield* pipe( |
| 30 | + Effect.succeed( |
| 31 | + `${BLOCKS_REGISTRY_URL}/api/shadcn-registry/block/${blockCategory}/${blockType}/${blockName}?apiKey=${apikey}`, |
| 32 | + ), |
| 33 | + ) |
| 34 | + |
| 35 | + console.log(componentPath) |
| 36 | + |
| 37 | + const args = ["add"] |
| 38 | + |
| 39 | + if (overwrite) { |
| 40 | + args.push("--overwrite") |
| 41 | + } |
| 42 | + args.push(componentPath) |
| 43 | + |
| 44 | + return yield* pipe( |
| 45 | + RawCommand.make("shadcnClone", ...args).pipe( |
| 46 | + RawCommand.stdin("inherit"), |
| 47 | + RawCommand.stdout("inherit"), |
| 48 | + RawCommand.stderr("inherit"), |
| 49 | + RawCommand.exitCode, |
| 50 | + ), |
| 51 | + ) |
| 52 | + }), |
| 53 | +).pipe(Command.withDescription("Adds UI components or blocks to your project.")) |
0 commit comments