From 718e47dba0be5c349918326fee8d8516bd181efb Mon Sep 17 00:00:00 2001 From: hikerpig Date: Sat, 23 Dec 2023 21:08:54 +0800 Subject: [PATCH] fix(cli): eliminate config side effects of `render` function --- .changeset/sharp-lizards-learn.md | 6 ++++++ .../pintora-cli/src/__tests__/render.spec.ts | 21 ++++++++++++++++++- packages/pintora-cli/src/render.ts | 19 ++++++++++------- packages/pintora-core/src/config.ts | 5 ++++- 4 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 .changeset/sharp-lizards-learn.md 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..891a69b0 100644 --- a/packages/pintora-cli/src/render.ts +++ b/packages/pintora-cli/src/render.ts @@ -1,5 +1,5 @@ import { RenderOptions, IRenderer } from '@pintora/renderer' -import { pintoraStandalone, PintoraConfig } from '@pintora/standalone' +import { pintoraStandalone, PintoraConfig, DeepPartial } from '@pintora/standalone' import { JSDOM } from 'jsdom' import { implForWrapper } from 'jsdom/lib/jsdom/living/generated/utils' import { Canvas, CanvasPattern } from 'canvas' @@ -16,7 +16,7 @@ export type CLIRenderOptions = { * Assign extra background color */ backgroundColor?: string - pintoraConfig?: Partial + pintoraConfig?: DeepPartial /** * width of the output, height will be calculated according to the diagram content ratio */ @@ -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..11e7e01b 100644 --- a/packages/pintora-core/src/config.ts +++ b/packages/pintora-core/src/config.ts @@ -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 @@ -65,7 +68,7 @@ const configApi = { newConfig.themeConfig = newConfig.themeConfig || ({} as any) newConfig.themeConfig.themeVariables = { ...themeVars } } - if (configThemeVars) { + if (configThemeVars && newConfig.themeConfig.themeVariables) { Object.assign(newConfig.themeConfig.themeVariables, configThemeVars) } }