import { ReactElement } from 'react' ; import { renderToDot } from '@ts-graphviz/react' ; import { ChildProcessOptions , exportToFile , Format } from '@ts-graphviz/node' ; import { CONFIG } from '@rediagram/common' ; import { ensureDir } from 'fs-extra' ; import path from 'path' ; import caller from 'caller' ; export type RenderOption = { /**
     * Output destination directory.
     */ dir ? : string ; /**
     * Output file name.
     */ name ? : string ; } ; type InternalRenderOption = RenderOption & { deprecated _caller ? : string ; } ; async function render ( element , { name , dir , format , dotOptions , } ) { const dot = renderToDot ( element ) ; const output = path . format ( { dir , name , ext : `.${ format }` } ) ; if ( dir !== undefined ) { await ensureDir ( dir ) ; } await exportToFile ( dot , { format , output , childProcessOptions : dotOptions , } ) ; } /**
 * Output PNG image.
 */ export function PNG ( element , options ? ) ; export async function PNG ( element , { name , dir , _caller = caller ( ) } = { } ) { const p = path . parse ( _caller ) ; await render ( element , { format : 'png' , name : name ?? p . name , dir : dir ?? CONFIG . output . getDir ( ) ?? p . dir , dotOptions : { timeout : CONFIG . dot ?. getTimeout ( ) , } , } ) ; } /**
 * Output SVG file.
 */ export function SVG ( element , options ? ) ; export async function SVG ( element , { name , dir , _caller = caller ( ) } = { } ) { const p = path . parse ( _caller ) ; await render ( element , { format : 'svg' , name : name ?? p . name , dir : dir ?? CONFIG . output . getDir ( ) ?? p . dir , dotOptions : { timeout : CONFIG . dot ?. getTimeout ( ) , } , } ) ; } /**
 * Output PDF file.
 */ export function PDF ( element , options ? ) ; export async function PDF ( element , { name , dir , _caller = caller ( ) } = { } ) { const p = path . parse ( _caller ) ; await render ( element , { format : 'pdf' , name : name ?? p . name , dir : dir ?? CONFIG . output . getDir ( ) ?? p . dir , dotOptions : { timeout : CONFIG . dot ?. getTimeout ( ) , } , } ) ; }