jgraph / drawio

draw.io is a JavaScript, client-side editor for general diagramming.
https://www.drawio.com
Apache License 2.0
40.34k stars 7.54k forks source link

Allow CLI to read a diagram from stdin and write the result to stdout #1946

Closed ggrossetie closed 2 years ago

ggrossetie commented 3 years ago

Is your feature request related to a problem? Please describe.

The diagrams.net (Electron-based) CLI is very useful but I would like to read a diagram from stdin and write the output to stdout to avoid file manipulations.

It would be particularly useful when executing the CLI from a program. For instance, let's say that I want to write a small HTTP service on top of the CLI. I will receive the input diagram as a request body and I want to write the result in the response. In this case, I don't want to:

  1. write a temporary input file to the disk,
  2. execute the command,
  3. read the generated output file,
  4. delete both the input and output file

It would be more efficient to execute the command with the input file as stdin and to read the result from stdout. No file manipulation.

Describe the solution you'd like

When exporting a single input file to a single output file, we could allow diagrams.net CLI to read it from stdin and output the result to stdout. By convention, command-line interfaces usually use - (dash character), for instance:

cat input.xml | drawio --export --format svg -

The above command will read the diagram from stdin. It's also common to output to stdout (by default) when the input is read from stdin. In other words, the above command will output to stdout by default, but the following command will output to output.svg:

cat input.xml | drawio --export --format svg --output output.svg -

Similarly, if we want to explicitly output to stdout then we can use the value -:

drawio --export --format svg --output - input.xml

Please note that the value of the parameter output is -.

Describe alternatives you've considered

I guess writing my own CLI inspired by electron.js but that would be a maintenance burden.

For reference, I did try to create a simple CLI based on Puppeteer (instead of Electron) but I had to make some changes to export.js in order to be able to call the right functions from Puppeteer (most notably render and get-svg-data callback).

If this is something that you would like to support (i.e., providing both an Electron-based CLI and a Puppeteer-based CLI) then I would gladly help. I know that diagrams.net is closed to contributions but let me know if I can be of any help.

Additional context

I'm working on https://kroki.io and I would like to provide an API to convert diagrams.net XML files to PNG, JPEG or SVG images (and potentially PDF and VSDX). As far as I understand, diagrams.net is using the browser (client-side JavaScript) to convert diagrams to images. So, I'm looking for a self-contained and lightweight solution to provide this service server-side.

davidjgraph commented 2 years ago

So, you mean implement a node app instead of the desktop tool? Do you want https://github.com/jgraph/draw-image-export2 ?

ggrossetie commented 2 years ago

I was under the impression that jgraph/draw-image-export2 was the "legacy" implementation? The description says: "The 2018 server-side PNG/PDF export implementation using Node, Puppeteer and Chrome headless".

Also jgraph/draw-image-export2 relies on an external service (draw.io server), so it's not really a standalone application:

await page.goto((process.env.DRAWIO_SERVER_URL || 'https://viewer.diagrams.net') + '/export3.html', {waitUntil: 'networkidle0'});

https://github.com/jgraph/draw-image-export2/blob/754f2c784d13535e38b14a11434d573a856a0fba/export.js#L455

davidjgraph commented 2 years ago

No, that's the current server-side generation, it's just we switched to that new architecture in 2018.

It doesn't rely on anything external, that is the external server. export3.html just happens to be served externally, it could just as well be bundled on that node sever.

ggrossetie commented 2 years ago

No, that's the current server-side generation, it's just we switched to that new architecture in 2018.

πŸ‘ŒπŸ»

It doesn't rely on anything external, that is the external server. export3.html just happens to be served externally, it could just as well be bundled on that node sever.

So the static resources are available here https://github.com/jgraph/drawio/tree/dev/src/main/webapp right? Should I worry about breaking changes? Should I use a specific version? latest?

In theory, I only need to serve the following files:

Is that correct? I will give it a try and report back, thanks for your guidance πŸ‘πŸ»

davidjgraph commented 2 years ago

Yes, that look like the right list (you'll soon find out if one is missing :) ).