clone

a browser-first diagram editor with live svg previews, local drafts and encrypted, accountless sharing

main
diagramzip / renderers / edge / scripts / build / dbml.mjs
dbml.mjs 2.5K
1import { readdir, readFile } from 'node:fs/promises'2import { build } from 'esbuild'34const rendererPath = new URL('../../node_modules/@softwaretechnik/dbml-renderer/lib/renderer.js', import.meta.url)5const upstreamRenderer = await readFile(rendererPath, 'utf8')6const rendererPattern = /const render = \(input, format\) => \{[\s\S]*?\n\};/7if (!rendererPattern.test(upstreamRenderer)) {8 throw new Error('DBML renderer layout changed; review the Viz-free repack before rebuilding.')9}10const dotOnlyRenderer = upstreamRenderer.replace(rendererPattern, `const render = (input, format) => {11 if (format !== "dot") throw new Error("The edge DBML bundle only supports DOT lowering.");12 return dot(input);13};`)14const parserPath = new URL('../../node_modules/@softwaretechnik/dbml-renderer/lib/parser.js', import.meta.url)15const upstreamParser = await readFile(parserPath, 'utf8')16if (!upstreamParser.includes('console.debug(peggyErr);')) {17 throw new Error('DBML parser layout changed; review the debug suppression before rebuilding.')18}19const quietParser = upstreamParser.replace(' console.debug(peggyErr);\n', '')2021await build({22 entryPoints: [new URL('../../toolchains/dbml/entry.ts', import.meta.url).pathname],23 bundle: true,24 format: 'esm',25 platform: 'node',26 minify: true,27 outfile: new URL('../../artifacts/dbml/dbml-backend.js', import.meta.url).pathname,28 plugins: [{29 name: 'dbml-dot-only',30 setup(plugin) {31 plugin.onLoad({ filter: /[\\/]@softwaretechnik[\\/]dbml-renderer[\\/]lib[\\/]renderer\.js$/ }, () => ({32 contents: dotOnlyRenderer,33 loader: 'js',34 }))35 plugin.onLoad({ filter: /[\\/]@softwaretechnik[\\/]dbml-renderer[\\/]lib[\\/]parser\.js$/ }, () => ({36 contents: quietParser,37 loader: 'js',38 }))39 },40 }],41})4243const { dbmlToDot } = await import(new URL(`../../artifacts/dbml/dbml-backend.js?build=${Date.now()}`, import.meta.url))44const examplesPath = new URL('../../node_modules/@softwaretechnik/dbml-renderer/examples/', import.meta.url)45const examples = (await readdir(examplesPath)).filter(filename => filename.endsWith('.dbml')).sort()46if (examples.length < 20) throw new Error('DBML upstream example corpus is incomplete.')47for (const filename of examples) {48 const source = await readFile(new URL(filename, examplesPath), 'utf8')49 const expectedDot = await readFile(new URL(`${filename}.dot`, examplesPath), 'utf8')50 if (dbmlToDot(source) !== expectedDot) {51 throw new Error(`DBML edge bundle differs from upstream expected DOT for ${filename}.`)52 }53}