import path from 'path'; import type { NormalizedConfig } from '@modern-js/core'; import { fs, Import, normalizeOutputPath } from '@modern-js/utils'; import { STORYBOOK_TEMPLATE_DIR } from '../constants'; const template: typeof import('lodash.template') = Import.lazy( 'lodash.template', require, ); const INTERPOLATE_REGPEXP = /<%=([\s\S]+?)%>/g; export type MainOptions = { appDirectory: string; disableTsChecker: boolean; stories: string[]; isTsProject: boolean; }; const MAIN_TEMPLATE = path.join(STORYBOOK_TEMPLATE_DIR, 'main.tmpl'); export const generateMain = (options: MainOptions) => { const mainTemplate = fs.readFileSync(MAIN_TEMPLATE, 'utf-8'); const injects: Record = { appDirectory: normalizeOutputPath(options.appDirectory), sbConfigDir: normalizeOutputPath( path.resolve(options.appDirectory, 'config/storybook'), ), userMainPath: normalizeOutputPath( path.resolve(options.appDirectory, 'config/storybook/main.js'), ), disableTsChecker: String(options.disableTsChecker), stories: JSON.stringify(options.stories), isTsProject: String(options.isTsProject), }; const execute = template(mainTemplate, { interpolate: INTERPOLATE_REGPEXP }); return execute(injects); }; export type PreviewOptions = { userPreviewPath?: string; runtime: NormalizedConfig['runtime']; designToken: Record; }; const PREVIEW_TEMPLATE = path.join(STORYBOOK_TEMPLATE_DIR, 'preview.tmpl'); const USER_PREVIEW_TEMPLATE = path.join( STORYBOOK_TEMPLATE_DIR, 'user-preview.tmpl', ); export const generatePreview = (options: PreviewOptions) => { const previewTemplate = fs.readFileSync( options.userPreviewPath ? USER_PREVIEW_TEMPLATE : PREVIEW_TEMPLATE, 'utf-8', ); const injects: Record = { userPreviewPath: options.userPreviewPath || '', runtime: JSON.stringify(options.runtime || {}), designToken: JSON.stringify(options.designToken), }; const execute = template(previewTemplate, { interpolate: INTERPOLATE_REGPEXP, }); return execute(injects); };