2020-04-08 18:43:35 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { CallbackWithError } from "./_fs_common.ts";
|
|
|
|
|
2020-04-20 05:29:37 -04:00
|
|
|
/**
|
|
|
|
* TODO: Also accept 'path' parameter as a Node polyfill Buffer or URL type once these
|
|
|
|
* are implemented. See https://github.com/denoland/deno/issues/3403
|
|
|
|
*/
|
|
|
|
type Path = string;
|
2020-04-08 18:43:35 -04:00
|
|
|
type MkdirOptions =
|
|
|
|
| { recursive?: boolean; mode?: number | undefined }
|
|
|
|
| number
|
|
|
|
| boolean;
|
|
|
|
|
|
|
|
export function mkdir(
|
|
|
|
path: Path,
|
|
|
|
options?: MkdirOptions | CallbackWithError,
|
|
|
|
callback?: CallbackWithError
|
|
|
|
): void {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (typeof recursive !== "boolean")
|
|
|
|
throw new Deno.errors.InvalidData(
|
|
|
|
"invalid recursive option , must be a boolean"
|
|
|
|
);
|
2020-04-20 05:29:37 -04:00
|
|
|
Deno.mkdir(path, { recursive, mode })
|
2020-04-08 18:43:35 -04:00
|
|
|
.then(() => {
|
|
|
|
if (callback && typeof callback == "function") {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
if (callback && typeof callback == "function") {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function mkdirSync(path: Path, options?: MkdirOptions): void {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (typeof recursive !== "boolean")
|
|
|
|
throw new Deno.errors.InvalidData(
|
|
|
|
"invalid recursive option , must be a boolean"
|
|
|
|
);
|
|
|
|
try {
|
2020-04-09 13:45:10 -04:00
|
|
|
Deno.mkdirSync(path, { recursive, mode });
|
2020-04-08 18:43:35 -04:00
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|