Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Fix managerHead preset in main.ts #22701

Merged
merged 4 commits into from
May 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions code/lib/builder-manager/src/utils/data.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { basename, join } from 'path';
import { basename } from 'path';
import type { DocsOptions, Options } from '@storybook/types';
import { getRefs } from '@storybook/core-common';

import { readTemplate } from './template';
// eslint-disable-next-line import/no-cycle
import { executor, getConfig } from '../index';
import { safeResolve } from './safeResolve';

export const getData = async (options: Options) => {
const refs = getRefs(options);
Expand All @@ -16,7 +15,7 @@ export const getData = async (options: Options) => {
const title = options.presets.apply<string>('title');
const docsOptions = options.presets.apply<DocsOptions>('docs', {});
const template = readTemplate('template.ejs');
const customHead = safeResolve(join(options.configDir, 'manager-head.html'));
const customHead = options.presets.apply<string>('managerHead');

// we await these, because crucially if these fail, we want to bail out asap
const [instance, config] = await Promise.all([
Expand Down
29 changes: 2 additions & 27 deletions code/lib/builder-manager/src/utils/template.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import path, { dirname, join } from 'path';
import { dirname, join } from 'path';
import fs from 'fs-extra';

import { render } from 'ejs';

import type { DocsOptions, Options, Ref } from '@storybook/types';

const interpolate = (string: string, data: Record<string, string> = {}) =>
Object.entries(data).reduce((acc, [k, v]) => acc.replace(new RegExp(`%${k}%`, 'g'), v), string);

export const getTemplatePath = async (template: string) => {
return join(
dirname(require.resolve('@storybook/builder-manager/package.json')),
Expand All @@ -17,32 +14,11 @@ export const getTemplatePath = async (template: string) => {
};

export const readTemplate = async (template: string) => {
// eslint-disable-next-line @typescript-eslint/no-shadow
const path = await getTemplatePath(template);

return fs.readFile(path, 'utf8');
};

export async function getManagerHeadTemplate(
configDirPath: string,
interpolations: Record<string, string>
) {
const head = await fs
.pathExists(path.resolve(configDirPath, 'manager-head.html'))
.then<Promise<string> | false>((exists) => {
if (exists) {
return fs.readFile(path.resolve(configDirPath, 'manager-head.html'), 'utf8');
}
return false;
});

if (!head) {
return '';
}

return interpolate(head, interpolations);
}

export async function getManagerMainTemplate() {
return getTemplatePath(`manager.ejs`);
}
Expand All @@ -60,7 +36,6 @@ export const renderHTML = async (
docsOptions: Promise<DocsOptions>,
{ versionCheck, releaseNotesData, previewUrl, configType }: Options
) => {
const customHeadRef = await customHead;
const titleRef = await title;
const templateRef = await template;

Expand All @@ -79,6 +54,6 @@ export const renderHTML = async (
RELEASE_NOTES_DATA: JSON.stringify(JSON.stringify(releaseNotesData), null, 2),
PREVIEW_URL: JSON.stringify(previewUrl, null, 2), // global preview URL
},
head: customHeadRef ? await fs.readFile(customHeadRef, 'utf8') : '',
head: (await customHead) || '',
});
};
15 changes: 15 additions & 0 deletions code/lib/core-server/src/presets/common-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import { dedent } from 'ts-dedent';
import { parseStaticDir } from '../utils/server-statics';
import { defaultStaticDirs } from '../utils/constants';

const interpolate = (string: string, data: Record<string, string> = {}) =>
Object.entries(data).reduce((acc, [k, v]) => acc.replace(new RegExp(`%${k}%`, 'g'), v), string);

const defaultFavicon = require.resolve('@storybook/core-server/public/favicon.svg');

export const staticDirs: PresetPropertyFn<'staticDirs'> = async (values = []) => [
Expand Down Expand Up @@ -217,3 +220,15 @@ export const docs = (
...docsOptions,
docsMode,
});

export const managerHead = async (_: any, options: Options) => {
const location = join(options.configDir, 'manager-head.html');
if (await pathExists(location)) {
const contents = readFile(location, 'utf-8');
const interpolations = options.presets.apply<Record<string, string>>('env');

return interpolate(await contents, await interpolations);
}

return '';
};
9 changes: 8 additions & 1 deletion code/lib/types/src/modules/core-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,20 @@ export interface StorybookConfig {
previewBody?: PresetValue<string>;

/**
* Programatically override the preview's main page template.
* Programmatically override the preview's main page template.
* This should return a reference to a file containing an `.ejs` template
* that will be interpolated with environment variables.
*
* @example '.storybook/index.ejs'
*/
previewMainTemplate?: string;

/**
* Programmatically modify the preview head/body HTML.
* The managerHead function accept a string,
* which is the existing head content, and return a modified string.
*/
managerHead?: PresetValue<string>;
}

export type PresetValue<T> = T | ((config: T, options: Options) => T | Promise<T>);
Expand Down