-
Notifications
You must be signed in to change notification settings - Fork 622
/
_fs_mkdir.ts
77 lines (67 loc) · 2.2 KB
/
_fs_mkdir.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import type { CallbackWithError } from "./_fs_common.ts";
import { promisify } from "../internal/util.mjs";
import { denoErrorToNodeError } from "../internal/errors.ts";
import { getValidatedPath } from "../internal/fs/utils.mjs";
import { validateBoolean } from "../internal/validators.mjs";
/**
* TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
type MkdirOptions =
| { recursive?: boolean; mode?: number | undefined }
| number
| boolean;
export function mkdir(
path: string | URL,
options?: MkdirOptions | CallbackWithError,
callback?: CallbackWithError,
) {
path = getValidatedPath(path) as string;
let mode = 0o777;
let recursive = false;
if (typeof options == "function") {
callback = options;
} else if (typeof options === "number") {
mode = options;
} else if (typeof options === "boolean") {
recursive = options;
} else if (options) {
if (options.recursive !== undefined) recursive = options.recursive;
if (options.mode !== undefined) mode = options.mode;
}
validateBoolean(recursive, "options.recursive");
Deno.mkdir(path, { recursive, mode })
.then(() => {
if (typeof callback === "function") {
callback(null);
}
}, (err) => {
if (typeof callback === "function") {
callback(err);
}
});
}
export const mkdirPromise = promisify(mkdir) as (
path: string | URL,
options?: MkdirOptions,
) => Promise<void>;
export function mkdirSync(path: string | URL, options?: MkdirOptions) {
path = getValidatedPath(path) as string;
let mode = 0o777;
let recursive = false;
if (typeof options === "number") {
mode = options;
} else if (typeof options === "boolean") {
recursive = options;
} else if (options) {
if (options.recursive !== undefined) recursive = options.recursive;
if (options.mode !== undefined) mode = options.mode;
}
validateBoolean(recursive, "options.recursive");
try {
Deno.mkdirSync(path, { recursive, mode });
} catch (err) {
throw denoErrorToNodeError(err as Error, { syscall: "mkdir", path });
}
}