Skip to content

Commit

Permalink
load blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Makisuo committed Jan 20, 2025
1 parent 760d24b commit deed70e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
35 changes: 34 additions & 1 deletion src/commands/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import url from "node:url"

import { customAlphabet } from "nanoid"

import { warningText } from "@/utils/logging"
import { getPackageManager } from "@/utils/get-package-manager"
import { error, highlight, warningText } from "@/utils/logging"
import { type } from "arktype"
import { listen } from "async-listen"
import chalk from "chalk"
Expand All @@ -32,6 +33,7 @@ const blockType = type({
slug: "string",
preview: "string",
meta: {
"packages?": "string[]",
ui: {
"[string]": "string",
},
Expand Down Expand Up @@ -70,6 +72,37 @@ export const addBlock = async ({ components }: { components: string[] }) => {
overwrite: false,
})

const packageManager = await getPackageManager()
const action = packageManager === "npm" ? "i " : "add "

if (json.meta.packages && json.meta.packages?.length === 0) {
const installCommand = `${packageManager} ${action} ${json.meta.packages.join(" ")} --silent`
const child = spawn(installCommand, {
stdio: "ignore",
shell: true,
})

await new Promise<void>((resolve, reject) => {
child.on("close", (code) => {
if (code === 0) {
resolve()
} else {
error(`Failed to install ${highlight(json.meta.packages!.join(" "))}. Exit code: ${code}`)
reject(
new Error(
`Installation failed for ${highlight(json.meta.packages!.join(" "))} with code ${code}`,
),
)
}
})

child.on("error", (err) => {
error(`Error while executing: ${highlight(installCommand)}`)
reject(err)
})
})
}

// TODO: Receive Block Data => On success, write block comps to file.
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ program.command("login").action(async () => {
})

program
.command("block [components...]")
.command("block [slug...]")
.option("--skip <type>", "Skip")
.option("-o, --overwrite", "Override existing components")
.action(async (components, options) => {
Expand Down

0 comments on commit deed70e

Please sign in to comment.