Skip to content

Commit

Permalink
Improve error when relative path is passed to the directory option
Browse files Browse the repository at this point in the history
Closes #143
  • Loading branch information
sindresorhus committed Nov 10, 2021
1 parent 972e0e8 commit d987a28
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ declare namespace electronDl {
readonly saveAs?: boolean;

/**
Directory to save the file in.
The directory to save the file in.
Must be an absolute path.
Default: [User's downloads directory](https://electronjs.org/docs/api/app/#appgetpathname)
*/
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ function registerListener(session, options, callback = () => {}) {

const window_ = majorElectronVersion() >= 12 ? BrowserWindow.fromWebContents(webContents) : getWindowFromWebContents(webContents);

if (options.directory && !path.isAbsolute(options.directory)) {
throw new Error('The `directory` option must be an absolute path');
}

const directory = options.directory || app.getPath('downloads');

let filePath;
if (options.filename) {
filePath = path.join(directory, options.filename);
Expand Down
2 changes: 0 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference lib="dom"/>
/// <reference types="node"/>
import {expectType} from 'tsd';
import {BrowserWindow, DownloadItem} from 'electron';
import electronDl = require('.');
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ Note: Only use this option when strictly necessary. Downloading directly without
Type: `string`\
Default: [User's downloads directory](https://electronjs.org/docs/api/app/#appgetpathname)

Directory to save the file in.
The directory to save the file in.

Must be an absolute path.

#### filename

Expand Down

0 comments on commit d987a28

Please sign in to comment.