Allow --input to be a single file when --output is a folder #1675
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The README currently has example CLI usage, where you can either:
a) process one or more single input files into the same number of output files:
svgo one.svg two.svg -o one.min.svg two.min.svg
or
b) process a directory of files, into an output directory:
svgo -f ./path/to/folder/with/svg/files -o ./path/to/folder/with/svg/output
I had hoped that I'd be able to do an in-between, with a single file as the input and a directory as the --output:
svgo one.svg -o ./path/to/folder/with/svg/output
That would let me use chokidar in my project's package.json, to watch any SVGs that are updated, and re-run SVGO on them:
"svg:watch": "chokidar src/assets/svg/*.svg -c 'svgo --input {path} --output dist/assets/svg'"
Unfortunately, I ran into an issue where if any layer of the output directory did not exist before SVGO ran, it would have unexpected results, like
Error: ENOENT: no such file or directory
, or creating a weirdassets/svg
file, that should have beenassets/svg/mySvg.svg
.My fix replaces the check to see if a single --output string matches a pre-existing directory, with a check that it's simply a directory string (rather than a filename string with a file extension). That way, if you intend to put files into an output directory, but the directory doesn't yet exist, the surrounding code in coa.js successfully creates both the intended directory and output file inside it.
If this PR is accepted, it might be helpful to put a relevant single-input folder-output example in the CLI usage section of the README, to show that this can now be done?