Skip to content

Commit

Permalink
refactor: optimize data fetching in BlockDisplay & ChartDisplay
Browse files Browse the repository at this point in the history
  • Loading branch information
hngngn committed Jan 3, 2025
1 parent e3283b8 commit 6325c03
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 590 deletions.
71 changes: 8 additions & 63 deletions docs/scripts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { existsSync, mkdirSync } from "node:fs";
import fs from "node:fs";
import fs, { existsSync, mkdirSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { registry } from "@/registry";
Expand All @@ -20,54 +19,6 @@ function createTempSourceFile(filename: string) {
return path.join(dir, filename);
}

const fixImport = (content: string) => {
const regex = /@\/(.+?)\/((?:.*?\/)?(?:components|ui|hooks|lib))\/([\w-]+)/g;

const replacement = (
match: string,
path: string,
type: string,
component: string,
) => {
if (type.endsWith("components")) {
return `@/components/${component}`;
}
if (type.endsWith("ui")) {
return `@/components/ui/${component}`;
}
if (type.endsWith("hooks")) {
return `@/hooks/${component}`;
}
if (type.endsWith("lib")) {
return `@/lib/${component}`;
}

return match;
};

return content.replace(regex, replacement);
};

const getFileContent = (filePath: string) => {
const raw = fs.readFileSync(filePath, "utf-8");

const project = new Project({
compilerOptions: {},
});

const tempFile = createTempSourceFile(filePath);
const sourceFile = project.createSourceFile(tempFile, raw, {
scriptKind: ScriptKind.TSX,
});

let code = sourceFile.getFullText();

// Fix imports.
code = fixImport(code);

return JSON.stringify(code, null, 4);
};

// ----------------------------------------------------------------------------
// Build __registry__/index.tsx.
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -196,21 +147,18 @@ export const Index: Record<string, any> = {
typeof file === "string" ? file : file.path
}`;
const resolvedFilePath = path.resolve(filePath);
const content = getFileContent(filePath);
return typeof file === "string"
? `"${resolvedFilePath}"`
: `{
path: "${filePath}",
type: "${file.type}",
target: "${file.target ?? ""}",
content: ${item.type === "registry:block" ? content : '""'}
}`;
})}],
component: clientOnly(() => import("${componentPath}"), { lazy: true }),
source: "${sourceFilename}",
category: "${item.category ?? ""}",
subcategory: "${item.subcategory ?? ""}"
category: "${item.category ?? ""}"
},`;
}
}
Expand All @@ -232,15 +180,12 @@ export const Index: Record<string, any> = {
return {
...item,
files: item.files?.map((_file) => {
const file =
typeof _file === "string"
? {
path: _file,
type: item.type,
}
: _file;

return file;
return typeof _file === "string"
? {
path: _file,
type: item.type,
}
: _file;
}),
};
});
Expand Down
Loading

0 comments on commit 6325c03

Please sign in to comment.