-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new registry build command (#6350)
* feat: implement shadcn/registry * feat: add schema field * fix: import * chore: add changeset * chore: remove console * fix: tests * fix: diff command * feat: move to schema/registy-item.json * fix * ci: switch to node 20 * ci: build packages * fix: types * chore: update schema * chore: update build registry script * feat(shadcn): add build command
- Loading branch information
Showing
17 changed files
with
217 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft-07/schema#", | ||
"description": "A shadcn registry of components, hooks, pages, etc.", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"homepage": { | ||
"type": "string" | ||
}, | ||
"items": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "https://ui.shadcn.com/schema/registry-item.json" | ||
} | ||
} | ||
}, | ||
"required": ["name", "homepage", "items"], | ||
"uniqueItems": true, | ||
"minItems": 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import * as fs from "fs/promises" | ||
import * as path from "path" | ||
import { preFlightBuild } from "@/src/preflights/preflight-build" | ||
import { registryItemSchema, registrySchema } from "@/src/registry" | ||
import { handleError } from "@/src/utils/handle-error" | ||
import { highlighter } from "@/src/utils/highlighter" | ||
import { logger } from "@/src/utils/logger" | ||
import { spinner } from "@/src/utils/spinner" | ||
import { Command } from "commander" | ||
import { z } from "zod" | ||
|
||
export const buildOptionsSchema = z.object({ | ||
cwd: z.string(), | ||
registryFile: z.string(), | ||
outputDir: z.string(), | ||
}) | ||
|
||
export const build = new Command() | ||
.name("build") | ||
.description("build components for a shadcn registry") | ||
.argument("[registry]", "path to registry.json file", "./registry.json") | ||
.option( | ||
"-o, --output <path>", | ||
"destination directory for json files", | ||
"./public/r" | ||
) | ||
.option( | ||
"-c, --cwd <cwd>", | ||
"the working directory. defaults to the current directory.", | ||
process.cwd() | ||
) | ||
.action(async (registry: string, opts) => { | ||
try { | ||
const options = buildOptionsSchema.parse({ | ||
cwd: path.resolve(opts.cwd), | ||
registryFile: registry, | ||
outputDir: opts.output, | ||
}) | ||
|
||
const { resolvePaths } = await preFlightBuild(options) | ||
const content = await fs.readFile(resolvePaths.registryFile, "utf-8") | ||
|
||
const result = registrySchema.safeParse(JSON.parse(content)) | ||
|
||
if (!result.success) { | ||
logger.error( | ||
`Invalid registry file found at ${highlighter.info( | ||
resolvePaths.registryFile | ||
)}.` | ||
) | ||
process.exit(1) | ||
} | ||
|
||
const buildSpinner = spinner("Building registry...") | ||
for (const registryItem of result.data.items) { | ||
if (!registryItem.files) { | ||
continue | ||
} | ||
|
||
buildSpinner.start(`Building ${registryItem.name}...`) | ||
|
||
// Add the schema to the registry item. | ||
registryItem["$schema"] = | ||
"https://ui.shadcn.com/schema/registry-item.json" | ||
|
||
// Loop through each file in the files array. | ||
for (const file of registryItem.files) { | ||
file["content"] = await fs.readFile( | ||
path.resolve(resolvePaths.cwd, file.path), | ||
"utf-8" | ||
) | ||
} | ||
|
||
// Validate the registry item. | ||
const result = registryItemSchema.safeParse(registryItem) | ||
if (!result.success) { | ||
logger.error( | ||
`Invalid registry item found for ${highlighter.info( | ||
registryItem.name | ||
)}.` | ||
) | ||
continue | ||
} | ||
|
||
// Write the registry item to the output directory. | ||
await fs.writeFile( | ||
path.resolve(resolvePaths.outputDir, `${result.data.name}.json`), | ||
JSON.stringify(result.data, null, 2) | ||
) | ||
} | ||
|
||
buildSpinner.succeed("Building registry.") | ||
} catch (error) { | ||
logger.break() | ||
handleError(error) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import path from "path" | ||
import { buildOptionsSchema } from "@/src/commands/build" | ||
import * as ERRORS from "@/src/utils/errors" | ||
import { highlighter } from "@/src/utils/highlighter" | ||
import { logger } from "@/src/utils/logger" | ||
import fs from "fs-extra" | ||
import { z } from "zod" | ||
|
||
export async function preFlightBuild( | ||
options: z.infer<typeof buildOptionsSchema> | ||
) { | ||
const errors: Record<string, boolean> = {} | ||
|
||
const resolvePaths = { | ||
cwd: options.cwd, | ||
registryFile: path.resolve(options.cwd, options.registryFile), | ||
outputDir: path.resolve(options.cwd, options.outputDir), | ||
} | ||
|
||
// Ensure registry file exists. | ||
if (!fs.existsSync(resolvePaths.registryFile)) { | ||
errors[ERRORS.BUILD_MISSING_REGISTRY_FILE] = true | ||
} | ||
|
||
// Create output directory if it doesn't exist. | ||
await fs.mkdir(resolvePaths.outputDir, { recursive: true }) | ||
|
||
if (Object.keys(errors).length > 0) { | ||
if (errors[ERRORS.BUILD_MISSING_REGISTRY_FILE]) { | ||
logger.break() | ||
logger.error( | ||
`The path ${highlighter.info( | ||
resolvePaths.registryFile | ||
)} does not exist.` | ||
) | ||
} | ||
|
||
logger.break() | ||
process.exit(1) | ||
} | ||
|
||
return { | ||
errors, | ||
resolvePaths, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters