Skip to content

Commit

Permalink
Fixes #16.
Browse files Browse the repository at this point in the history
  • Loading branch information
vseventer committed Oct 15, 2019
1 parent 8c36901 commit c57a615
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Added support for HEIF images.
* Added [`--premultiplied`](https://sharp.pixelplumbing.com/en/latest/api-composite/#composite) option.
* Added [`--reductionEffort` and `--smartSubsample`](https://sharp.pixelplumbing.com/en/stable/api-output/#webp) options.
* Fixed bug with output directories ([#16](https://github.com/vseventer/sharp-cli/issues/16)).
* Removed [`overlayWith`](http://sharp.pixelplumbing.com/en/v0.21.3/api-composite/#overlaywith) command.
* Updated `fs-extra`, `mocha`, `sharp`, `sinon`, and `standard` dependencies.

Expand Down
24 changes: 14 additions & 10 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const path = require('path')

// Package modules.
const bubbleError = require('bubble-stream-error')
const isDirectory = require('is-directory')
const sharp = require('sharp')
const urlTemplate = require('url-template')

Expand All @@ -49,31 +50,34 @@ module.exports = {
// Convert a list of files.
files: (input, output) => {
const template = urlTemplate.parse(output) // Parse output template.
output = path.resolve(output)

// Process files.
const isBatch = input.length > 1
const promises = input.map((src) => {
// Destination.
src = path.resolve(src) // Absolute path.

// Process file.
const transformer = queue.drain(sharp(src))

// Destination.
let dest = template.expand(path.parse(src))

// Path separator for windows is encoded by url-template.
// @see https://github.com/vseventer/sharp-cli/issues/8
if (process.platform === 'win32') {
dest = dest.split('%5C').join(path.sep)
}
dest = path.resolve(dest) // Absolute path.

// Absolute path.
dest = path.resolve(dest)

// Process file.
const transformer = queue.drain(sharp(src))

// If output was not a template, re-use source name.
if (output === dest) {
// If output was not a template, assume dest is a directory when using
// batch processing.
const outputAssumeDir = dest === output && isBatch
if (outputAssumeDir || isDirectory.sync(dest)) {
const defaultExt = path.extname(src)
const desiredExt = transformer.options.formatOut
dest = path.format({
dir: output,
dir: dest,
name: path.basename(src, defaultExt),
ext: desiredExt in EXTENSIONS ? EXTENSIONS[desiredExt] : defaultExt
})
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"dependencies": {
"bubble-stream-error": "1.0.x",
"is-directory": "0.3.x",
"multiyargs": "1.0.x",
"sharp": "0.23.1",
"url-template": "2.0.x",
Expand Down
13 changes: 13 additions & 0 deletions test/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ describe('convert', () => {
.files([input], dest)
.then(([info]) => expect(fs.existsSync(info.path)).to.be.true)
})
it('must convert a file and output to an existing directory', () => {
// Negative test for directory that does not exist.
const rand = '' + Math.random()
return convert
.files([input, input], rand)
.then(() => { throw new Error('STOP') })
.catch((err) => {
expect(err).to.exist()
expect(err).to.have.property('message')
expect(err.message).to.contain(`${rand}/input.jpg`)
expect(err.message).to.contain('No such file or directory')
})
})
it('must convert multiple files', () => {
return convert
.files([input, input], dest)
Expand Down

0 comments on commit c57a615

Please sign in to comment.