Skip to content

Commit

Permalink
fix: handle directory being passed to Deno.remove (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Apr 11, 2022
1 parent ec1b1cf commit de0153a
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 10 deletions.
7 changes: 6 additions & 1 deletion packages/shim-deno/src/deno/stable/functions/createSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
import { openSync } from "./openSync.js";

export const createSync: typeof Deno.createSync = function createSync(path) {
return openSync(path, { create: true, truncate: true });
return openSync(path, {
create: true,
truncate: true,
read: true,
write: true,
});
};
9 changes: 7 additions & 2 deletions packages/shim-deno/src/deno/stable/functions/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { promisify } from "util";

import { File } from "../classes/FsFile.js";
import { getFsFlag } from "../../internal/fs_flags.js";
import mapError from "../../internal/errorMap.js";

const nodeOpen = promisify(_open);

Expand All @@ -22,6 +23,10 @@ export const open: typeof Deno.open = async function open(
create,
createNew,
});
const fd = await nodeOpen(path, flagMode, mode);
return new File(fd);
try {
const fd = await nodeOpen(path, flagMode, mode);
return new File(fd);
} catch (err) {
throw mapError(err);
}
};
9 changes: 7 additions & 2 deletions packages/shim-deno/src/deno/stable/functions/openSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { openSync as nodeOpenSync } from "fs";

import { File } from "../classes/FsFile.js";
import { getFsFlag } from "../../internal/fs_flags.js";
import mapError from "../../internal/errorMap.js";

export const openSync: typeof Deno.openSync = function openSync(
path,
Expand All @@ -19,6 +20,10 @@ export const openSync: typeof Deno.openSync = function openSync(
create,
createNew,
});
const fd = nodeOpenSync(path, flagMode, mode);
return new File(fd);
try {
const fd = nodeOpenSync(path, flagMode, mode);
return new File(fd);
} catch (err) {
throw mapError(err);
}
};
7 changes: 7 additions & 0 deletions packages/shim-deno/src/deno/stable/functions/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import { read as nodeRead } from "fs";
const _read = promisify(nodeRead);

export const read: typeof Deno.read = async function read(rid, buffer) {
if (buffer == null) {
throw new TypeError("Buffer must not be null.");
}
if (buffer.length === 0) {
return 0;
}

const { bytesRead } = await _read(rid, buffer, 0, buffer.length, null);
// node returns 0 on EOF, Deno expects null
return bytesRead === 0 ? null : bytesRead;
Expand Down
17 changes: 14 additions & 3 deletions packages/shim-deno/src/deno/stable/functions/remove.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
///<reference path="../lib.deno.d.ts" />

import { rm } from "fs/promises";
import { rm, rmdir } from "fs/promises";

export const remove: typeof Deno.remove = function remove(
export const remove: typeof Deno.remove = async function remove(
path,
options = {},
) {
return rm(path, options.recursive ? { recursive: true, force: true } : {});
const innerOptions = options.recursive
? { recursive: true, force: true }
: {};
try {
return await rm(path, innerOptions);
} catch (err) {
if ((err as any).code === "ERR_FS_EISDIR") {
return await rmdir(path, innerOptions);
} else {
throw err;
}
}
};
16 changes: 14 additions & 2 deletions packages/shim-deno/src/deno/stable/functions/removeSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,17 @@

import * as fs from "fs";

export const removeSync: typeof Deno.removeSync = (path, options = {}) =>
fs.rmSync(path, options.recursive ? { recursive: true, force: true } : {});
export const removeSync: typeof Deno.removeSync = (path, options = {}) => {
const innerOptions = options.recursive
? { recursive: true, force: true }
: {};
try {
fs.rmSync(path, innerOptions);
} catch (err) {
if ((err as any).code === "ERR_FS_EISDIR") {
fs.rmdirSync(path, innerOptions);
} else {
throw err;
}
}
};
28 changes: 28 additions & 0 deletions packages/shim-deno/tools/run_tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ const testsToSkip = new Set([
// event_target_test
"eventTargetThisShouldDefaultToWindow", // window

// files_test
"filesIter", // deprecated
"filesIterCustomBufSize", // deprecated
"filesIterSync", // deprecated
"filesIterSyncCustomBufSize", // deprecated
"readerIter", // deprecated
"readerIterSync", // deprecated
"writePermFailure", // permissions
"openOptions", // todo
"openMode", // depends on umask
"openSyncMode", // depends on umask
"openSyncUrl", // depends on umask
"openUrl", // depends on umask
"readPermFailure", // permissions
"readWritePermFailure", // permissions
"openNotFound", // todo
"openModeWriteRead", // not implemented
"seekStart", // not implemented
"seekSyncStart", // not implemented
"seekCurrent", // not implemented
"seekSyncCurrent", // not implemented
"seekEnd", // not implemented
"seekSyncEnd", // not implemented
"seekMode", // not implemented
"readableStream", // not implemented
"readableStreamTextEncoderPipe", // not implemented
"writableStream", // not implemented

// mkdir_test
"mkdirMode", // depends on Deno.umask
"mkdirRecursiveIfExists", // depends on Deno.umask
Expand Down
1 change: 1 addition & 0 deletions packages/shim-deno/tools/working_test_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ third_party/deno/cli/tests/unit/copy_file_test.ts
third_party/deno/cli/tests/unit/dir_test.ts
third_party/deno/cli/tests/unit/error_stack_test.ts
third_party/deno/cli/tests/unit/event_target_test.ts
third_party/deno/cli/tests/unit/files_test.ts
third_party/deno/cli/tests/unit/get_random_values_test.ts
third_party/deno/cli/tests/unit/io_test.ts
third_party/deno/cli/tests/unit/message_channel_test.ts
Expand Down

0 comments on commit de0153a

Please sign in to comment.