Skip to content

Commit 96b6abf

Browse files
committed
refactor(scripts): extract mirrorRegistryTargets from prepareProjectDir
Inlining the registry-target mirroring pushed prepareProjectDir to cyclomatic 24, the one complexity finding fallow attributes to this branch — the other eight are inherited and out of scope here. Same behaviour, expressed as a filter chain in its own named function, so prepareProjectDir returns to what it was and the new helper stays flat.
1 parent 11960f3 commit 96b6abf

1 file changed

Lines changed: 26 additions & 18 deletions

File tree

scripts/generate-catalog-previews.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,28 +126,36 @@ function outputDir(kind: ItemKind): string {
126126
return resolve(repoRoot, "docs/images/catalog", typeDir);
127127
}
128128

129+
/**
130+
* Preview the item in the same layout users get after installation: some
131+
* components reference assets by their registry target path rather than by the
132+
* flat source path stored beside the manifest.
133+
*/
134+
function mirrorRegistryTargets(projectDir: string): void {
135+
const manifestPath = join(projectDir, "registry-item.json");
136+
if (!existsSync(manifestPath)) return;
137+
138+
const manifest = JSON.parse(readFileSync(manifestPath, "utf-8")) as {
139+
files?: { path?: string; target?: string }[];
140+
};
141+
142+
const copies = (manifest.files ?? [])
143+
.map((file) => [file.path, file.target] as const)
144+
.filter((pair): pair is readonly [string, string] => Boolean(pair[0] && pair[1]))
145+
.map(([path, target]) => [join(projectDir, path), join(projectDir, target)] as const)
146+
.filter(([from, to]) => from !== to && existsSync(from));
147+
148+
for (const [from, to] of copies) {
149+
mkdirSync(dirname(to), { recursive: true });
150+
cpSync(from, to);
151+
}
152+
}
153+
129154
async function prepareProjectDir(item: CatalogItem): Promise<string> {
130155
const tmpDir = join(tmpdir(), `hf-catalog-${item.name}-${Date.now()}`);
131156
mkdirSync(tmpDir, { recursive: true });
132157
cpSync(item.sourceDir, tmpDir, { recursive: true });
133-
134-
// Preview the item in the same layout users get after installation. Some
135-
// components reference assets by their registry target path rather than by
136-
// the flat source path stored beside the manifest.
137-
const registryItemPath = join(tmpDir, "registry-item.json");
138-
if (existsSync(registryItemPath)) {
139-
const manifest = JSON.parse(readFileSync(registryItemPath, "utf-8")) as {
140-
files?: { path?: string; target?: string }[];
141-
};
142-
for (const file of manifest.files ?? []) {
143-
if (!file.path || !file.target) continue;
144-
const sourcePath = join(tmpDir, file.path);
145-
const targetPath = join(tmpDir, file.target);
146-
if (!existsSync(sourcePath) || sourcePath === targetPath) continue;
147-
mkdirSync(dirname(targetPath), { recursive: true });
148-
cpSync(sourcePath, targetPath);
149-
}
150-
}
158+
mirrorRegistryTargets(tmpDir);
151159

152160
// The HyperFrames producer navigates to index.html at the project root.
153161
// Blocks and component demos are standalone HTML files, not index.html.

0 commit comments

Comments
 (0)