diff options
author | Jan Lo <loujiayu7@gmail.com> | 2019-01-29 06:54:52 +0800 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-01-28 17:54:52 -0500 |
commit | f7c0f4944352f5bd2bb04d6c64e6259357d3827a (patch) | |
tree | d73f8502732612958626e299f9bf031ef2d4e88a /js | |
parent | f05fd7a1f3a1114b3d525613d96f85967c224815 (diff) |
Combine deno.removeAll into deno.remove (#1596)
Diffstat (limited to 'js')
-rw-r--r-- | js/deno.ts | 2 | ||||
-rw-r--r-- | js/files_test.ts | 6 | ||||
-rw-r--r-- | js/remove.ts | 55 | ||||
-rw-r--r-- | js/remove_test.ts | 20 |
4 files changed, 37 insertions, 46 deletions
diff --git a/js/deno.ts b/js/deno.ts index c4d764b4b..24ef43b28 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -34,7 +34,7 @@ export { Buffer, readAll } from "./buffer"; export { mkdirSync, mkdir } from "./mkdir"; export { makeTempDirSync, makeTempDir } from "./make_temp_dir"; export { chmodSync, chmod } from "./chmod"; -export { removeSync, remove, removeAllSync, removeAll } from "./remove"; +export { removeSync, remove } from "./remove"; export { renameSync, rename } from "./rename"; export { readFileSync, readFile } from "./read_file"; export { readDirSync, readDir } from "./read_dir"; diff --git a/js/files_test.ts b/js/files_test.ts index e46ab1684..9014e7b83 100644 --- a/js/files_test.ts +++ b/js/files_test.ts @@ -61,7 +61,7 @@ testPerm({ write: true }, async function createFile() { f.close(); // TODO: test different modes - await deno.removeAll(tempDir); + await deno.remove(tempDir, { recursive: true }); }); testPerm({ write: true }, async function openModeWrite() { @@ -95,7 +95,7 @@ testPerm({ write: true }, async function openModeWrite() { file.close(); const fileSize = deno.statSync(filename).len; assertEqual(fileSize, 0); - await deno.removeAll(tempDir); + await deno.remove(tempDir, { recursive: true }); }); testPerm({ write: true }, async function openModeWriteRead() { @@ -124,5 +124,5 @@ testPerm({ write: true }, async function openModeWriteRead() { // assertEqual(result.nread, 13); // file.close(); - await deno.removeAll(tempDir); + await deno.remove(tempDir, { recursive: true }); }); diff --git a/js/remove.ts b/js/remove.ts index ac821f942..d9f69399d 100644 --- a/js/remove.ts +++ b/js/remove.ts @@ -3,55 +3,46 @@ import * as msg from "gen/msg_generated"; import * as flatbuffers from "./flatbuffers"; import * as dispatch from "./dispatch"; -/** Removes the named file or (empty) directory synchronously. Would throw - * error if permission denied, not found, or directory not empty. - * - * import { removeSync } from "deno"; - * removeSync("/path/to/empty_dir/or/file"); - */ -export function removeSync(path: string): void { - dispatch.sendSync(...req(path, false)); +export interface RemoveOption { + recursive?: boolean; } -/** Removes the named file or (empty) directory. Would throw error if - * permission denied, not found, or directory not empty. +/** Removes the named file or directory synchronously. Would throw + * error if permission denied, not found, or directory not empty if `recursive` + * set to false. + * `recursive` is set to false by default. * - * import { remove } from "deno"; - * await remove("/path/to/empty_dir/or/file"); - */ -export async function remove(path: string): Promise<void> { - await dispatch.sendAsync(...req(path, false)); -} - -/** Recursively removes the named file or directory synchronously. Would throw - * error if permission denied or not found. - * - * import { removeAllSync } from "deno"; - * removeAllSync("/path/to/dir/or/file"); + * import { removeSync } from "deno"; + * removeSync("/path/to/dir/or/file", {recursive: false}); */ -export function removeAllSync(path: string): void { - dispatch.sendSync(...req(path, true)); +export function removeSync(path: string, options: RemoveOption = {}): void { + dispatch.sendSync(...req(path, options)); } -/** Recursively removes the named file or directory. Would throw error if - * permission denied or not found. +/** Removes the named file or directory. Would throw error if + * permission denied, not found, or directory not empty if `recursive` set + * to false. + * `recursive` is set to false by default. * - * import { removeAll } from "deno"; - * await removeAll("/path/to/dir/or/file"); + * import { remove } from "deno"; + * await remove("/path/to/dir/or/file", {recursive: false}); */ -export async function removeAll(path: string): Promise<void> { - await dispatch.sendAsync(...req(path, true)); +export async function remove( + path: string, + options: RemoveOption = {} +): Promise<void> { + await dispatch.sendAsync(...req(path, options)); } function req( path: string, - recursive: boolean + options: RemoveOption ): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] { const builder = flatbuffers.createBuilder(); const path_ = builder.createString(path); msg.Remove.startRemove(builder); msg.Remove.addPath(builder, path_); - msg.Remove.addRecursive(builder, recursive); + msg.Remove.addRecursive(builder, !!options.recursive); const inner = msg.Remove.endRemove(builder); return [builder, msg.Any.Remove, inner]; } diff --git a/js/remove_test.ts b/js/remove_test.ts index 59879df8f..df4fe72d6 100644 --- a/js/remove_test.ts +++ b/js/remove_test.ts @@ -92,7 +92,7 @@ testPerm({ write: true }, function removeAllSyncDirSuccess() { deno.mkdirSync(path); let pathInfo = deno.statSync(path); assert(pathInfo.isDirectory()); // check exist first - deno.removeAllSync(path); // remove + deno.removeSync(path, { recursive: true }); // remove // We then check again after remove let err; try { @@ -112,7 +112,7 @@ testPerm({ write: true }, function removeAllSyncDirSuccess() { assert(pathInfo.isDirectory()); // check exist first const subPathInfo = deno.statSync(subPath); assert(subPathInfo.isDirectory()); // check exist first - deno.removeAllSync(path); // remove + deno.removeSync(path, { recursive: true }); // remove // We then check parent directory again after remove try { deno.statSync(path); @@ -132,7 +132,7 @@ testPerm({ write: true }, function removeAllSyncFileSuccess() { deno.writeFileSync(filename, data, 0o666); const fileInfo = deno.statSync(filename); assert(fileInfo.isFile()); // check exist first - deno.removeAllSync(filename); // remove + deno.removeSync(filename, { recursive: true }); // remove // We then check again after remove let err; try { @@ -150,7 +150,7 @@ testPerm({ write: true }, function removeAllSyncFail() { let err; try { // Non-existent - deno.removeAllSync("/baddir"); + deno.removeSync("/baddir", { recursive: true }); } catch (e) { err = e; } @@ -161,7 +161,7 @@ testPerm({ write: true }, function removeAllSyncFail() { testPerm({ write: false }, function removeAllSyncPerm() { let err; try { - deno.removeAllSync("/baddir"); + deno.removeSync("/baddir", { recursive: true }); } catch (e) { err = e; } @@ -258,7 +258,7 @@ testPerm({ write: true }, async function removeAllDirSuccess() { deno.mkdirSync(path); let pathInfo = deno.statSync(path); assert(pathInfo.isDirectory()); // check exist first - await deno.removeAll(path); // remove + await deno.remove(path, { recursive: true }); // remove // We then check again after remove let err; try { @@ -278,7 +278,7 @@ testPerm({ write: true }, async function removeAllDirSuccess() { assert(pathInfo.isDirectory()); // check exist first const subPathInfo = deno.statSync(subPath); assert(subPathInfo.isDirectory()); // check exist first - await deno.removeAll(path); // remove + await deno.remove(path, { recursive: true }); // remove // We then check parent directory again after remove try { deno.statSync(path); @@ -298,7 +298,7 @@ testPerm({ write: true }, async function removeAllFileSuccess() { deno.writeFileSync(filename, data, 0o666); const fileInfo = deno.statSync(filename); assert(fileInfo.isFile()); // check exist first - await deno.removeAll(filename); // remove + await deno.remove(filename, { recursive: true }); // remove // We then check again after remove let err; try { @@ -316,7 +316,7 @@ testPerm({ write: true }, async function removeAllFail() { let err; try { // Non-existent - await deno.removeAll("/baddir"); + await deno.remove("/baddir", { recursive: true }); } catch (e) { err = e; } @@ -327,7 +327,7 @@ testPerm({ write: true }, async function removeAllFail() { testPerm({ write: false }, async function removeAllPerm() { let err; try { - await deno.removeAll("/baddir"); + await deno.remove("/baddir", { recursive: true }); } catch (e) { err = e; } |