summaryrefslogtreecommitdiff
path: root/cli/js
diff options
context:
space:
mode:
Diffstat (limited to 'cli/js')
-rw-r--r--cli/js/deno.ts1
-rw-r--r--cli/js/files.ts55
-rw-r--r--cli/js/lib.deno.ns.d.ts39
-rw-r--r--cli/js/ops/fs/open.ts17
-rw-r--r--cli/js/tests/files_test.ts54
-rw-r--r--cli/js/tests/process_test.ts7
-rw-r--r--cli/js/write_file.ts12
7 files changed, 69 insertions, 116 deletions
diff --git a/cli/js/deno.ts b/cli/js/deno.ts
index fd53b58e7..71d0fdf99 100644
--- a/cli/js/deno.ts
+++ b/cli/js/deno.ts
@@ -35,7 +35,6 @@ export {
seek,
seekSync,
OpenOptions,
- OpenMode,
} from "./files.ts";
export { read, readSync, write, writeSync } from "./ops/io.ts";
export { FsEvent, watchFs } from "./ops/fs_events.ts";
diff --git a/cli/js/files.ts b/cli/js/files.ts
index d09fcf8db..97a8c1e2b 100644
--- a/cli/js/files.ts
+++ b/cli/js/files.ts
@@ -18,60 +18,43 @@ import {
open as opOpen,
openSync as opOpenSync,
OpenOptions,
- OpenMode,
} from "./ops/fs/open.ts";
-export { OpenOptions, OpenMode } from "./ops/fs/open.ts";
+export { OpenOptions } from "./ops/fs/open.ts";
-export function openSync(path: string, options?: OpenOptions): File;
-export function openSync(path: string, openMode?: OpenMode): File;
-
-/**@internal*/
export function openSync(
path: string,
- modeOrOptions: OpenOptions | OpenMode = "r"
+ options: OpenOptions = { read: true }
): File {
- let openMode = undefined;
- let options = undefined;
-
- if (typeof modeOrOptions === "string") {
- openMode = modeOrOptions;
- } else {
- checkOpenOptions(modeOrOptions);
- options = modeOrOptions as OpenOptions;
- }
-
- const rid = opOpenSync(path, openMode as OpenMode, options);
+ checkOpenOptions(options);
+ const rid = opOpenSync(path, options);
return new File(rid);
}
-export async function open(path: string, options?: OpenOptions): Promise<File>;
-export async function open(path: string, openMode?: OpenMode): Promise<File>;
-
-/**@internal*/
export async function open(
path: string,
- modeOrOptions: OpenOptions | OpenMode = "r"
+ options: OpenOptions = { read: true }
): Promise<File> {
- let openMode = undefined;
- let options = undefined;
-
- if (typeof modeOrOptions === "string") {
- openMode = modeOrOptions;
- } else {
- checkOpenOptions(modeOrOptions);
- options = modeOrOptions as OpenOptions;
- }
-
- const rid = await opOpen(path, openMode as OpenMode, options);
+ checkOpenOptions(options);
+ const rid = await opOpen(path, options);
return new File(rid);
}
export function createSync(path: string): File {
- return openSync(path, "w+");
+ return openSync(path, {
+ read: true,
+ write: true,
+ truncate: true,
+ create: true,
+ });
}
export function create(path: string): Promise<File> {
- return open(path, "w+");
+ return open(path, {
+ read: true,
+ write: true,
+ truncate: true,
+ create: true,
+ });
}
export class File
diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts
index 7145751c7..84e287fca 100644
--- a/cli/js/lib.deno.ns.d.ts
+++ b/cli/js/lib.deno.ns.d.ts
@@ -653,18 +653,6 @@ declare namespace Deno {
*/
export function openSync(path: string, options?: OpenOptions): File;
- /** Synchronously open a file and return an instance of `Deno.File`. The file
- * may be created depending on the mode passed in. It is the callers responsibility
- * to close the file when finished with it.
- *
- * const file = Deno.openSync("/foo/bar.txt", "r");
- * // Do work with file
- * Deno.close(file.rid);
- *
- * Requires `allow-read` and/or `allow-write` permissions depending on openMode.
- */
- export function openSync(path: string, openMode?: OpenMode): File;
-
/** Open a file and resolve to an instance of `Deno.File`. The
* file does not need to previously exist if using the `create` or `createNew`
* open options. It is the callers responsibility to close the file when finished
@@ -678,18 +666,6 @@ declare namespace Deno {
*/
export function open(path: string, options?: OpenOptions): Promise<File>;
- /** Open a file and resolve to an instance of `Deno.File`. The file may be
- * created depending on the mode passed in. It is the callers responsibility
- * to close the file when finished with it.
- *
- * const file = await Deno.open("/foo/bar.txt", "w+");
- * // Do work with file
- * Deno.close(file.rid);
- *
- * Requires `allow-read` and/or `allow-write` permissions depending on openMode.
- */
- export function open(path: string, openMode?: OpenMode): Promise<File>;
-
/** Creates a file if none exists or truncates an existing file and returns
* an instance of `Deno.File`.
*
@@ -890,21 +866,6 @@ declare namespace Deno {
mode?: number;
}
- /** A set of string literals which specify how to open a file.
- *
- * |Value |Description |
- * |------|--------------------------------------------------------------------------------------------------|
- * |`"r"` |Read-only. Default. Starts at beginning of file. |
- * |`"r+"`|Read-write. Start at beginning of file. |
- * |`"w"` |Write-only. Opens and truncates existing file or creates new one for writing only. |
- * |`"w+"`|Read-write. Opens and truncates existing file or creates new one for writing and reading. |
- * |`"a"` |Write-only. Opens existing file or creates new one. Each write appends content to the end of file.|
- * |`"a+"`|Read-write. Behaves like `"a"` and allows to read from file. |
- * |`"x"` |Write-only. Exclusive create - creates new file only if one doesn't exist already. |
- * |`"x+"`|Read-write. Behaves like `x` and allows reading from file. |
- */
- export type OpenMode = "r" | "r+" | "w" | "w+" | "a" | "a+" | "x" | "x+";
-
/** **UNSTABLE**: new API, yet to be vetted
*
* Check if a given resource id (`rid`) is a TTY.
diff --git a/cli/js/ops/fs/open.ts b/cli/js/ops/fs/open.ts
index b587f491d..afe713db8 100644
--- a/cli/js/ops/fs/open.ts
+++ b/cli/js/ops/fs/open.ts
@@ -15,27 +15,16 @@ export interface OpenOptions {
mode?: number;
}
-export type OpenMode = "r" | "r+" | "w" | "w+" | "a" | "a+" | "x" | "x+";
-
-export function openSync(
- path: string,
- openMode: OpenMode | undefined,
- options: OpenOptions | undefined
-): number {
+export function openSync(path: string, options: OpenOptions): number {
const mode: number | undefined = options?.mode;
- return sendSync("op_open", { path, options, openMode, mode });
+ return sendSync("op_open", { path, options, mode });
}
-export function open(
- path: string,
- openMode: OpenMode | undefined,
- options: OpenOptions | undefined
-): Promise<number> {
+export function open(path: string, options: OpenOptions): Promise<number> {
const mode: number | undefined = options?.mode;
return sendAsync("op_open", {
path,
options,
- openMode,
mode,
});
}
diff --git a/cli/js/tests/files_test.ts b/cli/js/tests/files_test.ts
index 11a2e08ae..39f4d6ce2 100644
--- a/cli/js/tests/files_test.ts
+++ b/cli/js/tests/files_test.ts
@@ -201,11 +201,11 @@ unitTest(
{ perms: { write: false } },
async function writePermFailure(): Promise<void> {
const filename = "tests/hello.txt";
- const writeModes: Deno.OpenMode[] = ["w", "a", "x"];
- for (const mode of writeModes) {
+ const openOptions: Deno.OpenOptions[] = [{ write: true }, { append: true }];
+ for (const options of openOptions) {
let err;
try {
- await Deno.open(filename, mode);
+ await Deno.open(filename, options);
} catch (e) {
err = e;
}
@@ -266,8 +266,8 @@ unitTest({ perms: { read: false } }, async function readPermFailure(): Promise<
> {
let caughtError = false;
try {
- await Deno.open("package.json", "r");
- await Deno.open("cli/tests/fixture.json", "r");
+ await Deno.open("package.json", { read: true });
+ await Deno.open("cli/tests/fixture.json", { read: true });
} catch (e) {
caughtError = true;
assert(e instanceof Deno.errors.PermissionDenied);
@@ -308,7 +308,12 @@ unitTest(
async function readNullBufferFailure(): Promise<void> {
const tempDir = Deno.makeTempDirSync();
const filename = tempDir + "hello.txt";
- const file = await Deno.open(filename, "w+");
+ const file = await Deno.open(filename, {
+ read: true,
+ write: true,
+ truncate: true,
+ create: true,
+ });
// reading into an empty buffer should return 0 immediately
const bytesRead = await file.read(new Uint8Array(0));
@@ -334,18 +339,15 @@ unitTest(
{ perms: { write: false, read: false } },
async function readWritePermFailure(): Promise<void> {
const filename = "tests/hello.txt";
- const writeModes: Deno.OpenMode[] = ["r+", "w+", "a+", "x+"];
- for (const mode of writeModes) {
- let err;
- try {
- await Deno.open(filename, mode);
- } catch (e) {
- err = e;
- }
- assert(!!err);
- assert(err instanceof Deno.errors.PermissionDenied);
- assertEquals(err.name, "PermissionDenied");
+ let err;
+ try {
+ await Deno.open(filename, { read: true });
+ } catch (e) {
+ err = e;
}
+ assert(!!err);
+ assert(err instanceof Deno.errors.PermissionDenied);
+ assertEquals(err.name, "PermissionDenied");
}
);
@@ -377,7 +379,11 @@ unitTest(
const encoder = new TextEncoder();
const filename = tempDir + "hello.txt";
const data = encoder.encode("Hello world!\n");
- let file = await Deno.open(filename, "w");
+ let file = await Deno.open(filename, {
+ create: true,
+ write: true,
+ truncate: true,
+ });
// assert file was created
let fileInfo = Deno.statSync(filename);
assert(fileInfo.isFile);
@@ -398,7 +404,10 @@ unitTest(
}
file.close();
// assert that existing file is truncated on open
- file = await Deno.open(filename, "w");
+ file = await Deno.open(filename, {
+ write: true,
+ truncate: true,
+ });
file.close();
const fileSize = Deno.statSync(filename).size;
assertEquals(fileSize, 0);
@@ -414,7 +423,12 @@ unitTest(
const filename = tempDir + "hello.txt";
const data = encoder.encode("Hello world!\n");
- const file = await Deno.open(filename, "w+");
+ const file = await Deno.open(filename, {
+ write: true,
+ truncate: true,
+ create: true,
+ read: true,
+ });
const seekPosition = 0;
// assert file was created
let fileInfo = Deno.statSync(filename);
diff --git a/cli/js/tests/process_test.ts b/cli/js/tests/process_test.ts
index 47efd86d5..1bcdf26da 100644
--- a/cli/js/tests/process_test.ts
+++ b/cli/js/tests/process_test.ts
@@ -229,7 +229,10 @@ unitTest(
async function runRedirectStdoutStderr(): Promise<void> {
const tempDir = await makeTempDir();
const fileName = tempDir + "/redirected_stdio.txt";
- const file = await open(fileName, "w");
+ const file = await open(fileName, {
+ create: true,
+ write: true,
+ });
const p = run({
cmd: [
@@ -261,7 +264,7 @@ unitTest(
const fileName = tempDir + "/redirected_stdio.txt";
const encoder = new TextEncoder();
await writeFile(fileName, encoder.encode("hello"));
- const file = await open(fileName, "r");
+ const file = await open(fileName);
const p = run({
cmd: ["python", "-c", "import sys; assert 'hello' == sys.stdin.read();"],
diff --git a/cli/js/write_file.ts b/cli/js/write_file.ts
index ed64141d2..af3f67251 100644
--- a/cli/js/write_file.ts
+++ b/cli/js/write_file.ts
@@ -24,8 +24,10 @@ export function writeFileSync(
}
}
- const openMode = !!options.append ? "a" : "w";
- const file = openSync(path, openMode);
+ const openOptions = !!options.append
+ ? { write: true, create: true, append: true }
+ : { write: true, create: true, truncate: true };
+ const file = openSync(path, openOptions);
if (
options.mode !== undefined &&
@@ -52,8 +54,10 @@ export async function writeFile(
}
}
- const openMode = !!options.append ? "a" : "w";
- const file = await open(path, openMode);
+ const openOptions = !!options.append
+ ? { write: true, create: true, append: true }
+ : { write: true, create: true, truncate: true };
+ const file = await open(path, openOptions);
if (
options.mode !== undefined &&