mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 08:33:43 -05:00
feat: Deno.create (#3629)
This commit is contained in:
parent
2d5457df15
commit
b71d5708c6
4 changed files with 33 additions and 9 deletions
|
@ -7,6 +7,8 @@ export {
|
|||
File,
|
||||
open,
|
||||
openSync,
|
||||
create,
|
||||
createSync,
|
||||
stdin,
|
||||
stdout,
|
||||
stderr,
|
||||
|
|
|
@ -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+");
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
12
cli/js/lib.deno_runtime.d.ts
vendored
12
cli/js/lib.deno_runtime.d.ts
vendored
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue