Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2.2.0
* Added `dry` (do everything except write files).

## 2.1.0 (June 19, 2022)
* Added AVIF support.
* Added GIF support.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Global Options
--compressionLevel, -c zlib compression level [number] [default: 6]
--delay Delay(s) between animation frames [number]
--density DPI for vector images [number] [default: 72]
--dry, -n Do everything except write files [boolean]
--format, -f Force output to a given format
[choices: "input", "avif", "gif", "heif", "jpeg", "jpg", "png", "raw", "tiff", "webp"] [default:
"input"]
Expand Down
7 changes: 7 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ const options = {
type: 'number'
},

dry: {
alias: 'n',
desc: 'Do everything except write files',
group: _global,
type: 'boolean'
},

// @see https://sharp.pixelplumbing.com/api-output#toformat
format: {
alias: 'f',
Expand Down
5 changes: 2 additions & 3 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@ module.exports = {
})
}

// Write, attach info and return.
// Write to file unless we're doing a dry run, attach info and return.
fs.createReadStream(src).pipe(transformer)
return transformer
.toFile(dest)
return Promise.resolve(args.dry ? transformer : transformer.toFile(dest))
.then((info) => Object.assign(info, { src, path: dest }))
})
return Promise.all(promises)
Expand Down
6 changes: 6 additions & 0 deletions test/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ describe('convert', () => {
return convert
.files([input], dest, {})
})
it('must not write to disk if the dry flag is set', () => {
const out = path.join(dest, `{name}-${Math.random()}{ext}`)
return convert
.files([input], out, { dry: true })
.then(() => expect(fs.existsSync(out)).to.be.false())
})
})
describe('stream', () => {
// Default output.
Expand Down