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 ba60952
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 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)
})
})
15 changes: 10 additions & 5 deletions packages/pintora-cli/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -15,7 +15,7 @@ export interface PintoraConfig {
themeConfig: {
theme: string
darkTheme?: string
themeVariables: ITheme
themeVariables?: ITheme
}
}

Expand All @@ -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

0 comments on commit ba60952

Please sign in to comment.