mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 23:34:47 -05:00
feat(std/node): adds fs.mkdtemp & fs.mkdtempSync (#8604)
This commit is contained in:
parent
091059450e
commit
34513c032c
3 changed files with 224 additions and 0 deletions
98
std/node/_fs/_fs_mkdtemp.ts
Normal file
98
std/node/_fs/_fs_mkdtemp.ts
Normal file
|
@ -0,0 +1,98 @@
|
|||
// Copyright Node.js contributors. All rights reserved. MIT License.
|
||||
import { existsSync } from "./_fs_exists.ts";
|
||||
import { mkdir, mkdirSync } from "./_fs_mkdir.ts";
|
||||
import {
|
||||
ERR_INVALID_CALLBACK,
|
||||
ERR_INVALID_OPT_VALUE_ENCODING,
|
||||
} from "../_errors.ts";
|
||||
|
||||
export type mkdtempCallback = (
|
||||
err: Error | undefined,
|
||||
directory?: string,
|
||||
) => void;
|
||||
|
||||
// https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_fs_mkdtemp_prefix_options_callback
|
||||
export function mkdtemp(prefix: string, callback: mkdtempCallback): void;
|
||||
export function mkdtemp(
|
||||
prefix: string,
|
||||
options: { encoding: string } | string,
|
||||
callback: mkdtempCallback,
|
||||
): void;
|
||||
export function mkdtemp(
|
||||
prefix: string,
|
||||
optionsOrCallback: { encoding: string } | string | mkdtempCallback,
|
||||
maybeCallback?: mkdtempCallback,
|
||||
): void {
|
||||
const callback: mkdtempCallback | undefined =
|
||||
typeof optionsOrCallback == "function" ? optionsOrCallback : maybeCallback;
|
||||
if (!callback) throw new ERR_INVALID_CALLBACK(callback);
|
||||
|
||||
const encoding: string | undefined = parseEncoding(optionsOrCallback);
|
||||
const path = tempDirPath(prefix);
|
||||
|
||||
mkdir(
|
||||
path,
|
||||
{ recursive: false, mode: 0o700 },
|
||||
(err: Error | null | undefined) => {
|
||||
if (err) callback(err);
|
||||
else callback(undefined, decode(path, encoding));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_fs_mkdtempsync_prefix_options
|
||||
export function mkdtempSync(
|
||||
prefix: string,
|
||||
options?: { encoding: string } | string,
|
||||
): string {
|
||||
const encoding: string | undefined = parseEncoding(options);
|
||||
const path = tempDirPath(prefix);
|
||||
|
||||
mkdirSync(path, { recursive: false, mode: 0o700 });
|
||||
return decode(path, encoding);
|
||||
}
|
||||
|
||||
function parseEncoding(
|
||||
optionsOrCallback?: { encoding: string } | string | mkdtempCallback,
|
||||
): string | undefined {
|
||||
let encoding: string | undefined;
|
||||
if (typeof optionsOrCallback == "function") encoding = undefined;
|
||||
else if (optionsOrCallback instanceof Object) {
|
||||
encoding = optionsOrCallback?.encoding;
|
||||
} else encoding = optionsOrCallback;
|
||||
|
||||
if (encoding) {
|
||||
try {
|
||||
new TextDecoder(encoding);
|
||||
} catch (error) {
|
||||
throw new ERR_INVALID_OPT_VALUE_ENCODING(encoding);
|
||||
}
|
||||
}
|
||||
|
||||
return encoding;
|
||||
}
|
||||
|
||||
function decode(str: string, encoding?: string): string {
|
||||
if (!encoding) return str;
|
||||
else {
|
||||
const decoder = new TextDecoder(encoding);
|
||||
const encoder = new TextEncoder();
|
||||
return decoder.decode(encoder.encode(str));
|
||||
}
|
||||
}
|
||||
|
||||
const CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
function randomName(): string {
|
||||
return [...Array(6)].map(() =>
|
||||
CHARS[Math.floor(Math.random() * CHARS.length)]
|
||||
).join("");
|
||||
}
|
||||
|
||||
function tempDirPath(prefix: string): string {
|
||||
let path: string;
|
||||
do {
|
||||
path = prefix + randomName();
|
||||
} while (existsSync(path));
|
||||
|
||||
return path;
|
||||
}
|
121
std/node/_fs/_fs_mkdtemp_test.ts
Normal file
121
std/node/_fs/_fs_mkdtemp_test.ts
Normal file
|
@ -0,0 +1,121 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
import { assert } from "../../testing/asserts.ts";
|
||||
import { mkdtemp, mkdtempSync } from "./_fs_mkdtemp.ts";
|
||||
import { existsSync } from "./_fs_exists.ts";
|
||||
import { env } from "../process.ts";
|
||||
import { isWindows } from "../../_util/os.ts";
|
||||
import { promisify } from "../_util/_util_promisify.ts";
|
||||
|
||||
const prefix = isWindows ? env.TEMP + "\\" : (env.TMPDIR || "/tmp") + "/";
|
||||
const doesNotExists = "/does/not/exists/";
|
||||
const options = { encoding: "ascii" };
|
||||
const badOptions = { encoding: "bogus" };
|
||||
|
||||
const mkdtempP = promisify(mkdtemp);
|
||||
|
||||
Deno.test({
|
||||
name: "[node/fs] mkdtemp",
|
||||
fn: async () => {
|
||||
try {
|
||||
const directory = await mkdtempP(prefix);
|
||||
assert(existsSync(directory));
|
||||
Deno.removeSync(directory);
|
||||
} catch (error) {
|
||||
assert(false);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[node/fs] mkdtemp (does not exists)",
|
||||
fn: async () => {
|
||||
try {
|
||||
const directory = await mkdtempP(doesNotExists);
|
||||
|
||||
// should have thrown already...
|
||||
assert(!existsSync(directory));
|
||||
Deno.removeSync(directory);
|
||||
} catch (error) {
|
||||
assert(true);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[node/fs] mkdtemp (with options)",
|
||||
fn: async () => {
|
||||
try {
|
||||
const directory = await mkdtempP(prefix, options);
|
||||
assert(existsSync(directory));
|
||||
Deno.removeSync(directory);
|
||||
} catch (error) {
|
||||
assert(false);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[node/fs] mkdtemp (with bad options)",
|
||||
fn: async () => {
|
||||
try {
|
||||
const directory = await mkdtempP(prefix, badOptions);
|
||||
|
||||
// should have thrown already...
|
||||
assert(!existsSync(directory));
|
||||
Deno.removeSync(directory);
|
||||
} catch (error) {
|
||||
assert(true);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[node/fs] mkdtempSync",
|
||||
fn: () => {
|
||||
const directory = mkdtempSync(prefix);
|
||||
const dirExists = existsSync(directory);
|
||||
Deno.removeSync(directory);
|
||||
assert(dirExists);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[node/fs] mkdtempSync (does not exists)",
|
||||
fn: () => {
|
||||
try {
|
||||
const directory = mkdtempSync(doesNotExists);
|
||||
// should have thrown already...
|
||||
|
||||
const dirExists = existsSync(directory);
|
||||
Deno.removeSync(directory);
|
||||
assert(!dirExists);
|
||||
} catch (error) {
|
||||
assert(true);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[node/fs] mkdtempSync (with options)",
|
||||
fn: () => {
|
||||
const directory = mkdtempSync(prefix, options);
|
||||
const dirExists = existsSync(directory);
|
||||
Deno.removeSync(directory);
|
||||
assert(dirExists);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[node/fs] mkdtempSync (with bad options)",
|
||||
fn: () => {
|
||||
try {
|
||||
const directory = mkdtempSync(prefix, badOptions);
|
||||
// should have thrown already...
|
||||
|
||||
Deno.removeSync(directory);
|
||||
assert(false);
|
||||
} catch (error) {
|
||||
assert(true);
|
||||
}
|
||||
},
|
||||
});
|
|
@ -9,6 +9,7 @@ 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";
|
||||
import { mkdtemp, mkdtempSync } from "./_fs/_fs_mkdtemp.ts";
|
||||
import { copyFile, copyFileSync } from "./_fs/_fs_copy.ts";
|
||||
import { writeFile, writeFileSync } from "./_fs/_fs_writeFile.ts";
|
||||
import { readdir, readdirSync } from "./_fs/_fs_readdir.ts";
|
||||
|
@ -43,6 +44,8 @@ export default {
|
|||
lstatSync,
|
||||
mkdir,
|
||||
mkdirSync,
|
||||
mkdtemp,
|
||||
mkdtempSync,
|
||||
open,
|
||||
openSync,
|
||||
promises,
|
||||
|
@ -87,6 +90,8 @@ export {
|
|||
lstatSync,
|
||||
mkdir,
|
||||
mkdirSync,
|
||||
mkdtemp,
|
||||
mkdtempSync,
|
||||
open,
|
||||
openSync,
|
||||
promises,
|
||||
|
|
Loading…
Reference in a new issue