From f7c0f4944352f5bd2bb04d6c64e6259357d3827a Mon Sep 17 00:00:00 2001 From: Jan Lo Date: Tue, 29 Jan 2019 06:54:52 +0800 Subject: [PATCH] Combine deno.removeAll into deno.remove (#1596) --- js/deno.ts | 2 +- js/files_test.ts | 6 +++--- js/remove.ts | 55 ++++++++++++++++++++--------------------------- js/remove_test.ts | 20 ++++++++--------- 4 files changed, 37 insertions(+), 46 deletions(-) diff --git a/js/deno.ts b/js/deno.ts index c4d764b4b6..24ef43b288 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 e46ab1684e..9014e7b831 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 ac821f9424..d9f69399da 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. +export interface RemoveOption { + recursive?: boolean; +} + +/** 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 { removeSync } from "deno"; - * removeSync("/path/to/empty_dir/or/file"); + * removeSync("/path/to/dir/or/file", {recursive: false}); */ -export function removeSync(path: string): void { - dispatch.sendSync(...req(path, false)); +export function removeSync(path: string, options: RemoveOption = {}): void { + dispatch.sendSync(...req(path, options)); } -/** 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. 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"); + * await remove("/path/to/dir/or/file", {recursive: false}); */ -export async function remove(path: string): Promise { - 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"); - */ -export function removeAllSync(path: string): void { - dispatch.sendSync(...req(path, true)); -} - -/** Recursively removes the named file or directory. Would throw error if - * permission denied or not found. - * - * import { removeAll } from "deno"; - * await removeAll("/path/to/dir/or/file"); - */ -export async function removeAll(path: string): Promise { - await dispatch.sendAsync(...req(path, true)); +export async function remove( + path: string, + options: RemoveOption = {} +): Promise { + 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 59879df8fb..df4fe72d6f 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; }