-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b252738
commit 7de243a
Showing
5 changed files
with
117 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env node | ||
|
||
import "global-jsdom/register"; | ||
import { compile, optimize, toSVG, showError } from "@penrose/core"; | ||
import { readFileSync } from "node:fs"; | ||
|
||
const pathResolver = (_path) => Promise.resolve(undefined); | ||
|
||
// TODO https://github.com/penrose/penrose/blob/main/packages/roger/index.ts | ||
// const resolvePath = (prefix, stylePaths) => { | ||
// const stylePrefixes = stylePaths.map((sty) => join(prefix, sty, "..")); | ||
// if (new Set(stylePrefixes).size > 1) { | ||
// console.warn( | ||
// chalk.yellow( | ||
// "Warning: the styles in this trio are not co-located. The first style will be used for image resolution." | ||
// ) | ||
// ); | ||
// } | ||
// const stylePrefix = stylePrefixes[0]; | ||
// return async (filePath) => { | ||
// // Handle absolute URLs | ||
// if (/^(http|https):\/\/[^ "]+$/.test(filePath)) { | ||
// const fileURL = new URL(filePath).href; | ||
// try { | ||
// const fileReq = await fetch(fileURL); | ||
// return fileReq.text(); | ||
// } catch (e) { | ||
// console.error(`Failed to resolve path: ${e}`); | ||
// return undefined; | ||
// } | ||
// } | ||
|
||
// // Relative paths | ||
// const joined = resolve(stylePrefix, filePath); | ||
// return fs.readFileSync(joined, "utf8"); | ||
// }; | ||
// }; | ||
|
||
const trio = JSON.parse(readFileSync(0, "utf-8")); | ||
|
||
const compiled = await compile(trio); | ||
if (compiled.isErr()) { | ||
console.error(showError(compiled.error)); | ||
process.exit(1); | ||
} | ||
const optimized = optimize(compiled.value); | ||
if (optimized.isErr()) { | ||
console.error(showError(optimized.error)); | ||
process.exit(1); | ||
} | ||
const svg = (await toSVG(optimized.value, pathResolver, "rehype")).outerHTML; | ||
// https://github.com/stereobooster/venn-nodejs/blob/main/bin/venn-nodejs.js | ||
// do we need different serializer | ||
// import serialize from "w3c-xmlserializer"; | ||
// const s = new XMLSerializer(); | ||
// s.serializeToString(svg); | ||
process.stdout.write(svg); | ||
process.exit(0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// import { type compile } from "@penrose/core"; | ||
import { spawn } from "node:child_process"; | ||
import { fileURLToPath } from "url"; | ||
import { dirname } from "path"; | ||
import { resolve } from "node:path"; | ||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
const executablePath = resolve(__dirname, `../bin/penrose.js`); | ||
|
||
// type CompileOptions = Parameters<typeof compile>[0]; | ||
type CompileOptions = { | ||
substance: string; | ||
style: string; | ||
domain: string; | ||
variation: string; | ||
excludeWarnings?: string[]; | ||
}; | ||
|
||
export const penrose = (opts: CompileOptions): Promise<string> => { | ||
return new Promise((resolve, reject) => { | ||
let res = ""; | ||
|
||
const bin = spawn(executablePath, [], { | ||
windowsHide: true, | ||
}); | ||
bin.stdout.on("data", (data: Buffer) => { | ||
res += data.toString(); | ||
}); | ||
bin.stderr.on("data", (data: Buffer) => { | ||
reject(`stderr: ${data.toString()}`); | ||
}); | ||
bin.on("close", (code) => { | ||
if (code === 0) { | ||
resolve(res); | ||
} else { | ||
reject(`child process exited with code ${code}`); | ||
} | ||
}); | ||
|
||
bin.stdin.write(JSON.stringify(opts)); | ||
bin.stdin.end(); | ||
}); | ||
}; |