2020-04-08 18:43:35 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-08 05:26:39 -04:00
|
|
|
import type { CallbackWithError } from "./_fs_common.ts";
|
2020-05-03 15:14:38 -04:00
|
|
|
import { fromFileUrl } from "../path.ts";
|
2020-04-08 18:43:35 -04:00
|
|
|
|
2020-04-20 05:29:37 -04:00
|
|
|
/**
|
2020-05-03 15:14:38 -04:00
|
|
|
* TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
|
2020-04-20 05:29:37 -04:00
|
|
|
* are implemented. See https://github.com/denoland/deno/issues/3403
|
|
|
|
*/
|
2020-04-08 18:43:35 -04:00
|
|
|
type MkdirOptions =
|
|
|
|
| { recursive?: boolean; mode?: number | undefined }
|
|
|
|
| number
|
|
|
|
| boolean;
|
|
|
|
|
|
|
|
export function mkdir(
|
2020-05-03 15:14:38 -04:00
|
|
|
path: string | URL,
|
2020-04-08 18:43:35 -04:00
|
|
|
options?: MkdirOptions | CallbackWithError,
|
2020-07-14 15:24:17 -04:00
|
|
|
callback?: CallbackWithError,
|
2020-04-08 18:43:35 -04:00
|
|
|
): void {
|
2020-05-03 15:14:38 -04:00
|
|
|
path = path instanceof URL ? fromFileUrl(path) : path;
|
|
|
|
|
2020-04-08 18:43:35 -04:00
|
|
|
let mode = 0o777;
|
|
|
|
let recursive = false;
|
|
|
|
|
|
|
|
if (typeof options == "function") {
|
2020-04-27 08:48:54 -04:00
|
|
|
callback = options;
|
2020-04-08 18:43:35 -04:00
|
|
|
} 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;
|
|
|
|
}
|
2020-07-08 05:26:39 -04:00
|
|
|
if (typeof recursive !== "boolean") {
|
2020-04-08 18:43:35 -04:00
|
|
|
throw new Deno.errors.InvalidData(
|
2020-07-14 15:24:17 -04:00
|
|
|
"invalid recursive option , must be a boolean",
|
2020-04-08 18:43:35 -04:00
|
|
|
);
|
2020-07-08 05:26:39 -04:00
|
|
|
}
|
2020-04-20 05:29:37 -04:00
|
|
|
Deno.mkdir(path, { recursive, mode })
|
2020-04-08 18:43:35 -04:00
|
|
|
.then(() => {
|
2020-04-29 21:36:44 -04:00
|
|
|
if (typeof callback === "function") {
|
2020-04-08 18:43:35 -04:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2020-04-29 21:36:44 -04:00
|
|
|
if (typeof callback === "function") {
|
2020-04-08 18:43:35 -04:00
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:14:38 -04:00
|
|
|
export function mkdirSync(path: string | URL, options?: MkdirOptions): void {
|
|
|
|
path = path instanceof URL ? fromFileUrl(path) : path;
|
2020-04-08 18:43:35 -04:00
|
|
|
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;
|
|
|
|
}
|
2020-07-08 05:26:39 -04:00
|
|
|
if (typeof recursive !== "boolean") {
|
2020-04-08 18:43:35 -04:00
|
|
|
throw new Deno.errors.InvalidData(
|
2020-07-14 15:24:17 -04:00
|
|
|
"invalid recursive option , must be a boolean",
|
2020-04-08 18:43:35 -04:00
|
|
|
);
|
2020-07-08 05:26:39 -04:00
|
|
|
}
|
2020-04-29 21:36:44 -04:00
|
|
|
|
|
|
|
Deno.mkdirSync(path, { recursive, mode });
|
2020-04-08 18:43:35 -04:00
|
|
|
}
|