diff options
author | Luca Casonato <lucacasonato@yahoo.com> | 2020-01-08 23:07:03 +0100 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2020-01-08 17:07:03 -0500 |
commit | b71d5708c603b09714a1f539f92f82392b6ee33d (patch) | |
tree | f1bf22ba6c7c72b2468d23ae7d69e4cb31c812af /cli/js | |
parent | 2d5457df15d8c4a81362bb2d185b5c6013faa1d8 (diff) |
feat: Deno.create (#3629)
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/deno.ts | 2 | ||||
-rw-r--r-- | cli/js/files.ts | 26 | ||||
-rw-r--r-- | cli/js/files_test.ts | 2 | ||||
-rw-r--r-- | cli/js/lib.deno_runtime.d.ts | 12 |
4 files changed, 33 insertions, 9 deletions
diff --git a/cli/js/deno.ts b/cli/js/deno.ts index 9bc3bec9b..ed100ef22 100644 --- a/cli/js/deno.ts +++ b/cli/js/deno.ts @@ -7,6 +7,8 @@ export { File, open, openSync, + create, + createSync, stdin, stdout, stderr, diff --git a/cli/js/files.ts b/cli/js/files.ts index 7f68341bd..1d04c0e70 100644 --- a/cli/js/files.ts +++ b/cli/js/files.ts @@ -39,6 +39,24 @@ export async function open( return new File(rid); } +/** Creates a file if none exists or truncates an existing file and returns + * an instance of the `File` object synchronously. + * + * const file = Deno.createSync("/foo/bar.txt"); + */ +export function createSync(filename: string): File { + return openSync(filename, "w+"); +} + +/** Creates a file if none exists or truncates an existing file and returns + * an instance of the `File` object. + * + * const file = await Deno.create("/foo/bar.txt"); + */ +export function create(filename: string): Promise<File> { + return open(filename, "w+"); +} + /** Read synchronously from a file ID into an array buffer. * * Return `number | EOF` for the operation. @@ -223,11 +241,3 @@ export type OpenMode = | "x" /** Read-write. Behaves like `x` and allows to read from file. */ | "x+"; - -/** A factory function for creating instances of `File` associated with the - * supplied file name. - * @internal - */ -export function create(filename: string): Promise<File> { - return open(filename, "w+"); -} diff --git a/cli/js/files_test.ts b/cli/js/files_test.ts index 824803588..a4881296a 100644 --- a/cli/js/files_test.ts +++ b/cli/js/files_test.ts @@ -161,7 +161,7 @@ testPerm({ read: true, write: true }, async function createFile(): Promise< > { const tempDir = await Deno.makeTempDir(); const filename = tempDir + "/test.txt"; - const f = await Deno.open(filename, "w"); + const f = await Deno.create(filename); let fileInfo = Deno.statSync(filename); assert(fileInfo.isFile()); assert(fileInfo.len === 0); diff --git a/cli/js/lib.deno_runtime.d.ts b/cli/js/lib.deno_runtime.d.ts index afecc9b75..f65c9650d 100644 --- a/cli/js/lib.deno_runtime.d.ts +++ b/cli/js/lib.deno_runtime.d.ts @@ -306,6 +306,18 @@ declare namespace Deno { * })(); */ export function open(filename: string, mode?: OpenMode): Promise<File>; + /** Creates a file if none exists or truncates an existing file and returns + * an instance of the `File` object synchronously. + * + * const file = Deno.createSync("/foo/bar.txt"); + */ + export function createSync(filename: string): File; + /** Creates a file if none exists or truncates an existing file and returns + * an instance of the `File` object. + * + * const file = await Deno.create("/foo/bar.txt"); + */ + export function create(filename: string): Promise<File>; /** Read synchronously from a file ID into an array buffer. * * Return `number | EOF` for the operation. |