Skip to content

Commit

Permalink
fix(cli): eliminate config side effects of render function
Browse files Browse the repository at this point in the history
  • Loading branch information
hikerpig committed Dec 23, 2023
1 parent befb11e commit 75d84af
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/sharp-lizards-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@pintora/cli': patch
'@pintora/core': patch
---

eliminate config side effects of `render` function
21 changes: 20 additions & 1 deletion packages/pintora-cli/src/__tests__/render.spec.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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)
})
})
19 changes: 12 additions & 7 deletions packages/pintora-cli/src/render.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -16,7 +16,7 @@ export type CLIRenderOptions = {
* Assign extra background color
*/
backgroundColor?: string
pintoraConfig?: Partial<PintoraConfig>
pintoraConfig?: DeepPartial<PintoraConfig>
/**
* width of the output, height will be calculated according to the diagram content ratio
*/
Expand All @@ -41,15 +41,14 @@ function renderPrepare(opts: CLIRenderOptions) {
return {
container,
pintorRender(renderOpts: Pick<RenderOptions, 'renderer'>) {
let config = pintoraStandalone.getConfig<PintoraConfig>()
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<IRenderer>((resolve, reject) => {
Expand All @@ -59,7 +58,8 @@ function renderPrepare(opts: CLIRenderOptions) {
containerSize,
enhanceGraphicIR(ir) {
if (!ir.bgColor) {
const themeVariables = pintoraStandalone.getConfig<PintoraConfig>().themeConfig.themeVariables
const themeVariables: Partial<PintoraConfig['themeConfig']['themeVariables']> =
config.themeConfig.themeVariables || {}
const newBgColor =
backgroundColor ||
themeVariables.canvasBackground ||
Expand All @@ -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'

Expand Down
5 changes: 4 additions & 1 deletion packages/pintora-core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const configApi = {
getConfig<T = PintoraConfig>() {
return config as any as T
},
/**
* Sets the configuration for Pintora.
*/
setConfig<T extends PintoraConfig = PintoraConfig>(c: DeepPartial<T>) {
const newConfig = configApi.gnernateNewConfig(c)
config = newConfig
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 75d84af

Please sign in to comment.