@fumadocs/cli 1.3.2 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build/index.d.ts +4 -6
- package/dist/build/index.d.ts.map +1 -1
- package/dist/build/index.js +10 -18
- package/dist/build/index.js.map +1 -1
- package/dist/{client-YTcWP1iz.js → client-C2A4Jf2w.js} +1 -1
- package/dist/{client-YTcWP1iz.js.map → client-C2A4Jf2w.js.map} +1 -1
- package/dist/{config-Dyass4D9.d.ts → config-Bx-m6awG.d.ts} +9 -5
- package/dist/config-Bx-m6awG.d.ts.map +1 -0
- package/dist/config.d.ts +2 -2
- package/dist/config.js +74 -1
- package/dist/config.js.map +1 -0
- package/dist/{fs-CigSthjp.js → fs-C3j4H046.js} +1 -1
- package/dist/{fs-CigSthjp.js.map → fs-C3j4H046.js.map} +1 -1
- package/dist/{ast-BRNdmLn5.js → import-CSbSteB3.js} +56 -2
- package/dist/import-CSbSteB3.js.map +1 -0
- package/dist/index.js +13 -15
- package/dist/index.js.map +1 -1
- package/dist/installer-BWoLnsXE.js +673 -0
- package/dist/installer-BWoLnsXE.js.map +1 -0
- package/dist/registry/client.d.ts +17 -9
- package/dist/registry/client.d.ts.map +1 -1
- package/dist/registry/client.js +1 -1
- package/dist/registry/installer/index.d.ts +17 -27
- package/dist/registry/installer/index.d.ts.map +1 -1
- package/dist/registry/installer/index.js +1 -3
- package/dist/registry/macros/route-handler.d.ts +21 -0
- package/dist/registry/macros/route-handler.d.ts.map +1 -0
- package/dist/registry/macros/route-handler.js +11 -0
- package/dist/registry/macros/route-handler.js.map +1 -0
- package/dist/registry/schema.d.ts +2 -2
- package/dist/registry/schema.js +14 -11
- package/dist/registry/schema.js.map +1 -1
- package/dist/{schema-DrgqlhpT.d.ts → schema-BAaUX4uu.d.ts} +21 -10
- package/dist/schema-BAaUX4uu.d.ts.map +1 -0
- package/dist/schema.json +1 -0
- package/dist/types-79PW0lgM.d.ts +6 -0
- package/dist/types-79PW0lgM.d.ts.map +1 -0
- package/package.json +6 -6
- package/dist/ast-BRNdmLn5.js.map +0 -1
- package/dist/config-BYrMmXOw.js +0 -55
- package/dist/config-BYrMmXOw.js.map +0 -1
- package/dist/config-Dyass4D9.d.ts.map +0 -1
- package/dist/installer-B3-my9zN.js +0 -248
- package/dist/installer-B3-my9zN.js.map +0 -1
- package/dist/schema/default.json +0 -1
- package/dist/schema/src.json +0 -1
- package/dist/schema-DrgqlhpT.d.ts.map +0 -1
package/dist/build/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { d as registryInfoSchema, n as CompiledFile, t as CompiledComponent } from "../schema-BAaUX4uu.js";
|
|
2
|
+
import { n as DistributiveOmit } from "../types-79PW0lgM.js";
|
|
2
3
|
import { z } from "zod";
|
|
3
4
|
import { ResolverFactory } from "oxc-resolver";
|
|
4
5
|
|
|
@@ -12,11 +13,9 @@ interface CompiledRegistry {
|
|
|
12
13
|
components: CompiledComponent[];
|
|
13
14
|
info: z.output<typeof registryInfoSchema>;
|
|
14
15
|
}
|
|
15
|
-
|
|
16
|
-
type: NamespaceType;
|
|
16
|
+
type ComponentFile = DistributiveOmit<CompiledFile, 'content'> & {
|
|
17
17
|
path: string;
|
|
18
|
-
|
|
19
|
-
}
|
|
18
|
+
};
|
|
20
19
|
interface Component {
|
|
21
20
|
name: string;
|
|
22
21
|
title?: string;
|
|
@@ -123,7 +122,6 @@ declare class ComponentCompiler {
|
|
|
123
122
|
private readonly devDependencies;
|
|
124
123
|
private readonly dependencies;
|
|
125
124
|
constructor(compiler: RegistryCompiler, component: Component);
|
|
126
|
-
private toImportPath;
|
|
127
125
|
build(): Promise<CompiledComponent>;
|
|
128
126
|
private onBuildFile;
|
|
129
127
|
private buildFile;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/build/compiler.ts","../../src/build/index.ts"],"mappings":"
|
|
1
|
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HANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { n as
|
|
2
|
-
import { n as
|
|
1
|
+
import { a as transformSpecifiers, n as encodeImport } from "../import-CSbSteB3.js";
|
|
2
|
+
import { n as isRelative } from "../fs-C3j4H046.js";
|
|
3
3
|
import * as fs$1 from "node:fs/promises";
|
|
4
4
|
import * as path$1 from "node:path";
|
|
5
5
|
import picocolors from "picocolors";
|
|
@@ -116,11 +116,6 @@ var ComponentCompiler = class {
|
|
|
116
116
|
this.dependencies = /* @__PURE__ */ new Map();
|
|
117
117
|
this.registry = compiler.raw;
|
|
118
118
|
}
|
|
119
|
-
toImportPath(file) {
|
|
120
|
-
let filePath = file.target ?? file.path;
|
|
121
|
-
if (filePath.startsWith("./")) filePath = filePath.slice(2);
|
|
122
|
-
return `@/${filePath.replaceAll(path$1.sep, "/")}`;
|
|
123
|
-
}
|
|
124
119
|
async build() {
|
|
125
120
|
const files = (await Promise.all(this.component.files.map((file) => this.onBuildFile(file)))).flat();
|
|
126
121
|
const dependencies = Object.fromEntries(this.dependencies);
|
|
@@ -143,6 +138,7 @@ var ComponentCompiler = class {
|
|
|
143
138
|
const resolver = this.compiler.resolver;
|
|
144
139
|
const queue = [];
|
|
145
140
|
return [await this.buildFile(file, (reference) => {
|
|
141
|
+
if (reference.type === "dependency" && reference.dep === "@fumadocs/cli") return reference.specifier;
|
|
146
142
|
if (reference.type === "unknown-specifier") {
|
|
147
143
|
if (!reference.specifier.startsWith("node:")) console.warn(`Unknown specifier ${reference.specifier}, skipping for now`);
|
|
148
144
|
return reference.specifier;
|
|
@@ -152,21 +148,21 @@ var ComponentCompiler = class {
|
|
|
152
148
|
const refFile = this.registry.onUnknownFile?.(reference.file);
|
|
153
149
|
if (refFile) {
|
|
154
150
|
queue.push(refFile);
|
|
155
|
-
return
|
|
151
|
+
return encodeImport(refFile);
|
|
156
152
|
}
|
|
157
153
|
if (refFile === false) return;
|
|
158
154
|
throw new Error(`Unknown file ${reference.file} referenced by ${file.path}`);
|
|
159
155
|
}
|
|
160
156
|
if (reference.type === "sub-component") {
|
|
161
157
|
const resolved = reference.resolved;
|
|
162
|
-
if (resolved.component.name === this.component.name) return
|
|
158
|
+
if (resolved.component.name === this.component.name) return encodeImport(resolved.file);
|
|
163
159
|
if (resolved.type === "remote") this.subComponents.set(`${resolved.registryName}:${resolved.component.name}`, {
|
|
164
160
|
type: "http",
|
|
165
161
|
baseUrl: resolved.registryName,
|
|
166
162
|
component: resolved.component.name
|
|
167
163
|
});
|
|
168
164
|
else this.subComponents.set(resolved.component.name, resolved.component.name);
|
|
169
|
-
return
|
|
165
|
+
return encodeImport(resolved.file);
|
|
170
166
|
}
|
|
171
167
|
const dep = resolver.getDepInfo(reference.dep);
|
|
172
168
|
if (dep) (dep.type === "dev" ? this.devDependencies : this.dependencies).set(dep.name, dep.version);
|
|
@@ -183,10 +179,8 @@ var ComponentCompiler = class {
|
|
|
183
179
|
}[path$1.extname(file.path)];
|
|
184
180
|
const content = (await fs$1.readFile(sourceFilePath)).toString();
|
|
185
181
|
if (!astType) return {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
type: file.type,
|
|
189
|
-
target: file.target
|
|
182
|
+
...file,
|
|
183
|
+
content
|
|
190
184
|
};
|
|
191
185
|
const resolver = this.compiler.resolver;
|
|
192
186
|
const ast = await parse(sourceFilePath, content, { astType });
|
|
@@ -227,10 +221,8 @@ var ComponentCompiler = class {
|
|
|
227
221
|
return writeReference(onResolve ? onResolve(resolved, ctx) : resolved);
|
|
228
222
|
});
|
|
229
223
|
return {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
path: file.path,
|
|
233
|
-
target: file.target
|
|
224
|
+
...file,
|
|
225
|
+
content: s.toString()
|
|
234
226
|
};
|
|
235
227
|
}
|
|
236
228
|
};
|
package/dist/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["fs","path","fs","path"],"sources":["../../src/build/compiler.ts","../../src/build/index.ts"],"sourcesContent":["import * as fs from 'node:fs/promises';\nimport * as path from 'node:path';\nimport type {\n CompiledComponent,\n CompiledFile,\n httpSubComponent,\n NamespaceType,\n registryInfoSchema,\n} from '@/registry/schema';\nimport type { z } from 'zod';\nimport { parse } from 'oxc-parser';\nimport { ResolverFactory } from 'oxc-resolver';\nimport MagicString from 'magic-string';\nimport { transformSpecifiers } from '@/utils/ast';\nimport { isRelative } from '@/utils/fs';\n\nexport type OnResolve = (\n reference: SourceReference,\n from: { component: Component; file: ComponentFile },\n) => Reference;\n\nexport interface CompiledRegistry {\n name: string;\n components: CompiledComponent[];\n info: z.output<typeof registryInfoSchema>;\n}\n\nexport interface ComponentFile {\n type: NamespaceType;\n path: string;\n target?: string;\n}\n\nexport interface Component {\n name: string;\n title?: string;\n description?: string;\n files: ComponentFile[];\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n\n /**\n * Don't list the component in registry index file\n */\n unlisted?: boolean;\n\n /**\n * Map imported file paths, inherit from registry if not defined.\n */\n onResolve?: OnResolve;\n}\n\nexport interface PackageJson {\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n}\n\nexport interface Registry extends Omit<z.input<typeof registryInfoSchema>, 'indexes'> {\n name: string;\n packageJson: string | PackageJson;\n tsconfigPath: string;\n components: Component[];\n\n /**\n * The directory of registry, used to resolve relative paths\n */\n dir: string;\n\n /**\n * Map import paths of components\n */\n onResolve?: OnResolve;\n /**\n * When a referenced file is not found in component files, this function is called.\n * @returns file, or `false` to mark as external.\n */\n onUnknownFile?: (absolutePath: string) => ComponentFile | false | undefined;\n\n dependencies?: Record<string, string | null>;\n devDependencies?: Record<string, string | null>;\n}\n\nexport class RegistryCompiler {\n readonly raw: Registry;\n resolver!: RegistryResolver;\n\n constructor(registry: Registry) {\n this.raw = registry;\n }\n\n private async readPackageJson(): Promise<PackageJson | undefined> {\n if (typeof this.raw.packageJson !== 'string') return this.raw.packageJson;\n\n return fs\n .readFile(path.join(this.raw.dir, this.raw.packageJson))\n .then((res) => JSON.parse(res.toString()) as PackageJson)\n .catch(() => undefined);\n }\n\n async compile(): Promise<CompiledRegistry> {\n const registry = this.raw;\n this.resolver = new RegistryResolver(this, await this.readPackageJson());\n const output: CompiledRegistry = {\n name: registry.name,\n info: {\n indexes: [],\n unlistedIndexes: [],\n env: registry.env,\n variables: registry.variables,\n },\n components: [],\n };\n\n const builtComps = await Promise.all(\n registry.components.map(async (component) => {\n const compiler = new ComponentCompiler(this, component);\n\n return [component, await compiler.build()] as [Component, CompiledComponent];\n }),\n );\n\n for (const [input, comp] of builtComps) {\n const arr = input.unlisted ? output.info.unlistedIndexes : output.info.indexes;\n\n arr.push({\n name: input.name,\n title: input.title,\n description: input.description,\n });\n output.components.push(comp);\n }\n\n return output;\n }\n}\n\nclass RegistryResolver {\n private readonly deps: Record<string, string | null>;\n private readonly devDeps: Record<string, string | null>;\n private readonly fileToComponent = new Map<string, [Component, ComponentFile]>();\n readonly oxc: ResolverFactory;\n\n constructor(\n private readonly compiler: RegistryCompiler,\n packageJson: PackageJson = {},\n ) {\n const registry = compiler.raw;\n\n for (const comp of registry.components) {\n for (const file of comp.files) {\n if (this.fileToComponent.has(file.path))\n console.warn(\n `the same file ${file.path} exists in multiple component, you should make the shared file a separate component.`,\n );\n this.fileToComponent.set(file.path, [comp, file]);\n }\n }\n\n this.deps = {\n ...packageJson?.dependencies,\n ...registry.dependencies,\n };\n\n this.devDeps = {\n ...packageJson?.devDependencies,\n ...registry.devDependencies,\n };\n\n // resolve anything possible\n this.oxc = new ResolverFactory({\n extensions: ['.js', '.jsx', '.ts', '.tsx', '.node'],\n conditionNames: ['node', 'import', 'require', 'default', 'types'],\n tsconfig: {\n configFile: path.join(registry.dir, registry.tsconfigPath),\n },\n });\n }\n\n getDepFromSpecifier(specifier: string) {\n return specifier.startsWith('@')\n ? specifier.split('/').slice(0, 2).join('/')\n : specifier.split('/')[0];\n }\n\n getDepInfo(name: string):\n | {\n type: 'runtime' | 'dev';\n name: string;\n version: string | null;\n }\n | undefined {\n if (name in this.deps)\n return {\n name,\n type: 'runtime',\n version: this.deps[name],\n };\n\n if (name in this.devDeps)\n return {\n name,\n type: 'dev',\n version: this.devDeps[name],\n };\n\n console.warn(`dep info for ${name} cannot be found`);\n }\n\n getComponentByName(name: string): Component | undefined {\n return this.compiler.raw.components.find((comp) => comp.name === name);\n }\n\n getSubComponent(file: string) {\n const relativeFile = path.relative(this.compiler.raw.dir, file);\n const comp = this.fileToComponent.get(relativeFile);\n\n if (!comp) return;\n return {\n component: comp[0],\n file: comp[1],\n };\n }\n}\n\nexport type SourceReference =\n | {\n type: 'file';\n /**\n * Absolute path\n */\n file: string;\n }\n | {\n type: 'dependency';\n dep: string;\n specifier: string;\n }\n | {\n type: 'sub-component';\n resolved:\n | {\n type: 'local';\n component: Component;\n file: ComponentFile;\n }\n | {\n type: 'remote';\n component: Component;\n file: ComponentFile;\n registryName: string;\n };\n }\n | {\n type: 'unknown-specifier';\n specifier: string;\n };\n\nexport type Reference =\n | SourceReference\n | {\n type: 'custom';\n specifier: string;\n };\n\nexport class ComponentCompiler {\n private readonly processedFiles = new Set<string>();\n private readonly registry: Registry;\n private readonly subComponents = new Map<string, string | z.input<typeof httpSubComponent>>();\n private readonly devDependencies = new Map<string, string | null>();\n private readonly dependencies = new Map<string, string | null>();\n\n constructor(\n private readonly compiler: RegistryCompiler,\n private readonly component: Component,\n ) {\n this.registry = compiler.raw;\n }\n\n private toImportPath(file: ComponentFile): string {\n let filePath = file.target ?? file.path;\n\n if (filePath.startsWith('./')) filePath = filePath.slice(2);\n\n return `@/${filePath.replaceAll(path.sep, '/')}`;\n }\n\n async build(): Promise<CompiledComponent> {\n const files = (\n await Promise.all(this.component.files.map((file) => this.onBuildFile(file)))\n ).flat();\n const dependencies = Object.fromEntries(this.dependencies);\n const devDependencies = Object.fromEntries(this.devDependencies);\n\n if (this.component.dependencies) {\n Object.assign(dependencies, this.component.dependencies);\n }\n if (this.component.devDependencies) {\n Object.assign(devDependencies, this.component.devDependencies);\n }\n\n return {\n name: this.component.name,\n title: this.component.title,\n description: this.component.description,\n files,\n subComponents: Array.from(this.subComponents.values()),\n dependencies,\n devDependencies,\n };\n }\n\n private async onBuildFile(file: ComponentFile): Promise<CompiledFile[]> {\n if (this.processedFiles.has(file.path)) return [];\n this.processedFiles.add(file.path);\n const resolver = this.compiler.resolver;\n\n const queue: ComponentFile[] = [];\n const result = await this.buildFile(file, (reference) => {\n if (reference.type === 'unknown-specifier') {\n if (!reference.specifier.startsWith('node:')) {\n console.warn(`Unknown specifier ${reference.specifier}, skipping for now`);\n }\n\n return reference.specifier;\n }\n\n if (reference.type === 'custom') return reference.specifier;\n\n if (reference.type === 'file') {\n const refFile = this.registry.onUnknownFile?.(reference.file);\n if (refFile) {\n queue.push(refFile);\n return this.toImportPath(refFile);\n }\n\n if (refFile === false) return;\n\n throw new Error(`Unknown file ${reference.file} referenced by ${file.path}`);\n }\n\n if (reference.type === 'sub-component') {\n const resolved = reference.resolved;\n if (resolved.component.name === this.component.name)\n return this.toImportPath(resolved.file);\n\n if (resolved.type === 'remote') {\n this.subComponents.set(`${resolved.registryName}:${resolved.component.name}`, {\n type: 'http',\n baseUrl: resolved.registryName,\n component: resolved.component.name,\n });\n } else {\n this.subComponents.set(resolved.component.name, resolved.component.name);\n }\n\n return this.toImportPath(resolved.file);\n }\n\n const dep = resolver.getDepInfo(reference.dep);\n if (dep) {\n const map = dep.type === 'dev' ? this.devDependencies : this.dependencies;\n map.set(dep.name, dep.version);\n }\n\n return reference.specifier;\n });\n\n return [result, ...(await Promise.all(queue.map((file) => this.onBuildFile(file)))).flat()];\n }\n\n private async buildFile(\n file: ComponentFile,\n /**\n * write references back to import specifiers\n *\n * keep original one if `undefined`\n */\n writeReference: (reference: Reference) => string | undefined,\n ): Promise<CompiledFile> {\n const sourceFilePath = path.join(this.registry.dir, file.path);\n const astTypes: Record<string, 'js' | 'ts' | undefined> = {\n '.ts': 'ts',\n '.tsx': 'ts',\n '.js': 'js',\n '.jsx': 'js',\n };\n const astType = astTypes[path.extname(file.path)];\n const content = (await fs.readFile(sourceFilePath)).toString();\n\n if (!astType)\n return {\n content,\n path: file.path,\n type: file.type,\n target: file.target,\n };\n\n const resolver = this.compiler.resolver;\n\n const ast = await parse(sourceFilePath, content, {\n astType,\n });\n\n if (ast.errors.length > 0) {\n throw new Error(`failed to parse file ${sourceFilePath}: \\n${ast.errors.join('\\n')}`);\n }\n\n const s = new MagicString(content);\n const ctx = { component: this.component, file };\n // Process import paths\n transformSpecifiers(ast.program, s, (specifier) => {\n let resolved: Reference = {\n type: 'unknown-specifier',\n specifier: specifier,\n };\n const onResolve = this.component.onResolve ?? this.registry.onResolve;\n const resolvedSpecifier = resolver.oxc.resolveFileSync(sourceFilePath, specifier);\n if (resolvedSpecifier.error || !resolvedSpecifier.path) {\n return writeReference(onResolve ? onResolve(resolved, ctx) : resolved);\n }\n\n resolved = {\n type: 'file',\n file: resolvedSpecifier.path,\n };\n\n // outside of registry dir\n if (!isRelative(this.registry.dir, resolvedSpecifier.path)) {\n resolved = {\n type: 'dependency',\n dep: resolver.getDepFromSpecifier(specifier),\n specifier,\n };\n } else {\n const sub = resolver.getSubComponent(resolvedSpecifier.path);\n if (sub) {\n resolved = {\n type: 'sub-component',\n resolved: {\n type: 'local',\n component: sub.component,\n file: sub.file,\n },\n };\n }\n }\n\n return writeReference(onResolve ? onResolve(resolved, ctx) : resolved);\n });\n\n return {\n content: s.toString(),\n type: file.type,\n path: file.path,\n target: file.target,\n };\n }\n}\n\nexport function resolveFromRemote(\n r: Registry,\n component: string,\n selectFile: (file: ComponentFile) => boolean,\n): Reference | undefined {\n const comp = r.components.find((comp) => comp.name === component);\n if (!comp) return;\n const file = comp.files.find(selectFile);\n if (!file) return;\n\n return {\n type: 'sub-component',\n resolved: {\n type: 'remote',\n registryName: r.name,\n component: comp,\n file,\n },\n };\n}\n","import * as fs from 'node:fs/promises';\nimport * as path from 'node:path';\nimport picocolors from 'picocolors';\nimport type { CompiledRegistry } from '@/build/compiler';\n\nexport * from './compiler';\n\nexport interface MonoRegistry extends CompiledRegistry {\n registries: CompiledRegistry[];\n}\n\nexport function combineRegistry(\n root: CompiledRegistry,\n ...items: CompiledRegistry[]\n): MonoRegistry {\n return {\n ...root,\n info: {\n ...root.info,\n registries: items.map((item) => item.name),\n },\n registries: items,\n };\n}\n\nexport async function writeFumadocsRegistry(\n out: CompiledRegistry | MonoRegistry,\n options: {\n dir: string;\n\n /**\n * Remove previous outputs\n *\n * @defaultValue false\n */\n cleanDir?: boolean;\n\n log?: boolean;\n },\n): Promise<void> {\n const { dir, cleanDir = false, log = true } = options;\n\n if (cleanDir) {\n await fs.rm(dir, {\n recursive: true,\n force: true,\n });\n console.log(picocolors.bold(picocolors.greenBright('Cleaned directory')));\n }\n\n async function writeInfo() {\n const file = path.join(dir, '_registry.json');\n const json = JSON.stringify(out.info, null, 2);\n\n await writeFile(file, json, log);\n }\n\n const write = out.components.map(async (comp) => {\n const file = path.join(dir, `${comp.name}.json`);\n const json = JSON.stringify(comp, null, 2);\n\n await writeFile(file, json, log);\n });\n\n write.push(writeInfo());\n if ('registries' in out) {\n for (const child of out.registries) {\n write.push(\n writeFumadocsRegistry(child, {\n dir: path.join(dir, child.name),\n log: options.log,\n }),\n );\n }\n }\n\n await Promise.all(write);\n}\n\nasync function writeFile(file: string, content: string, log = true): Promise<void> {\n await fs.mkdir(path.dirname(file), { recursive: true });\n await fs.writeFile(file, content);\n\n if (log) {\n const size = (Buffer.byteLength(content) / 1024).toFixed(2);\n\n console.log(\n `${picocolors.greenBright('+')} ${path.relative(process.cwd(), file)} ${picocolors.dim(`${size} KB`)}`,\n );\n }\n}\n"],"mappings":";;;;;;;;;AAkFA,IAAa,mBAAb,MAA8B;CAI5B,YAAY,UAAoB;AAC9B,OAAK,MAAM;;CAGb,MAAc,kBAAoD;AAChE,MAAI,OAAO,KAAK,IAAI,gBAAgB,SAAU,QAAO,KAAK,IAAI;AAE9D,SAAOA,K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sourcesContent":["import * as fs from 'node:fs/promises';\nimport * as path from 'node:path';\nimport type {\n CompiledComponent,\n CompiledFile,\n httpSubComponent,\n registryInfoSchema,\n} from '@/registry/schema';\nimport type { z } from 'zod';\nimport { parse } from 'oxc-parser';\nimport { ResolverFactory } from 'oxc-resolver';\nimport MagicString from 'magic-string';\nimport { transformSpecifiers } from '@/utils/ast';\nimport { isRelative } from '@/utils/fs';\nimport type { DistributiveOmit } from '@/types';\nimport { encodeImport } from '@/registry/protocols/import';\n\nexport type OnResolve = (\n reference: SourceReference,\n from: { component: Component; file: ComponentFile },\n) => Reference;\n\nexport interface CompiledRegistry {\n name: string;\n components: CompiledComponent[];\n info: z.output<typeof registryInfoSchema>;\n}\n\nexport type ComponentFile = DistributiveOmit<CompiledFile, 'content'> & {\n path: string;\n};\n\nexport interface Component {\n name: string;\n title?: string;\n description?: string;\n files: ComponentFile[];\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n\n /**\n * Don't list the component in registry index file\n */\n unlisted?: boolean;\n\n /**\n * Map imported file paths, inherit from registry if not defined.\n */\n onResolve?: OnResolve;\n}\n\nexport interface PackageJson {\n dependencies?: Record<string, string>;\n devDependencies?: Record<string, string>;\n}\n\nexport interface Registry extends Omit<z.input<typeof registryInfoSchema>, 'indexes'> {\n name: string;\n packageJson: string | PackageJson;\n tsconfigPath: string;\n components: Component[];\n\n /**\n * The directory of registry, used to resolve relative paths\n */\n dir: string;\n\n /**\n * Map import paths of components\n */\n onResolve?: OnResolve;\n /**\n * When a referenced file is not found in component files, this function is called.\n * @returns file, or `false` to mark as external.\n */\n onUnknownFile?: (absolutePath: string) => ComponentFile | false | undefined;\n\n dependencies?: Record<string, string | null>;\n devDependencies?: Record<string, string | null>;\n}\n\nexport class RegistryCompiler {\n readonly raw: Registry;\n resolver!: RegistryResolver;\n\n constructor(registry: Registry) {\n this.raw = registry;\n }\n\n private async readPackageJson(): Promise<PackageJson | undefined> {\n if (typeof this.raw.packageJson !== 'string') return this.raw.packageJson;\n\n return fs\n .readFile(path.join(this.raw.dir, this.raw.packageJson))\n .then((res) => JSON.parse(res.toString()) as PackageJson)\n .catch(() => undefined);\n }\n\n async compile(): Promise<CompiledRegistry> {\n const registry = this.raw;\n this.resolver = new RegistryResolver(this, await this.readPackageJson());\n const output: CompiledRegistry = {\n name: registry.name,\n info: {\n indexes: [],\n unlistedIndexes: [],\n env: registry.env,\n variables: registry.variables,\n },\n components: [],\n };\n\n const builtComps = await Promise.all(\n registry.components.map(async (component) => {\n const compiler = new ComponentCompiler(this, component);\n\n return [component, await compiler.build()] as [Component, CompiledComponent];\n }),\n );\n\n for (const [input, comp] of builtComps) {\n const arr = input.unlisted ? output.info.unlistedIndexes : output.info.indexes;\n\n arr.push({\n name: input.name,\n title: input.title,\n description: input.description,\n });\n output.components.push(comp);\n }\n\n return output;\n }\n}\n\nclass RegistryResolver {\n private readonly deps: Record<string, string | null>;\n private readonly devDeps: Record<string, string | null>;\n private readonly fileToComponent = new Map<string, [Component, ComponentFile]>();\n readonly oxc: ResolverFactory;\n\n constructor(\n private readonly compiler: RegistryCompiler,\n packageJson: PackageJson = {},\n ) {\n const registry = compiler.raw;\n\n for (const comp of registry.components) {\n for (const file of comp.files) {\n if (this.fileToComponent.has(file.path))\n console.warn(\n `the same file ${file.path} exists in multiple component, you should make the shared file a separate component.`,\n );\n this.fileToComponent.set(file.path, [comp, file]);\n }\n }\n\n this.deps = {\n ...packageJson?.dependencies,\n ...registry.dependencies,\n };\n\n this.devDeps = {\n ...packageJson?.devDependencies,\n ...registry.devDependencies,\n };\n\n // resolve anything possible\n this.oxc = new ResolverFactory({\n extensions: ['.js', '.jsx', '.ts', '.tsx', '.node'],\n conditionNames: ['node', 'import', 'require', 'default', 'types'],\n tsconfig: {\n configFile: path.join(registry.dir, registry.tsconfigPath),\n },\n });\n }\n\n getDepFromSpecifier(specifier: string) {\n return specifier.startsWith('@')\n ? specifier.split('/').slice(0, 2).join('/')\n : specifier.split('/')[0];\n }\n\n getDepInfo(name: string):\n | {\n type: 'runtime' | 'dev';\n name: string;\n version: string | null;\n }\n | undefined {\n if (name in this.deps)\n return {\n name,\n type: 'runtime',\n version: this.deps[name],\n };\n\n if (name in this.devDeps)\n return {\n name,\n type: 'dev',\n version: this.devDeps[name],\n };\n\n console.warn(`dep info for ${name} cannot be found`);\n }\n\n getComponentByName(name: string): Component | undefined {\n return this.compiler.raw.components.find((comp) => comp.name === name);\n }\n\n getSubComponent(file: string) {\n const relativeFile = path.relative(this.compiler.raw.dir, file);\n const comp = this.fileToComponent.get(relativeFile);\n\n if (!comp) return;\n return {\n component: comp[0],\n file: comp[1],\n };\n }\n}\n\nexport type SourceReference =\n | {\n type: 'file';\n /**\n * Absolute path\n */\n file: string;\n }\n | {\n type: 'dependency';\n dep: string;\n specifier: string;\n }\n | {\n type: 'sub-component';\n resolved:\n | {\n type: 'local';\n component: Component;\n file: ComponentFile;\n }\n | {\n type: 'remote';\n component: Component;\n file: ComponentFile;\n registryName: string;\n };\n }\n | {\n type: 'unknown-specifier';\n specifier: string;\n };\n\nexport type Reference =\n | SourceReference\n | {\n type: 'custom';\n specifier: string;\n };\n\nexport class ComponentCompiler {\n private readonly processedFiles = new Set<string>();\n private readonly registry: Registry;\n private readonly subComponents = new Map<string, string | z.input<typeof httpSubComponent>>();\n private readonly devDependencies = new Map<string, string | null>();\n private readonly dependencies = new Map<string, string | null>();\n\n constructor(\n private readonly compiler: RegistryCompiler,\n private readonly component: Component,\n ) {\n this.registry = compiler.raw;\n }\n\n async build(): Promise<CompiledComponent> {\n const files = (\n await Promise.all(this.component.files.map((file) => this.onBuildFile(file)))\n ).flat();\n const dependencies = Object.fromEntries(this.dependencies);\n const devDependencies = Object.fromEntries(this.devDependencies);\n\n if (this.component.dependencies) {\n Object.assign(dependencies, this.component.dependencies);\n }\n if (this.component.devDependencies) {\n Object.assign(devDependencies, this.component.devDependencies);\n }\n\n return {\n name: this.component.name,\n title: this.component.title,\n description: this.component.description,\n files,\n subComponents: Array.from(this.subComponents.values()),\n dependencies,\n devDependencies,\n };\n }\n\n private async onBuildFile(file: ComponentFile): Promise<CompiledFile[]> {\n if (this.processedFiles.has(file.path)) return [];\n this.processedFiles.add(file.path);\n const resolver = this.compiler.resolver;\n\n const queue: ComponentFile[] = [];\n const result = await this.buildFile(file, (reference) => {\n const external = reference.type === 'dependency' && reference.dep === '@fumadocs/cli';\n\n if (external) {\n return reference.specifier;\n }\n\n if (reference.type === 'unknown-specifier') {\n if (!reference.specifier.startsWith('node:')) {\n console.warn(`Unknown specifier ${reference.specifier}, skipping for now`);\n }\n\n return reference.specifier;\n }\n\n if (reference.type === 'custom') return reference.specifier;\n\n if (reference.type === 'file') {\n const refFile = this.registry.onUnknownFile?.(reference.file);\n if (refFile) {\n queue.push(refFile);\n return encodeImport(refFile);\n }\n\n if (refFile === false) return;\n\n throw new Error(`Unknown file ${reference.file} referenced by ${file.path}`);\n }\n\n if (reference.type === 'sub-component') {\n const resolved = reference.resolved;\n if (resolved.component.name === this.component.name) return encodeImport(resolved.file);\n\n if (resolved.type === 'remote') {\n this.subComponents.set(`${resolved.registryName}:${resolved.component.name}`, {\n type: 'http',\n baseUrl: resolved.registryName,\n component: resolved.component.name,\n });\n } else {\n this.subComponents.set(resolved.component.name, resolved.component.name);\n }\n\n return encodeImport(resolved.file);\n }\n\n const dep = resolver.getDepInfo(reference.dep);\n if (dep) {\n const map = dep.type === 'dev' ? this.devDependencies : this.dependencies;\n map.set(dep.name, dep.version);\n }\n\n return reference.specifier;\n });\n\n return [result, ...(await Promise.all(queue.map((file) => this.onBuildFile(file)))).flat()];\n }\n\n private async buildFile(\n file: ComponentFile,\n /**\n * write references back to import specifiers\n *\n * keep original one if `undefined`\n */\n writeReference: (reference: Reference) => string | undefined,\n ): Promise<CompiledFile> {\n const sourceFilePath = path.join(this.registry.dir, file.path);\n const astTypes: Record<string, 'js' | 'ts' | undefined> = {\n '.ts': 'ts',\n '.tsx': 'ts',\n '.js': 'js',\n '.jsx': 'js',\n };\n const astType = astTypes[path.extname(file.path)];\n const content = (await fs.readFile(sourceFilePath)).toString();\n\n if (!astType) {\n return {\n ...file,\n content,\n };\n }\n\n const resolver = this.compiler.resolver;\n\n const ast = await parse(sourceFilePath, content, {\n astType,\n });\n\n if (ast.errors.length > 0) {\n throw new Error(`failed to parse file ${sourceFilePath}: \\n${ast.errors.join('\\n')}`);\n }\n\n const s = new MagicString(content);\n const ctx = { component: this.component, file };\n // Process import paths\n transformSpecifiers(ast.program, s, (specifier) => {\n let resolved: Reference = {\n type: 'unknown-specifier',\n specifier: specifier,\n };\n const onResolve = this.component.onResolve ?? this.registry.onResolve;\n const resolvedSpecifier = resolver.oxc.resolveFileSync(sourceFilePath, specifier);\n if (resolvedSpecifier.error || !resolvedSpecifier.path) {\n return writeReference(onResolve ? onResolve(resolved, ctx) : resolved);\n }\n\n resolved = {\n type: 'file',\n file: resolvedSpecifier.path,\n };\n\n // outside of registry dir\n if (!isRelative(this.registry.dir, resolvedSpecifier.path)) {\n resolved = {\n type: 'dependency',\n dep: resolver.getDepFromSpecifier(specifier),\n specifier,\n };\n } else {\n const sub = resolver.getSubComponent(resolvedSpecifier.path);\n if (sub) {\n resolved = {\n type: 'sub-component',\n resolved: {\n type: 'local',\n component: sub.component,\n file: sub.file,\n },\n };\n }\n }\n\n return writeReference(onResolve ? onResolve(resolved, ctx) : resolved);\n });\n\n return {\n ...file,\n content: s.toString(),\n };\n }\n}\n\nexport function resolveFromRemote(\n r: Registry,\n component: string,\n selectFile: (file: ComponentFile) => boolean,\n): Reference | undefined {\n const comp = r.components.find((comp) => comp.name === component);\n if (!comp) return;\n const file = comp.files.find(selectFile);\n if (!file) return;\n\n return {\n type: 'sub-component',\n resolved: {\n type: 'remote',\n registryName: r.name,\n component: comp,\n file,\n },\n };\n}\n","import * as fs from 'node:fs/promises';\nimport * as path from 'node:path';\nimport picocolors from 'picocolors';\nimport type { CompiledRegistry } from '@/build/compiler';\n\nexport * from './compiler';\n\nexport interface MonoRegistry extends CompiledRegistry {\n registries: CompiledRegistry[];\n}\n\nexport function combineRegistry(\n root: CompiledRegistry,\n ...items: CompiledRegistry[]\n): MonoRegistry {\n return {\n ...root,\n info: {\n ...root.info,\n registries: items.map((item) => item.name),\n },\n registries: items,\n };\n}\n\nexport async function writeFumadocsRegistry(\n out: CompiledRegistry | MonoRegistry,\n options: {\n dir: string;\n\n /**\n * Remove previous outputs\n *\n * @defaultValue false\n */\n cleanDir?: boolean;\n\n log?: boolean;\n },\n): Promise<void> {\n const { dir, cleanDir = false, log = true } = options;\n\n if (cleanDir) {\n await fs.rm(dir, {\n recursive: true,\n force: true,\n });\n console.log(picocolors.bold(picocolors.greenBright('Cleaned directory')));\n }\n\n async function writeInfo() {\n const file = path.join(dir, '_registry.json');\n const json = JSON.stringify(out.info, null, 2);\n\n await writeFile(file, json, log);\n }\n\n const write = out.components.map(async (comp) => {\n const file = path.join(dir, `${comp.name}.json`);\n const json = JSON.stringify(comp, null, 2);\n\n await writeFile(file, json, log);\n });\n\n write.push(writeInfo());\n if ('registries' in out) {\n for (const child of out.registries) {\n write.push(\n writeFumadocsRegistry(child, {\n dir: path.join(dir, child.name),\n log: options.log,\n }),\n );\n }\n }\n\n await Promise.all(write);\n}\n\nasync function writeFile(file: string, content: string, log = true): Promise<void> {\n await fs.mkdir(path.dirname(file), { recursive: true });\n await fs.writeFile(file, content);\n\n if (log) {\n const size = (Buffer.byteLength(content) / 1024).toFixed(2);\n\n console.log(\n `${picocolors.greenBright('+')} ${path.relative(process.cwd(), file)} ${picocolors.dim(`${size} KB`)}`,\n );\n }\n}\n"],"mappings":";;;;;;;;;AAiFA,IAAa,mBAAb,MAA8B;CAI5B,YAAY,UAAoB;AAC9B,OAAK,MAAM;;CAGb,MAAc,kBAAoD;AAChE,MAAI,OAAO,KAAK,IAAI,gBAAgB,SAAU,QAAO,KAAK,IAAI;AAE9D,SAAOA,KACJ,SAASC,OAAK,KAAK,KAAK,IAAI,KAAK,KAAK,IAAI,YAAY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e,YAAY;AAIzB,QAAM,UAHOC,OAAK,KAAK,KAAK,iBAAiB,EAChC,KAAK,UAAU,IAAI,MAAM,MAAM,EAAE,EAElB,IAAI;;CAGlC,MAAM,QAAQ,IAAI,WAAW,IAAI,OAAO,SAAS;AAI/C,QAAM,UAHOA,OAAK,KAAK,KAAK,GAAG,KAAK,KAAK,OAAO,EACnC,KAAK,UAAU,MAAM,MAAM,EAAE,EAEd,IAAI;GAChC;AAEF,OAAM,KAAK,WAAW,CAAC;AACvB,KAAI,gBAAgB,IAClB,MAAK,MAAM,SAAS,IAAI,WACtB,OAAM,KACJ,sBAAsB,OAAO;EAC3B,KAAKA,OAAK,KAAK,KAAK,MAAM,KAAK;EAC/B,KAAK,QAAQ;EACd,CAAC,CACH;AAIL,OAAM,QAAQ,IAAI,MAAM;;AAG1B,eAAe,UAAU,MAAc,SAAiB,MAAM,MAAqB;AACjF,OAAMD,KAAG,MAAMC,OAAK,QAAQ,KAAK,EAAE,EAAE,WAAW,MAAM,CAAC;AACvD,OAAMD,KAAG,UAAU,MAAM,QAAQ;AAEjC,KAAI,KAAK;EACP,MAAM,QAAQ,OAAO,WAAW,QAAQ,GAAG,MAAM,QAAQ,EAAE;AAE3D,UAAQ,IACN,GAAG,WAAW,YAAY,IAAI,CAAC,GAAGC,OAAK,SAAS,QAAQ,KAAK,EAAE,KAAK,CAAC,GAAG,WAAW,IAAI,GAAG,KAAK,KAAK,GACrG"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client-
|
|
1
|
+
{"version":3,"file":"client-C2A4Jf2w.js","names":[],"sources":["../src/utils/cache.ts","../src/registry/client.ts"],"sourcesContent":["export interface AsyncCache<V> {\n cached: (\n key: string,\n fn: (\n /**\n * set a cache value before the compute function completes.\n *\n * useful to handle recursive access.\n */\n presolve: (v: V) => void,\n ) => V | Promise<V>,\n ) => V | Promise<V>;\n $value: <T>() => AsyncCache<T>;\n invalidate: (key: string) => void;\n}\n\n/**\n * cache for async resources, finished promises will be resolved into original value, otherwise wrapped with a promise.\n */\nexport function createCache<V>(store = new Map<string, V | Promise<V>>()): AsyncCache<V> {\n return {\n cached(key, fn) {\n let cached = store.get(key);\n if (cached) return cached;\n\n cached = fn((v) => store.set(key, v));\n if (cached instanceof Promise) {\n cached = cached.then((out) => {\n // replace with resolved if still exists\n if (store.has(key)) {\n store.set(key, out);\n }\n\n return out;\n });\n }\n store.set(key, cached);\n return cached;\n },\n invalidate(key) {\n store.delete(key);\n },\n $value<T>() {\n return this as unknown as AsyncCache<T>;\n },\n };\n}\n","import {\n type DownloadedRegistryInfo,\n type Component,\n componentSchema,\n registryInfoSchema,\n} from '@/registry/schema';\nimport path from 'node:path';\nimport fs from 'node:fs/promises';\nimport type { LoadedConfig } from '@/config';\nimport { createCache } from '@/utils/cache';\n\nexport interface RegistryClient {\n readonly registryId: string;\n readonly config: LoadedConfig;\n fetchRegistryInfo: () => Promise<DownloadedRegistryInfo>;\n fetchComponent: (name: string) => Promise<Component>;\n hasComponent: (name: string) => Promise<boolean>;\n createLinkedRegistryClient: (registryName: string) => RegistryClient;\n}\n\nconst fetchCache = createCache<unknown>();\n\nexport class HttpRegistryClient implements RegistryClient {\n readonly registryId: string;\n\n constructor(\n readonly baseUrl: string,\n readonly config: LoadedConfig,\n ) {\n this.registryId = baseUrl;\n }\n\n async fetchRegistryInfo(baseUrl = this.baseUrl) {\n const url = new URL('_registry.json', `${baseUrl}/`);\n\n return fetchCache.$value<DownloadedRegistryInfo>().cached(url.href, async () => {\n const res = await fetch(url);\n if (!res.ok) {\n throw new Error(`failed to fetch ${url.href}: ${res.statusText}`);\n }\n\n return registryInfoSchema.parse(await res.json());\n });\n }\n\n async fetchComponent(name: string) {\n const url = new URL(`${name}.json`, `${this.baseUrl}/`);\n\n return fetchCache.$value<Component>().cached(url.href, async () => {\n const res = await fetch(`${this.baseUrl}/${name}.json`);\n if (!res.ok) {\n if (res.status === 404) {\n throw new Error(`component ${name} not found at ${url.href}`);\n }\n throw new Error(await res.text());\n }\n\n return componentSchema.parse(await res.json());\n });\n }\n\n async hasComponent(name: string) {\n const url = new URL(`${name}.json`, `${this.baseUrl}/`);\n const res = await fetch(url, { method: 'HEAD' });\n return res.ok;\n }\n\n createLinkedRegistryClient(name: string) {\n return new HttpRegistryClient(`${this.baseUrl}/${name}`, this.config);\n }\n}\n\nexport class LocalRegistryClient implements RegistryClient {\n readonly registryId: string;\n private registryInfo: DownloadedRegistryInfo | undefined;\n\n constructor(\n private readonly dir: string,\n readonly config: LoadedConfig,\n ) {\n this.registryId = dir;\n }\n\n async fetchRegistryInfo(dir = this.dir) {\n if (this.registryInfo) return this.registryInfo;\n\n const filePath = path.join(dir, '_registry.json');\n const out = await fs\n .readFile(filePath)\n .then((res) => JSON.parse(res.toString()))\n .catch((e) => {\n throw new Error(`failed to resolve local file \"${filePath}\"`, {\n cause: e,\n });\n });\n\n return (this.registryInfo = registryInfoSchema.parse(out));\n }\n\n async fetchComponent(name: string) {\n const filePath = path.join(this.dir, `${name}.json`);\n const out = await fs\n .readFile(filePath)\n .then((res) => JSON.parse(res.toString()))\n .catch((e) => {\n throw new Error(`component ${name} not found at ${filePath}`, { cause: e });\n });\n\n return componentSchema.parse(out);\n }\n\n async hasComponent(name: string) {\n const filePath = path.join(this.dir, `${name}.json`);\n try {\n await fs.stat(filePath);\n return true;\n } catch {\n return false;\n }\n }\n\n createLinkedRegistryClient(name: string) {\n return new LocalRegistryClient(path.join(this.dir, name), this.config);\n }\n}\n"],"mappings":";;;;;;;AAmBA,SAAgB,YAAe,wBAAQ,IAAI,KAA6B,EAAiB;AACvF,QAAO;EACL,OAAO,KAAK,IAAI;GACd,IAAI,SAAS,MAAM,IAAI,IAAI;AAC3B,OAAI,OAAQ,QAAO;AAEnB,YAAS,IAAI,MAAM,MAAM,IAAI,KAAK,EAAE,CAAC;AACrC,OAAI,kBAAkB,QACpB,UAAS,OAAO,MAAM,QAAQ;AAE5B,QAAI,MAAM,IAAI,IAAI,CAChB,OAAM,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|
4
|
-
declare
|
|
4
|
+
declare const frameworks: readonly ["next", "waku", "react-router", "tanstack-start"];
|
|
5
|
+
type Framework = (typeof frameworks)[number];
|
|
6
|
+
declare function createConfigSchema(): z.ZodObject<{
|
|
5
7
|
$schema: z.ZodOptional<z.ZodDefault<z.ZodString>>;
|
|
6
8
|
aliases: z.ZodDefault<z.ZodObject<{
|
|
7
9
|
uiDir: z.ZodDefault<z.ZodString>;
|
|
@@ -15,6 +17,7 @@ declare function createConfigSchema(isSrc: boolean): z.ZodObject<{
|
|
|
15
17
|
"radix-ui": "radix-ui";
|
|
16
18
|
"base-ui": "base-ui";
|
|
17
19
|
}>>;
|
|
20
|
+
framework: z.ZodDefault<z.ZodLiteral<"next" | "waku" | "react-router" | "tanstack-start">>;
|
|
18
21
|
commands: z.ZodDefault<z.ZodObject<{
|
|
19
22
|
format: z.ZodOptional<z.ZodString>;
|
|
20
23
|
}, z.core.$strip>>;
|
|
@@ -28,8 +31,8 @@ declare function createOrLoadConfig(file?: string): Promise<LoadedConfig>;
|
|
|
28
31
|
*
|
|
29
32
|
* @returns the created config, `undefined` if not created
|
|
30
33
|
*/
|
|
31
|
-
declare function initConfig(file?: string
|
|
32
|
-
declare function getDefaultConfig(
|
|
34
|
+
declare function initConfig(file?: string): Promise<LoadedConfig | undefined>;
|
|
35
|
+
declare function getDefaultConfig(): Promise<{
|
|
33
36
|
aliases: {
|
|
34
37
|
uiDir: string;
|
|
35
38
|
componentsDir: string;
|
|
@@ -39,11 +42,12 @@ declare function getDefaultConfig(src?: boolean): Promise<{
|
|
|
39
42
|
};
|
|
40
43
|
baseDir: string;
|
|
41
44
|
uiLibrary: "radix-ui" | "base-ui";
|
|
45
|
+
framework: "next" | "waku" | "react-router" | "tanstack-start";
|
|
42
46
|
commands: {
|
|
43
47
|
format?: string | undefined;
|
|
44
48
|
};
|
|
45
49
|
$schema?: string | undefined;
|
|
46
50
|
}>;
|
|
47
51
|
//#endregion
|
|
48
|
-
export {
|
|
49
|
-
//# sourceMappingURL=config-
|
|
52
|
+
export { createOrLoadConfig as a, createConfigSchema as i, Framework as n, getDefaultConfig as o, LoadedConfig as r, initConfig as s, ConfigInput as t };
|
|
53
|
+
//# sourceMappingURL=config-Bx-m6awG.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config-Bx-m6awG.d.ts","names":[],"sources":["../src/config.ts"],"mappings":";;;cAIM,UAAA;AAAA,KACM,SAAA,WAAoB,UAAA;AAAA,iBAEhB,kBAAA,CAAA,GAAkB,CAAA,CAAA,SAAA;;;;;;;;;;;;;;;;;;;KAyD7B,YAAA,GAAe,UAAA,QAAkB,kBAAA;AAAA,KAE1B,WAAA,GAAc,CAAA,CAAE,KAAA,CAAM,YAAA;AAAA,KACtB,YAAA,GAAe,CAAA,CAAE,MAAA,CAAO,YAAA;AAAA,iBAEd,kBAAA,CAAmB,IAAA,YAAsB,OAAA,CAAQ,YAAA;;;;;;iBAejD,UAAA,CAAW,IAAA,YAAsB,OAAA,CAAQ,YAAA;AAAA,iBAezC,gBAAA,CAAA,GAAgB,OAAA"}
|
package/dist/config.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
export { ConfigInput, LoadedConfig, createConfigSchema, createOrLoadConfig, getDefaultConfig, initConfig };
|
|
1
|
+
import { a as createOrLoadConfig, i as createConfigSchema, n as Framework, o as getDefaultConfig, r as LoadedConfig, s as initConfig, t as ConfigInput } from "./config-Bx-m6awG.js";
|
|
2
|
+
export { ConfigInput, Framework, LoadedConfig, createConfigSchema, createOrLoadConfig, getDefaultConfig, initConfig };
|
package/dist/config.js
CHANGED
|
@@ -1,2 +1,75 @@
|
|
|
1
|
-
import
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
//#region src/config.ts
|
|
5
|
+
const frameworks = [
|
|
6
|
+
"next",
|
|
7
|
+
"waku",
|
|
8
|
+
"react-router",
|
|
9
|
+
"tanstack-start"
|
|
10
|
+
];
|
|
11
|
+
function createConfigSchema() {
|
|
12
|
+
const defaultAliases = {
|
|
13
|
+
uiDir: "./components/ui",
|
|
14
|
+
componentsDir: "./components",
|
|
15
|
+
layoutDir: "./layouts",
|
|
16
|
+
cssDir: "./styles",
|
|
17
|
+
libDir: "./lib"
|
|
18
|
+
};
|
|
19
|
+
return z.object({
|
|
20
|
+
$schema: z.string().default("node_modules/@fumadocs/cli/dist/schema.json").optional(),
|
|
21
|
+
aliases: z.object({
|
|
22
|
+
uiDir: z.string().default(defaultAliases.uiDir),
|
|
23
|
+
componentsDir: z.string().default(defaultAliases.uiDir),
|
|
24
|
+
layoutDir: z.string().default(defaultAliases.layoutDir),
|
|
25
|
+
cssDir: z.string().default(defaultAliases.componentsDir),
|
|
26
|
+
libDir: z.string().default(defaultAliases.libDir)
|
|
27
|
+
}).default(defaultAliases),
|
|
28
|
+
baseDir: z.string().default(() => existsSync("./src") ? "src" : ""),
|
|
29
|
+
uiLibrary: z.enum(["radix-ui", "base-ui"]).default("radix-ui"),
|
|
30
|
+
framework: z.literal(frameworks).default(() => {
|
|
31
|
+
return detectFrameworkFromPackageJson() ?? "next";
|
|
32
|
+
}),
|
|
33
|
+
commands: z.object({ format: z.string().optional() }).default({})
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
function detectFrameworkFromPackageJson(pkgPath = "./package.json") {
|
|
37
|
+
try {
|
|
38
|
+
const pkgRaw = readFileSync(pkgPath, "utf-8");
|
|
39
|
+
const pkg = JSON.parse(pkgRaw);
|
|
40
|
+
const deps = {
|
|
41
|
+
...pkg.dependencies,
|
|
42
|
+
...pkg.devDependencies
|
|
43
|
+
};
|
|
44
|
+
if (deps["next"]) return "next";
|
|
45
|
+
if (deps["waku"]) return "waku";
|
|
46
|
+
if (deps["react-router"] || deps["react-router-dom"]) return "react-router";
|
|
47
|
+
if (deps["@tanstack/react-start"]) return "tanstack-start";
|
|
48
|
+
} catch {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function createOrLoadConfig(file = "./cli.json") {
|
|
53
|
+
const inited = await initConfig(file);
|
|
54
|
+
if (inited) return inited;
|
|
55
|
+
const content = (await fs.readFile(file)).toString();
|
|
56
|
+
return createConfigSchema().parse(JSON.parse(content));
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Write new config, skip if a config already exists
|
|
60
|
+
*
|
|
61
|
+
* @returns the created config, `undefined` if not created
|
|
62
|
+
*/
|
|
63
|
+
async function initConfig(file = "./cli.json") {
|
|
64
|
+
if (await fs.stat(file).then(() => true).catch(() => false)) return;
|
|
65
|
+
const defaultConfig = await getDefaultConfig();
|
|
66
|
+
await fs.writeFile(file, JSON.stringify(defaultConfig, null, 2));
|
|
67
|
+
return defaultConfig;
|
|
68
|
+
}
|
|
69
|
+
async function getDefaultConfig() {
|
|
70
|
+
return createConfigSchema().parse({});
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
2
73
|
export { createConfigSchema, createOrLoadConfig, getDefaultConfig, initConfig };
|
|
74
|
+
|
|
75
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","names":[],"sources":["../src/config.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport { z } from 'zod';\nimport { existsSync, readFileSync } from 'node:fs';\n\nconst frameworks = ['next', 'waku', 'react-router', 'tanstack-start'] as const;\nexport type Framework = (typeof frameworks)[number];\n\nexport function createConfigSchema() {\n const defaultAliases = {\n uiDir: './components/ui',\n componentsDir: './components',\n layoutDir: './layouts',\n cssDir: './styles',\n libDir: './lib',\n };\n\n return z.object({\n $schema: z.string().default('node_modules/@fumadocs/cli/dist/schema.json').optional(),\n aliases: z\n .object({\n uiDir: z.string().default(defaultAliases.uiDir),\n componentsDir: z.string().default(defaultAliases.uiDir),\n layoutDir: z.string().default(defaultAliases.layoutDir),\n cssDir: z.string().default(defaultAliases.componentsDir),\n libDir: z.string().default(defaultAliases.libDir),\n })\n .default(defaultAliases),\n\n baseDir: z.string().default(() => (existsSync('./src') ? 'src' : '')),\n uiLibrary: z.enum(['radix-ui', 'base-ui']).default('radix-ui'),\n framework: z.literal(frameworks).default(() => {\n return detectFrameworkFromPackageJson() ?? 'next';\n }),\n\n commands: z\n .object({\n /**\n * command to format output code automatically\n */\n format: z.string().optional(),\n })\n .default({}),\n });\n}\n\nfunction detectFrameworkFromPackageJson(pkgPath = './package.json'): Framework | undefined {\n try {\n const pkgRaw = readFileSync(pkgPath, 'utf-8');\n const pkg = JSON.parse(pkgRaw);\n\n const deps = {\n ...pkg.dependencies,\n ...pkg.devDependencies,\n };\n\n if (deps['next']) return 'next';\n if (deps['waku']) return 'waku';\n if (deps['react-router'] || deps['react-router-dom']) return 'react-router';\n if (deps['@tanstack/react-start']) return 'tanstack-start';\n } catch {\n return;\n }\n}\n\ntype ConfigSchema = ReturnType<typeof createConfigSchema>;\n\nexport type ConfigInput = z.input<ConfigSchema>;\nexport type LoadedConfig = z.output<ConfigSchema>;\n\nexport async function createOrLoadConfig(file = './cli.json'): Promise<LoadedConfig> {\n const inited = await initConfig(file);\n if (inited) return inited;\n\n const content = (await fs.readFile(file)).toString();\n const configSchema = createConfigSchema();\n\n return configSchema.parse(JSON.parse(content));\n}\n\n/**\n * Write new config, skip if a config already exists\n *\n * @returns the created config, `undefined` if not created\n */\nexport async function initConfig(file = './cli.json'): Promise<LoadedConfig | undefined> {\n if (\n await fs\n .stat(file)\n .then(() => true)\n .catch(() => false)\n ) {\n return;\n }\n\n const defaultConfig = await getDefaultConfig();\n await fs.writeFile(file, JSON.stringify(defaultConfig, null, 2));\n return defaultConfig;\n}\n\nexport async function getDefaultConfig() {\n return createConfigSchema().parse({} satisfies ConfigInput);\n}\n"],"mappings":";;;;AAIA,MAAM,aAAa;CAAC;CAAQ;CAAQ;CAAgB;CAAiB;AAGrE,SAAgB,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AAO;CAEnB,MAAM,WAAW,MAAM,GAAG,SAAS,KAAK,EAAE,UAAU;AAGpD,QAFqB,oBAAoB,CAErB,MAAM,KAAK,MAAM,QAAQ,CAAC;;;;;;;AAQhD,eAAsB,WAAW,OAAO,cAAiD;AACvF,KACE,MAAM,GACH,KAAK,KAAK,CACV,WAAW,KAAK,CAChB,YAAY,MAAM,CAErB;CAGF,MAAM,gBAAgB,MAAM,kBAAkB;AAC9C,OAAM,GAAG,UAAU,MAAM,KAAK,UAAU,eAAe,MAAM,EAAE,CAAC;AAChE,QAAO;;AAGT,eAAsB,mBAAmB;AACvC,QAAO,oBAAoB,CAAC,MAAM,EAAE,CAAuB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fs-
|
|
1
|
+
{"version":3,"file":"fs-C3j4H046.js","names":[],"sources":["../src/utils/fs.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nexport async function exists(pathLike: string): Promise<boolean> {\n try {\n await fs.access(pathLike);\n return true;\n } catch {\n return false;\n }\n}\n\nexport function isRelative(from: string, to: string): boolean {\n return !path.relative(from, to).startsWith(`..${path.sep}`);\n}\n"],"mappings":";;;AAGA,eAAsB,OAAO,UAAoC;AAC/D,KAAI;AACF,QAAM,GAAG,OAAO,SAAS;AACzB,SAAO;SACD;AACN,SAAO;;;AAIX,SAAgB,WAAW,MAAc,IAAqB;AAC5D,QAAO,CAAC,KAAK,SAAS,MAAM,GAAG,CAAC,WAAW,KAAK,KAAK,MAAM"}
|
|
@@ -64,7 +64,61 @@ function transformSpecifiers(program, s, transformSpecifier) {
|
|
|
64
64
|
}
|
|
65
65
|
}).visit(program);
|
|
66
66
|
}
|
|
67
|
+
function getImportedBinding(spec) {
|
|
68
|
+
if (spec.type === "ImportSpecifier") {
|
|
69
|
+
let imported;
|
|
70
|
+
switch (spec.imported.type) {
|
|
71
|
+
case "Identifier":
|
|
72
|
+
imported = spec.imported.name;
|
|
73
|
+
break;
|
|
74
|
+
case "Literal":
|
|
75
|
+
imported = spec.imported.value;
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
imported,
|
|
80
|
+
local: spec.local.name
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if (spec.type === "ImportDefaultSpecifier") return {
|
|
84
|
+
imported: "default",
|
|
85
|
+
local: spec.local.name
|
|
86
|
+
};
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
function collectMacroBindings(program, name) {
|
|
90
|
+
const locals = /* @__PURE__ */ new Set();
|
|
91
|
+
const importDecls = [];
|
|
92
|
+
const seenDecl = /* @__PURE__ */ new Set();
|
|
93
|
+
new Visitor({ ImportDeclaration(node) {
|
|
94
|
+
for (const spec of node.specifiers) {
|
|
95
|
+
const b = getImportedBinding(spec);
|
|
96
|
+
if (!b || b.imported !== name) continue;
|
|
97
|
+
locals.add(b.local);
|
|
98
|
+
if (!seenDecl.has(node)) {
|
|
99
|
+
seenDecl.add(node);
|
|
100
|
+
importDecls.push(node);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
} }).visit(program);
|
|
104
|
+
if (locals.size === 0) return null;
|
|
105
|
+
return {
|
|
106
|
+
importDecls,
|
|
107
|
+
locals
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region src/registry/protocols/import.ts
|
|
112
|
+
function encodeImport(file) {
|
|
113
|
+
if (file.type === "route-handler") return `route-handler:${file.route}`;
|
|
114
|
+
return `:${file.target ?? file.path}`;
|
|
115
|
+
}
|
|
116
|
+
function decodeImport(s) {
|
|
117
|
+
if (s.startsWith(":")) return { target: s.slice(1) };
|
|
118
|
+
if (s.startsWith("route-handler:")) return { route: s.slice(14) };
|
|
119
|
+
return { raw: s };
|
|
120
|
+
}
|
|
67
121
|
//#endregion
|
|
68
|
-
export { transformSpecifiers as n, typescriptExtensions as r,
|
|
122
|
+
export { transformSpecifiers as a, toImportSpecifier as i, encodeImport as n, typescriptExtensions as o, collectMacroBindings as r, decodeImport as t };
|
|
69
123
|
|
|
70
|
-
//# sourceMappingURL=
|
|
124
|
+
//# sourceMappingURL=import-CSbSteB3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"import-CSbSteB3.js","names":[],"sources":["../src/constants.ts","../src/utils/ast.ts","../src/registry/protocols/import.ts"],"sourcesContent":["export const typescriptExtensions = ['.ts', '.tsx', '.js', '.jsx'];\n","import path from 'node:path';\nimport { typescriptExtensions } from '@/constants';\nimport { type Program, Visitor } from 'oxc-parser';\nimport type MagicString from 'magic-string';\nimport type { ImportDeclaration, ImportDeclarationSpecifier } from '@oxc-project/types';\n\n/**\n * Return the import modifier for `sourceFile` to import `referenceFile`\n *\n * @example\n * ```ts\n * toReferencePath('index.ts', 'dir/hello.ts')\n * // should output './dir/hello'\n * ```\n */\nexport function toImportSpecifier(sourceFile: string, referenceFile: string): string {\n const extname = path.extname(referenceFile);\n const removeExt = typescriptExtensions.includes(extname);\n\n let importPath = path\n .relative(\n path.dirname(sourceFile),\n removeExt ? referenceFile.substring(0, referenceFile.length - extname.length) : referenceFile,\n )\n .replaceAll(path.sep, '/');\n\n if (removeExt && importPath.endsWith('/index')) {\n importPath = importPath.slice(0, -'/index'.length);\n }\n\n return importPath.startsWith('../') ? importPath : `./${importPath}`;\n}\n\nexport function transformSpecifiers(\n program: Program,\n s: MagicString,\n transformSpecifier: (value: string) => string | undefined,\n) {\n new Visitor({\n // static imports\n ImportDeclaration(node) {\n const source = node.source;\n const out = transformSpecifier(source.value);\n if (out) {\n s.update(source.start + 1, source.end - 1, out);\n source.value = out;\n }\n },\n // dynamic imports\n ImportExpression(node) {\n if (node.source.type === 'Literal') {\n const source = node.source;\n const out = transformSpecifier(source.value as string);\n if (out) {\n s.update(source.start + 1, source.end - 1, out);\n source.value = out;\n }\n }\n },\n // exports\n ExportAllDeclaration(node) {\n const source = node.source;\n const out = transformSpecifier(source.value);\n if (out) {\n s.update(source.start + 1, source.end - 1, out);\n source.value = out;\n }\n },\n ExportNamedDeclaration(node) {\n const source = node.source;\n if (!source) return;\n const out = transformSpecifier(source.value);\n if (out) {\n s.update(source.start + 1, source.end - 1, out);\n source.value = out;\n }\n },\n }).visit(program);\n}\n\nfunction getImportedBinding(\n spec: ImportDeclarationSpecifier,\n): { imported: string; local: string } | null {\n if (spec.type === 'ImportSpecifier') {\n let imported: string;\n switch (spec.imported.type) {\n case 'Identifier':\n imported = spec.imported.name;\n break;\n case 'Literal':\n imported = spec.imported.value;\n break;\n }\n\n return {\n imported,\n local: spec.local.name,\n };\n }\n if (spec.type === 'ImportDefaultSpecifier') {\n return { imported: 'default', local: spec.local.name };\n }\n return null;\n}\n\nexport function collectMacroBindings(\n program: Program,\n name: string,\n): { importDecls: ImportDeclaration[]; locals: Set<string> } | null {\n const locals = new Set<string>();\n const importDecls: ImportDeclaration[] = [];\n const seenDecl = new Set<ImportDeclaration>();\n\n new Visitor({\n ImportDeclaration(node: ImportDeclaration) {\n for (const spec of node.specifiers) {\n const b = getImportedBinding(spec);\n if (!b || b.imported !== name) continue;\n\n locals.add(b.local);\n\n if (!seenDecl.has(node)) {\n seenDecl.add(node);\n importDecls.push(node);\n }\n }\n },\n }).visit(program);\n\n if (locals.size === 0) return null;\n return { importDecls, locals };\n}\n","import type { ComponentFile } from '@/build';\nimport type { CompiledFile } from '../schema';\n\nexport type ImportInfo =\n | {\n raw: string;\n }\n | {\n target: string;\n }\n | {\n route: string;\n };\n\nexport function encodeImport(file: ComponentFile | CompiledFile): string {\n if (file.type === 'route-handler') return `route-handler:${file.route}`;\n return `:${file.target ?? file.path}`;\n}\n\nexport function decodeImport(s: string): ImportInfo {\n if (s.startsWith(':'))\n return {\n target: s.slice(1),\n };\n\n if (s.startsWith('route-handler:'))\n return {\n route: s.slice('route-handler:'.length),\n };\n\n return { raw: s };\n}\n"],"mappings":";;;AAAA,MAAa,uBAAuB;CAAC;CAAO;CAAQ;CAAO;CAAO;;;;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/index.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { t as ComponentInstaller } from "./installer-B3-my9zN.js";
|
|
2
|
+
import { createOrLoadConfig, initConfig } from "./config.js";
|
|
3
|
+
import { n as LocalRegistryClient, t as HttpRegistryClient } from "./client-C2A4Jf2w.js";
|
|
4
|
+
import { t as ComponentInstaller } from "./installer-BWoLnsXE.js";
|
|
5
|
+
import { t as exists } from "./fs-C3j4H046.js";
|
|
7
6
|
import fs from "node:fs/promises";
|
|
8
7
|
import path from "node:path";
|
|
9
8
|
import { Command } from "commander";
|
|
10
9
|
import picocolors from "picocolors";
|
|
11
10
|
import { x } from "tinyexec";
|
|
12
11
|
import { autocompleteMultiselect, box, cancel, confirm, group, intro, isCancel, log, outro, select, spinner } from "@clack/prompts";
|
|
12
|
+
import { detect } from "package-manager-detector";
|
|
13
13
|
import { exec } from "node:child_process";
|
|
14
14
|
import { promisify } from "node:util";
|
|
15
15
|
//#region src/commands/file-tree.ts
|
|
@@ -63,7 +63,7 @@ async function runTree(args) {
|
|
|
63
63
|
}
|
|
64
64
|
//#endregion
|
|
65
65
|
//#region package.json
|
|
66
|
-
var version = "1.3.
|
|
66
|
+
var version = "1.3.3";
|
|
67
67
|
//#endregion
|
|
68
68
|
//#region src/commands/shared.ts
|
|
69
69
|
const UIRegistries = {
|
|
@@ -108,12 +108,9 @@ function pluginPreserveLayouts() {
|
|
|
108
108
|
$subComponents: comp.$subComponents.filter((child) => !layoutNameSet.has(child.name))
|
|
109
109
|
};
|
|
110
110
|
},
|
|
111
|
-
|
|
112
|
-
if (layoutNameSet.has(stack[0].name)) return;
|
|
113
|
-
|
|
114
|
-
if (!(specifier in layoutComps)) return specifier;
|
|
115
|
-
return `fumadocs-ui/${layoutComps[specifier]}`;
|
|
116
|
-
});
|
|
111
|
+
transformImport(specifier, { stack }) {
|
|
112
|
+
if (layoutNameSet.has(stack[0].name) || !(specifier in layoutComps)) return specifier;
|
|
113
|
+
return `fumadocs-ui/${layoutComps[specifier]}`;
|
|
117
114
|
}
|
|
118
115
|
};
|
|
119
116
|
}
|
|
@@ -189,7 +186,8 @@ async function install(target, installer) {
|
|
|
189
186
|
if (deps.hasRequired()) {
|
|
190
187
|
log.message();
|
|
191
188
|
box([...deps.dependencies, ...deps.devDependencies].join("\n"), "New Dependencies");
|
|
192
|
-
const
|
|
189
|
+
const pm = (await detect())?.name ?? "npm";
|
|
190
|
+
const value = await confirm({ message: `Do you want to install with ${pm}?` });
|
|
193
191
|
if (isCancel(value)) {
|
|
194
192
|
outro("Installation terminated");
|
|
195
193
|
process.exit(0);
|
|
@@ -197,9 +195,9 @@ async function install(target, installer) {
|
|
|
197
195
|
if (value) {
|
|
198
196
|
const spin = spinner({ errorMessage: "Failed to install dependencies" });
|
|
199
197
|
spin.start("Installing dependencies");
|
|
200
|
-
await deps.installRequired();
|
|
198
|
+
await deps.installRequired(pm);
|
|
201
199
|
spin.stop("Dependencies installed");
|
|
202
|
-
}
|
|
200
|
+
} else await deps.writeRequired();
|
|
203
201
|
}
|
|
204
202
|
await installer.onEnd();
|
|
205
203
|
outro(picocolors.bold(picocolors.greenBright("Successful")));
|