diff --git a/.changeset/sharp-lizards-learn.md b/.changeset/sharp-lizards-learn.md new file mode 100644 index 00000000..586787dc --- /dev/null +++ b/.changeset/sharp-lizards-learn.md @@ -0,0 +1,6 @@ +--- +'@pintora/cli': patch +'@pintora/core': patch +--- + +eliminate config side effects of `render` function diff --git a/packages/pintora-cli/src/__tests__/render.spec.ts b/packages/pintora-cli/src/__tests__/render.spec.ts index 3be4da5c..789b042e 100644 --- a/packages/pintora-cli/src/__tests__/render.spec.ts +++ b/packages/pintora-cli/src/__tests__/render.spec.ts @@ -1,6 +1,7 @@ +import { pintoraStandalone } from '@pintora/standalone' +import { EXAMPLES } from '@pintora/test-shared' import { SVG_MIME_TYPE } from '../const' import { render } from '../render' -import { EXAMPLES } from '@pintora/test-shared' describe('render', () => { it('can output svg', async () => { @@ -19,4 +20,22 @@ describe('render', () => { expect(buf.constructor).toBe(Buffer) expect(buf.length).toBeGreaterThan(0) }) + + it('pintoraConfig should not alter global config', async () => { + const oldTheme = pintoraStandalone.configApi.getConfig().themeConfig.theme + const oldUseMaxWidth = pintoraStandalone.configApi.getConfig().core?.useMaxWidth + await render({ + code: EXAMPLES.er.code, + width: 1000, + pintoraConfig: { + themeConfig: { + theme: 'light', + }, + }, + }) + + const config = pintoraStandalone.configApi.getConfig() + expect(config.themeConfig.theme).toBe(oldTheme) + expect(config.core.useMaxWidth).toBe(oldUseMaxWidth) + }) }) diff --git a/packages/pintora-cli/src/render.ts b/packages/pintora-cli/src/render.ts index 6c75d85c..ce05a43d 100644 --- a/packages/pintora-cli/src/render.ts +++ b/packages/pintora-cli/src/render.ts @@ -41,15 +41,14 @@ function renderPrepare(opts: CLIRenderOptions) { return { container, pintorRender(renderOpts: Pick) { + let config = pintoraStandalone.getConfig() if (pintoraConfig) { - pintoraStandalone.setConfig(pintoraConfig) + config = pintoraStandalone.configApi.gnernateNewConfig(pintoraConfig) } const containerSize = opts.width ? { width: opts.width } : undefined if (opts.width) { - pintoraStandalone.setConfig({ - core: { useMaxWidth: true }, - }) + config = pintoraStandalone.configApi.gnernateNewConfig({ core: { useMaxWidth: true } }) } return new Promise((resolve, reject) => { @@ -59,7 +58,8 @@ function renderPrepare(opts: CLIRenderOptions) { containerSize, enhanceGraphicIR(ir) { if (!ir.bgColor) { - const themeVariables = pintoraStandalone.getConfig().themeConfig.themeVariables + const themeVariables: Partial = + config.themeConfig.themeVariables || {} const newBgColor = backgroundColor || themeVariables.canvasBackground || @@ -81,6 +81,11 @@ function renderPrepare(opts: CLIRenderOptions) { } } +/** + * Renders the Pintora CLI options to the specified output format. + * @param opts - The CLIRenderOptions. + * @returns A promise that resolves to the rendered output. + */ export function render(opts: CLIRenderOptions) { const mimeType = opts.mimeType || 'image/png' diff --git a/packages/pintora-core/src/config.ts b/packages/pintora-core/src/config.ts index 50487e55..61b75b90 100644 --- a/packages/pintora-core/src/config.ts +++ b/packages/pintora-core/src/config.ts @@ -15,7 +15,7 @@ export interface PintoraConfig { themeConfig: { theme: string darkTheme?: string - themeVariables: ITheme + themeVariables?: ITheme } } @@ -38,6 +38,9 @@ const configApi = { getConfig() { return config as any as T }, + /** + * Sets the configuration for Pintora. + */ setConfig(c: DeepPartial) { const newConfig = configApi.gnernateNewConfig(c) config = newConfig