Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions code/core/src/core-server/utils/manifests/manifests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,30 @@ describe('manifests', () => {
expect(files['/output/manifests/components.html']).toContain('<!doctype html>');
});

it('should write HTML file when docs manifest exists', async () => {
mockManifests = {
docs: {
v: 0,
docs: {
'intro--docs': {
id: 'intro--docs',
name: 'docs',
path: './Intro.mdx',
title: 'Intro',
content: '# Introduction',
},
},
},
};

await writeManifests('/output', mockPresets);

const files = vol.toJSON();
expect(files['/output/manifests/components.html']).toBeDefined();
expect(files['/output/manifests/components.html']).toContain('<!doctype html>');
expect(files['/output/manifests/components.html']).toContain('Unattached Docs');
});

it('should handle errors when presets.apply fails', async () => {
const error = new Error('Preset application failed');
vi.mocked(mockPresets.apply).mockRejectedValue(error);
Expand Down Expand Up @@ -360,11 +384,11 @@ describe('manifests', () => {
expect(res.end).toHaveBeenCalled();
const html = (res.end as any).mock.calls[0][0];
expect(html).toContain('<!doctype html>');
expect(html).toContain('Components Manifest');
expect(html).toContain('Manifest Debugger');
expect(res.statusCode).toBeUndefined();
});

it('should return 404 message when components manifest does not exist', async () => {
it('should return 404 message when no components or docs manifest exist', async () => {
mockManifests = {
other: { data: 'value' },
};
Expand All @@ -383,7 +407,9 @@ describe('manifests', () => {

expect(res.statusCode).toBe(404);
expect(res.setHeader).toHaveBeenCalledWith('Content-Type', 'text/html; charset=utf-8');
expect(res.end).toHaveBeenCalledWith('<pre>No components manifest configured.</pre>');
expect(res.end).toHaveBeenCalledWith(
'<pre>No components or docs manifest configured.</pre>'
);
});

it('should return 404 when manifests is empty', async () => {
Expand All @@ -402,7 +428,9 @@ describe('manifests', () => {
await handler(req, res);

expect(res.statusCode).toBe(404);
expect(res.end).toHaveBeenCalledWith('<pre>No components manifest configured.</pre>');
expect(res.end).toHaveBeenCalledWith(
'<pre>No components or docs manifest configured.</pre>'
);
});

it('should handle errors with 500 status and return error HTML', async () => {
Expand Down
18 changes: 11 additions & 7 deletions code/core/src/core-server/utils/manifests/manifests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Polka } from 'polka';
import invariant from 'tiny-invariant';

import { Tag } from '../../../shared/constants/tags';
import { renderComponentsManifest } from './render-components-manifest';
import { type DocsManifest, renderComponentsManifest } from './render-components-manifest';

async function getManifests(presets: Presets) {
const generator = await presets.apply('storyIndexGenerator');
Expand Down Expand Up @@ -37,10 +37,13 @@ export async function writeManifests(outputDir: string, presets: Presets) {
writeFile(join(outputDir, 'manifests', `${name}.json`), JSON.stringify(content))
)
);
if ('components' in manifests) {
if ('components' in manifests || 'docs' in manifests) {
await writeFile(
join(outputDir, 'manifests', 'components.html'),
renderComponentsManifest(manifests.components as ComponentsManifest)
renderComponentsManifest(
manifests.components as ComponentsManifest | undefined,
manifests.docs as DocsManifest | undefined
)
);
}
} catch (e) {
Expand Down Expand Up @@ -72,17 +75,18 @@ export function registerManifests({ app, presets }: { app: Polka; presets: Prese
app.get('/manifests/components.html', async (req, res) => {
try {
const manifests = await getManifests(presets);
const manifest = manifests.components;
const componentsManifest = manifests.components;
const docsManifest = manifests.docs as DocsManifest | undefined;

if (!manifest) {
if (!componentsManifest && !docsManifest) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.end(`<pre>No components manifest configured.</pre>`);
res.end(`<pre>No components or docs manifest configured.</pre>`);
return;
}

res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.end(renderComponentsManifest(manifest));
res.end(renderComponentsManifest(componentsManifest, docsManifest));
} catch (e) {
res.statusCode = 500;
res.setHeader('Content-Type', 'text/html; charset=utf-8');
Expand Down
Loading