Files
shadcn-ui/apps/v4/scripts/build-registry.mts
shadcn 1aa35048a5 feat: v4 updates (#7499)
* feat(v4): update home page

* fix

* fix: cards

* feat(v4): charts page

* feat: update pages

* feat: colors

* fix

* feat: add docs

* feat: mdx work

* fix

* fix

* fix: sidebar

* fix: lint

* feat: updates

* feat: update components

* feat: fix docs

* fix: responsive

* feat: implement cmdk

* fix: update navigation menu demo

* fix: code style

* fix: themes

* feat: implement blocks page

* fix: docs config

* refactor

* fix: outputFileTracingIncludes

* fix

* fix: output

* fix

* fix: registry

* refactor: move docs

* debug: docs

* debug

* revert

* fix: mjs

* deps: pin fumadocs

* debug

* fix: downgrade next

* fix: index page

* refactor: move mdx components

* fix: remove copy button

* fix

* was it zod

* yes it was

* remove copy page

* fix: color page

* fix: colors page

* fix: meta colors

* fix: copy button

* feat: sync registry

* fix: registry build script

* feat: update port

* feat: clean up examples

* fix

* fix: mobile nav

* fix: blur for mobile

* fix: sidebar nav

* feat: update examples

* fix: build scripts

* feat: update components

* feat: restyle

* fix: types

* fix: styles

* fix: margins

* fix: screenshots

* fix

* feat: update theme

* fix: charts nav

* fix: image

* feat: optimize images

* fix: menu

* fix: card

* fix: border

* check

* feat: implement charts page

* fix: charts

* fix: og images

* feat: extend touch

* fix: static

* fix: sizing

* fix: mobile screenshots

* fix: page nav

* fix

* feat: update favicon

* fix: theme selector

* fix: feedback

* fix: sink

* docs: update

* fix: styles

* chore: update registry

* fix: command

* fix

* fix: minor updates

* fix: typography on smaller devices

* fix: format

* fix: remove unused icon

* feat: update favicon

* fix: typography

* docs: typography page

* fix: steps
2025-05-30 11:35:16 +04:00

158 lines
4.3 KiB
TypeScript

import { exec } from "child_process"
import { promises as fs } from "fs"
import path from "path"
import { rimraf } from "rimraf"
import { registry } from "@/registry/index"
async function buildRegistryIndex() {
let index = `/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-explicit-any */
// @ts-nocheck
// This file is autogenerated by scripts/build-registry.ts
// Do not edit this file directly.
import * as React from "react"
export const Index: Record<string, any> = {`
for (const item of registry.items) {
const resolveFiles = item.files?.map(
(file) => `registry/new-york-v4/${file.path}`
)
if (!resolveFiles) {
continue
}
const componentPath = item.files?.[0]?.path
? `@/registry/new-york-v4/${item.files[0].path}`
: ""
index += `
"${item.name}": {
name: "${item.name}",
description: "${item.description ?? ""}",
type: "${item.type}",
registryDependencies: ${JSON.stringify(item.registryDependencies)},
files: [${item.files?.map((file) => {
const filePath = `registry/new-york-v4/${typeof file === "string" ? file : file.path}`
const resolvedFilePath = path.resolve(filePath)
return typeof file === "string"
? `"${resolvedFilePath}"`
: `{
path: "${filePath}",
type: "${file.type}",
target: "${file.target ?? ""}"
}`
})}],
component: ${
componentPath
? `React.lazy(async () => {
const mod = await import("${componentPath}")
const exportName = Object.keys(mod).find(key => typeof mod[key] === 'function' || typeof mod[key] === 'object') || item.name
return { default: mod.default || mod[exportName] }
})`
: "null"
},
categories: ${JSON.stringify(item.categories)},
meta: ${JSON.stringify(item.meta)},
},`
}
index += `
}`
console.log(`#️⃣ ${Object.keys(registry.items).length} components found`)
// Write style index.
rimraf.sync(path.join(process.cwd(), "registry/__index__.tsx"))
await fs.writeFile(path.join(process.cwd(), "registry/__index__.tsx"), index)
}
async function buildRegistryJsonFile() {
// 1. Fix the path for registry items.
const fixedRegistry = {
...registry,
items: registry.items.map((item) => {
const files = item.files?.map((file) => {
return {
...file,
path: `registry/new-york-v4/${file.path}`,
}
})
return {
...item,
files,
}
}),
}
// 2. Write the content of the registry to `registry.json`
rimraf.sync(path.join(process.cwd(), `registry.json`))
await fs.writeFile(
path.join(process.cwd(), `registry.json`),
JSON.stringify(fixedRegistry, null, 2)
)
}
async function buildRegistry() {
return new Promise((resolve, reject) => {
const process = exec(
`pnpm dlx shadcn build registry.json --output ../www/public/r/styles/new-york-v4`
)
process.on("exit", (code) => {
if (code === 0) {
resolve(undefined)
} else {
reject(new Error(`Process exited with code ${code}`))
}
})
})
}
async function syncRegistry() {
// Store the current registry content
const registryDir = path.join(process.cwd(), "registry")
const registryIndexPath = path.join(registryDir, "__index__.tsx")
let registryContent = null
try {
registryContent = await fs.readFile(registryIndexPath, "utf8")
} catch {
// File might not exist yet, that's ok
}
// 1. Call pnpm registry:build for www.
await exec("pnpm --filter=www registry:build")
// 2. Copy the www/public/r directory to v4/public/r.
rimraf.sync(path.join(process.cwd(), "public/r"))
await fs.cp(
path.resolve(process.cwd(), "../www/public/r"),
path.resolve(process.cwd(), "public/r"),
{ recursive: true }
)
// 3. Restore the registry content if we had it
if (registryContent) {
await fs.writeFile(registryIndexPath, registryContent, "utf8")
}
}
try {
console.log("🗂️ Building registry/__index__.tsx...")
await buildRegistryIndex()
console.log("💅 Building registry.json...")
await buildRegistryJsonFile()
console.log("🏗️ Building registry...")
await buildRegistry()
console.log("🔄 Syncing registry...")
await syncRegistry()
} catch (error) {
console.error(error)
process.exit(1)
}