Skip to content

Commit

Permalink
Add overwrite option
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 14, 2019
1 parent b7d3511 commit 0cc9686
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,12 @@ const captureWebsite = async (url, options) => {
return buffer;
};

module.exports.file = async (url, filePath, options) => {
module.exports.file = async (url, filePath, options = {}) => {
const screenshot = await captureWebsite(url, options);
await writeFile(filePath, screenshot, {flag: 'wx'});

await writeFile(filePath, screenshot, {
flag: options.overwrite ? 'w' : 'wx'
});
};

module.exports.buffer = async (url, options) => captureWebsite(url, {...options, encoding: 'binary'});
Expand Down
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,15 @@ Options passed to [`puppeteer.launch()`](https://github.com/GoogleChrome/puppete

Note: Some of the launch options are overridden by the `debug` option.

##### overwrite

Type: `boolean`<br>
Default: `false`

Overwrite the destination file if it exists instead of throwing an error.

*This option applies only to `captureWebsite.file()`.*

### captureWebsite.devices

Type: `string[]`
Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,23 @@ test('`authentication` option', async t => {
})));
});

test('`overwrite` option', async t => {
const filePath = tempy.file();

await t.notThrowsAsync(async () => {
await captureWebsite.file(server.url, filePath, {
width: 100,
height: 100
});

await captureWebsite.file(server.url, filePath, {
width: 100,
height: 100,
overwrite: true
});
});
});

test('handle redirects', async t => {
const server = await createTestServer();

Expand Down

0 comments on commit 0cc9686

Please sign in to comment.