Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const isDeno = typeof globalThis !== "undefined" && "Deno" in globalThis;

async function exitWithCode(code: number): Promise<never> {
if (isDeno) {
Deno.exit(code);
const DenoNS = (globalThis as any).Deno;
DenoNS.exit(code);
} else {
const mod = await import("node:process");
mod.default.exit(code);
Expand All @@ -26,7 +27,10 @@ async function exitWithCode(code: number): Promise<never> {

async function readText(p: string): Promise<string> {
try {
if (isDeno) return await Deno.readTextFile(p);
if (isDeno) {
const DenoNS = (globalThis as any).Deno;
return await DenoNS.readTextFile(p);
}
const fs = await import("node:fs/promises");
return await fs.readFile(p, "utf8");
} catch (_e) {
Expand All @@ -38,7 +42,8 @@ async function readText(p: string): Promise<string> {

async function writeText(p: string, content: string) {
if (isDeno) {
await Deno.writeTextFile(p, content);
const DenoNS = (globalThis as any).Deno;
await DenoNS.writeTextFile(p, content);
} else {
const fs = await import("node:fs/promises");
await fs.writeFile(p, content, "utf8");
Expand All @@ -48,7 +53,8 @@ async function writeText(p: string, content: string) {
async function fileExists(p: string): Promise<boolean> {
try {
if (isDeno) {
await Deno.stat(p);
const DenoNS = (globalThis as any).Deno;
await DenoNS.stat(p);
return true;
} else {
const fs = await import("node:fs/promises");
Expand Down Expand Up @@ -90,7 +96,7 @@ async function initConfig(path: string) {

// Main function to avoid top-level await which is incompatible with CommonJS
async function main() {
const argv = isDeno ? Deno.args : (await import("node:process")).argv.slice(2);
const argv = isDeno ? (globalThis as any).Deno.args : (await import("node:process")).argv.slice(2);
const { cmd, configPath } = parseArgs(argv);

if (cmd === "init") {
Expand Down Expand Up @@ -121,7 +127,8 @@ async function main() {
main().catch((e) => {
console.error("Fatal error:", e);
if (isDeno) {
Deno.exit(1);
const DenoNS = (globalThis as any).Deno;
DenoNS.exit(1);
} else {
import("node:process").then(mod => mod.default.exit(1));
}
Expand Down
9 changes: 5 additions & 4 deletions runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ export async function loadAdapters(): Promise<Adapters> {

const mermaid: MermaidRenderer = { toPng(_mmd) { return Promise.resolve(null); } };

const DenoNS = (globalThis as any).Deno;
const fs: FSLike = {
readFile: p => Deno.readTextFile(p),
writeFile: (p, d) => Deno.writeTextFile(p, d),
writeBinaryFile: (p, d) => Deno.writeFile(p, d),
readFile: p => DenoNS.readTextFile(p),
writeFile: (p, d) => DenoNS.writeTextFile(p, d),
writeBinaryFile: (p, d) => DenoNS.writeFile(p, d),
mkdirp: p => ensureDir(p),
exists: p => Deno.stat(p).then(()=>true).catch(()=>false),
exists: p => DenoNS.stat(p).then(()=>true).catch(()=>false),
};

return {
Expand Down
7 changes: 4 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ async function extractMachinesFromFile(filePath: string, _adapters: Adapters): P
// Convert to absolute file:// URL
let importPath = filePath;
if (!importPath.startsWith('file://') && !importPath.startsWith('http://') && !importPath.startsWith('https://')) {
// Check if Deno is available
// Check if Deno is available using safer pattern to avoid WASM transformation issues
const isDeno = typeof globalThis !== "undefined" && "Deno" in globalThis;

// Make it absolute if it's not already
if (!importPath.startsWith('/')) {
if (isDeno) {
// Safe to access Deno directly after checking isDeno
importPath = `${Deno.cwd()}/${importPath}`;
// Access Deno dynamically to avoid WASM transformer issues with direct global access
const DenoNS = (globalThis as any).Deno;
importPath = `${DenoNS.cwd()}/${importPath}`;
} else {
const process = await import("node:process");
importPath = `${process.cwd()}/${importPath}`;
Expand Down