vseventer / sharp-cli

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

Request to not normalize the output path before applying the macros #40

Closed KasparEtter closed 4 years ago

KasparEtter commented 4 years ago

I wanted to use sharp-cli to scale images at <folder>/original/<picture>.png to folder/scaled/<picture>.png. When I used sharp resize 2000 --input "*/original/*.png" --output "{dir}/../scaled/{base}", I got the error ENOENT: no such file or directory, open 'scaled/<picture>.png' because the folder scaled doesn't exist on the project level but only in the subfolders (i.e. <folder>/scaled). (Please take all values in this example figuratively. The error message used an absolute path but I cut the prefix as it's not relevant here, etc.)

After debugging sharp-cli locally, I realized that the problem is not where the output template is processed but rather where the arguments are parsed. So apparently yargs is to blame for this. When looking at

https://github.com/vseventer/sharp-cli/blob/cf12e80be7d56e229b344643b4213f1bfc154e3f/lib/cli.js#L87-L97

and yarg's options documentation, it became clear that sharp-cli asks yarg to normalize the template. After removing normalize: true,, I had the behavior I wanted.

Is there any reason to ask yarg for this normalization (rather than just resolving the path after processing the template)? If not, can you remove this line upstream so that I don't have to run a custom version of sharp-cli on my computer? (If you want, I can make a PR for this.) It cannot be there for security reasons as it's still possible to leave the current working directory with --output "{dir}/../../scaled/{base}" respectively just --output "../generated/{base}".