Skip to content

Commit 776fb01

Browse files
committed
Accept file URL instances
Fixes #36
1 parent 5ff9523 commit 776fb01

5 files changed

Lines changed: 48 additions & 3 deletions

File tree

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ console.log(paths);
4646
// ]
4747
```
4848
*/
49-
export function makeDirectory(path: string, options?: Options): Promise<string>;
49+
export function makeDirectory(path: string | URL, options?: Options): Promise<string>;
5050

5151
/**
5252
Synchronously make a directory and its parents if needed - Think `mkdir -p`.
@@ -64,4 +64,4 @@ console.log(path);
6464
//=> '/Users/sindresorhus/fun/unicorn/rainbow/cake'
6565
```
6666
*/
67-
export function makeDirectorySync(path: string, options?: Options): string;
67+
export function makeDirectorySync(path: string | URL, options?: Options): string;

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import process from 'node:process';
22
import fs from 'node:fs';
33
import fsPromises from 'node:fs/promises';
44
import path from 'node:path';
5+
import {fileURLToPath} from 'node:url';
6+
7+
const toPath = urlOrPath => {
8+
if (urlOrPath instanceof URL) {
9+
return fileURLToPath(urlOrPath);
10+
}
11+
12+
return urlOrPath;
13+
};
514

615
// https://github.com/nodejs/node/issues/8987
716
// https://github.com/libuv/libuv/pull/1088
@@ -41,6 +50,7 @@ const permissionError = pth => {
4150
};
4251

4352
export async function makeDirectory(input, options) {
53+
input = toPath(input);
4454
checkPath(input);
4555
options = processOptions(options);
4656

@@ -96,6 +106,7 @@ export async function makeDirectory(input, options) {
96106
}
97107

98108
export function makeDirectorySync(input, options) {
109+
input = toPath(input);
99110
checkPath(input);
100111
options = processOptions(options);
101112

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Returns the path to the created directory.
8181

8282
#### path
8383

84-
Type: `string`
84+
Type: `string | URL`
8585

8686
The directory to create.
8787

test/async.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import process from 'node:process';
22
import fs from 'node:fs';
33
import path from 'node:path';
4+
import {pathToFileURL} from 'node:url';
45
import test from 'ava';
56
import {temporaryDirectory, temporaryFile} from 'tempy';
67
import gracefulFs from 'graceful-fs';
@@ -118,6 +119,22 @@ test.serial('handles invalid path characters', async t => {
118119
});
119120
});
120121

122+
test('accepts URL instance', async t => {
123+
const directory = getFixture();
124+
const urlPath = pathToFileURL(directory);
125+
const madeDirectory = await makeDirectory(urlPath);
126+
t.true(madeDirectory.length > 0);
127+
assertDirectory(t, madeDirectory);
128+
});
129+
130+
test('accepts URL instance with custom fs', async t => {
131+
const directory = getFixture();
132+
const urlPath = pathToFileURL(directory);
133+
const madeDirectory = await makeDirectory(urlPath, customFsOptions);
134+
t.true(madeDirectory.length > 0);
135+
assertDirectory(t, madeDirectory);
136+
});
137+
121138
test('preserves EACCES error when parent directory lacks execute permission', async t => {
122139
if (process.platform === 'win32') {
123140
t.pass('Skip on Windows');

test/sync.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import process from 'node:process';
22
import fs from 'node:fs';
33
import path from 'node:path';
4+
import {pathToFileURL} from 'node:url';
45
import test from 'ava';
56
import {temporaryDirectory, temporaryFile} from 'tempy';
67
import gracefulFs from 'graceful-fs';
@@ -107,6 +108,22 @@ test('handles null bytes in path', t => {
107108
t.regex(error.code, /ERR_INVALID_ARG_VALUE|ENOENT/);
108109
});
109110

111+
test('accepts URL instance', t => {
112+
const directory = getFixture();
113+
const urlPath = pathToFileURL(directory);
114+
const madeDirectory = makeDirectorySync(urlPath);
115+
t.true(madeDirectory.length > 0);
116+
assertDirectory(t, madeDirectory);
117+
});
118+
119+
test('accepts URL instance with custom fs', t => {
120+
const directory = getFixture();
121+
const urlPath = pathToFileURL(directory);
122+
const madeDirectory = makeDirectorySync(urlPath, customFsOptions);
123+
t.true(madeDirectory.length > 0);
124+
assertDirectory(t, madeDirectory);
125+
});
126+
110127
test('preserves EACCES error when parent directory lacks execute permission', t => {
111128
if (process.platform === 'win32') {
112129
t.pass('Skip on Windows');

0 commit comments

Comments
 (0)