Skip to content

Commit

Permalink
Create intermediate directories if they do not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 29, 2022
1 parent f4f8840 commit b669dbd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global document */
import process from 'node:process';
import {promises as fs} from 'node:fs';
import path from 'node:path';
import fileUrl from 'file-url';
import puppeteer from 'puppeteer';
import toughCookie from 'tough-cookie';
Expand Down Expand Up @@ -440,6 +441,8 @@ const captureWebsite = {};
captureWebsite.file = async (url, filePath, options = {}) => {
const screenshot = await internalCaptureWebsite(url, options);

await fs.mkdir(path.dirname(filePath), {recursive: true});

await fs.writeFile(filePath, screenshot, {
flag: options.overwrite ? 'w' : 'wx',
});
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ await captureWebsite.file('https://sindresorhus.com', 'screenshot.png');

Capture a screenshot of the given `input` and save it to the given `outputFilePath`.

Intermediate directories are created for you if they do not exist.

Returns a `Promise<void>` that resolves when the screenshot is written.

### captureWebsite.buffer(input, options?)
Expand Down

0 comments on commit b669dbd

Please sign in to comment.