mirror of
https://github.com/denoland/deno.git
synced 2025-01-22 06:09:25 -05:00
Combine deno.removeAll into deno.remove (#1596)
This commit is contained in:
parent
f05fd7a1f3
commit
f7c0f49443
4 changed files with 37 additions and 46 deletions
|
@ -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";
|
||||
|
|
|
@ -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 });
|
||||
});
|
||||
|
|
55
js/remove.ts
55
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<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");
|
||||
*/
|
||||
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<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];
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue