demike / TsUML2

Generates UML diagrams from TypeScript source code
MIT License
266 stars 34 forks source link

Fix Async Issues – (Use writeFileSync instead of writeFile?) #83

Closed ckrauterlovescoffee closed 3 months ago

ckrauterlovescoffee commented 5 months ago

I am trying to import this package, create a diagram and then mess with that diagram subsequently. When doing so, I get the following error,

node:fs:455
    return binding.readFileUtf8(path, stringToFlags(options.flag));
                   ^

Error: ENOENT: no such file or directory, open './docs/mermaid.dsl'
    at readFileSync (node:fs:455:20)
    at Redacted (file:///Users/redacted-user/github/diagrammer/src/diagrammer.ts:52:29)
    at Redacted (file:///Users/redacted-user/github/diagrammer/src/diagrammer.ts:202:24)
    at Redacted (file:///Users/redacted-user/github/diagrammer/src/diagrammer.ts:42:30)
    at file:///Users/redacted-user/github/diagrammer/src/app.ts:2:18
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:120:12) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: './docs/mermaid.dsl'
}

The reason I am seeing this error is because here and here, writeFile is being used which is inherently async, but it is not being awaited in this package (in the "here" links). So, my file, ./docs/mermaid.dsl hasn't been created by the time that I am trying to read it. My jenky work around is to sleep for a few hundred milliseconds before any subsequent read operations.

Two obvious paths to fix this would be to convert the write methods to async methods and await the writeFile calls... (createDiagram() would also need to be async) Or switch to writeFileSync so the operation is synchronous.

Would you be willing to make this change?

demike commented 4 months ago

@ckrauterlovescoffee you can use parseProject getMermaidDSL / getNomnomlDSL for now and write it yourself. I will use the async version of writeFile in the next version

ckrauterlovescoffee commented 3 months ago

Thanks! Looks like this can be closed now as you've added async.