vseventer / sharp-cli

CLI for sharp.
MIT License
186 stars 20 forks source link

How can I target output to a dynamic subdirectory? #43

Open winston0410 opened 4 years ago

winston0410 commented 4 years ago

I have multiples subdirectories in my image directories, and I want to convert images inside these subdirectories and output the formatted files there.

I tried with the following cmd but it outputs all files in the main directory, images. Is something like this possible? Do I need to hard code subdirectories?


npx sharp --input './dist/images/**/*.jpg' --output './dist/images/' -f webp;
vseventer commented 4 years ago

You should be able to use macros for that.

winston0410 commented 4 years ago

Thank you so much for you reply. I tried both methods, hardcoding paths and using macros. Hardcoding paths only works when you don't care about the subdirectories. Macros( {dir} )did respect subdirectories, but I cannot change their value. Is it possible to modify the value in {dir}, or is there any workaround to achieve the following?

Take the example from README:

Input path:

┌─────────────────────┬────────────┐
│          dir        │    base    │
├──────┬              ├──────┬─────┤
│ root │              │ name │ ext │
"  /    home/user/dir / file  .txt "
└──────┴──────────────┴──────┴─────┘

Expected output path:

┌─────────────────────┬────────────┐
│          dir        │    base    │
├──────┬              ├──────┬─────┤
│ root │              │ name │ ext │
"  /    home/user/formatted-dir / file  .txt "
└──────┴──────────────┴──────┴─────┘
vseventer commented 3 years ago

@winston0410 I see what you want to achieve, and I don't think it's easily doable today. If you can point me to a library or pattern that could be used to implement something like this, let me know and I'll be happy to take a look.

beevk commented 3 years ago

@vseventer Maybe this will help: JPEG-Compressor-CLI jpgc -r -o ./out/ This option (-o) generates all the files in ./out directory while also maintaining the original directory structure.