mirror of
https://github.com/denoland/deno.git
synced 2025-01-22 15:10:44 -05:00
feat(std/node): add fs.mkdir functions and fs.exists tests (#4660)
This commit is contained in:
parent
0cf4ac91e6
commit
68bde7a0c6
3 changed files with 104 additions and 2 deletions
26
std/node/_fs/_fs_exists_test.ts
Normal file
26
std/node/_fs/_fs_exists_test.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
import { assertEquals } from "../../testing/asserts.ts";
|
||||
import { exists, existsSync } from "./_fs_exists.ts";
|
||||
|
||||
const { test } = Deno;
|
||||
|
||||
test(async function existsFile() {
|
||||
const availableFile = await new Promise(async (resolve) => {
|
||||
const tmpFilePath = await Deno.makeTempFile();
|
||||
exists(tmpFilePath, (exists: boolean) => resolve(exists));
|
||||
Deno.remove(tmpFilePath);
|
||||
});
|
||||
const notAvailableFile = await new Promise((resolve) => {
|
||||
exists("./notAvailable.txt", (exists: boolean) => resolve(exists));
|
||||
});
|
||||
assertEquals(availableFile, true);
|
||||
assertEquals(notAvailableFile, false);
|
||||
});
|
||||
|
||||
test(function existsSyncFile() {
|
||||
const tmpFilePath = Deno.makeTempFileSync();
|
||||
assertEquals(existsSync(tmpFilePath), true);
|
||||
Deno.removeSync(tmpFilePath);
|
||||
assertEquals(existsSync("./notAvailable.txt"), false);
|
||||
});
|
73
std/node/_fs/_fs_mkdir.ts
Normal file
73
std/node/_fs/_fs_mkdir.ts
Normal file
|
@ -0,0 +1,73 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
import { CallbackWithError } from "./_fs_common.ts";
|
||||
|
||||
type Path = string; // TODO path can also be a Buffer or URL.
|
||||
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"
|
||||
);
|
||||
new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
await Deno.mkdir(path, recursive, mode);
|
||||
resolve();
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
}
|
||||
})
|
||||
.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 {
|
||||
Deno.mkdirSync(path, recursive, mode);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ import * as constants from "./_fs/_fs_constants.ts";
|
|||
import { readFile, readFileSync } from "./_fs/_fs_readFile.ts";
|
||||
import { readlink, readlinkSync } from "./_fs/_fs_readlink.ts";
|
||||
import { exists, existsSync } from "./_fs/_fs_exists.ts";
|
||||
import { mkdir, mkdirSync } from "./_fs/_fs_mkdir.ts";
|
||||
|
||||
export {
|
||||
access,
|
||||
|
@ -22,10 +23,12 @@ export {
|
|||
close,
|
||||
closeSync,
|
||||
constants,
|
||||
exists,
|
||||
existsSync,
|
||||
readFile,
|
||||
readFileSync,
|
||||
readlink,
|
||||
readlinkSync,
|
||||
exists,
|
||||
existsSync,
|
||||
mkdir,
|
||||
mkdirSync,
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue