d2 icon indicating copy to clipboard operation
d2 copied to clipboard

cli: Add --out-dir flag

Open alixander opened this issue 3 years ago • 6 comments

d2 ./static/d2/*.d2 ./static/generated/*.svg

alixander avatar Oct 23 '22 18:10 alixander

You'd have to do:

d2 `./static/d2/*.d2` `./static/generated/*.svg`

To avoid the shell interpreting the globs first.

I think instead of dealing with the shadowing globs, this should be achieved as so:

d2 --out-dir ./static/generated ./static/d2/*.d2

Now the shell can interpet the glob and it will work exactly as expected. out-dir also has precedence in tsconfig.json.

You could also now use xargs to run things in parallel:

# Start a process for every 256 .d2 files.
# Run up to 8 processes at a time.
find . -name './static/d2/*.d2' | xargs d2 -P8 -n256 --out-dir ./static/generated

The xargs example is a little contrived as d2 would always compile in parallel anyway but it demonstrates that --out-dir is more composable with other shellisms.

nhooyr avatar Oct 24 '22 21:10 nhooyr

does it even need --out-dir then?

the CLI can check that the second arg (output) is an existing directory, and treat it as out-dir if so.

alixander avatar Oct 24 '22 21:10 alixander

@alixander that wouldn't work with globs. globs are by convention always at the end of a command invocation. that's how xargs works by default, it just appends the list of files to the end of the command as individual args. otherwise you need to use -I{} which is annoying and means that you have to spin up a process per file which is noticably inefficient. i think gnu xargs has a way around that where you can put the list of args anywhere in the command but it's not portable and we should strive for portability when possible.

also the directory may not exist, we ought to create it then instead of interpreting it as a file to be compiled. the dual behaviour would be generally confusing anyway.

nhooyr avatar Oct 24 '22 22:10 nhooyr

okay, this task is to add --out-dir then

alixander avatar Oct 24 '22 22:10 alixander

will also need --out-format (https://github.com/terrastruct/d2/issues/1288)

alixander avatar May 03 '23 21:05 alixander

up

sdykae avatar Jun 06 '24 16:06 sdykae