mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
Add writeFileSync
This commit is contained in:
parent
bce0ecbdb8
commit
46117017c4
6 changed files with 55 additions and 3 deletions
5
deno.d.ts
vendored
5
deno.d.ts
vendored
|
@ -4,4 +4,9 @@ declare module "deno" {
|
|||
function pub(channel: string, payload: Uint8Array): null | ArrayBuffer;
|
||||
|
||||
function readFileSync(filename: string): Uint8Array;
|
||||
function writeFileSync(
|
||||
filename: string,
|
||||
data: Uint8Array,
|
||||
perm: number
|
||||
): void;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ message Msg {
|
|||
FETCH_RES = 10;
|
||||
READ_FILE_SYNC = 11;
|
||||
READ_FILE_SYNC_RES = 12;
|
||||
WRITE_FILE_SYNC = 13;
|
||||
}
|
||||
Command command = 1;
|
||||
|
||||
|
@ -89,4 +90,10 @@ message Msg {
|
|||
|
||||
// READ_FILE_SYNC_RES
|
||||
bytes read_file_sync_data = 120;
|
||||
|
||||
// WRITE_FILE_SYNC
|
||||
string write_file_sync_filename = 130;
|
||||
bytes write_file_sync_data = 131;
|
||||
uint32 write_file_sync_perm = 132;
|
||||
// write_file_sync_perm specified by https://godoc.org/os#FileMode
|
||||
}
|
||||
|
|
14
os.go
14
os.go
|
@ -34,6 +34,9 @@ func InitOS() {
|
|||
os.Exit(int(msg.ExitCode))
|
||||
case Msg_READ_FILE_SYNC:
|
||||
return ReadFileSync(msg.ReadFileSyncFilename)
|
||||
case Msg_WRITE_FILE_SYNC:
|
||||
return WriteFileSync(msg.WriteFileSyncFilename, msg.WriteFileSyncData,
|
||||
msg.WriteFileSyncPerm)
|
||||
default:
|
||||
panic("[os] Unexpected message " + string(buf))
|
||||
}
|
||||
|
@ -176,3 +179,14 @@ func ReadFileSync(filename string) []byte {
|
|||
check(err)
|
||||
return out
|
||||
}
|
||||
|
||||
func WriteFileSync(filename string, data []byte, perm uint32) []byte {
|
||||
err := afero.WriteFile(fs, filename, data, os.FileMode(perm))
|
||||
res := &Msg{}
|
||||
if err != nil {
|
||||
res.Error = err.Error()
|
||||
}
|
||||
out, err := proto.Marshal(res)
|
||||
check(err)
|
||||
return out
|
||||
}
|
||||
|
|
13
os.ts
13
os.ts
|
@ -48,3 +48,16 @@ export function readFileSync(filename: string): Uint8Array {
|
|||
});
|
||||
return res.readFileSyncData;
|
||||
}
|
||||
|
||||
export function writeFileSync(
|
||||
filename: string,
|
||||
data: Uint8Array,
|
||||
perm: number
|
||||
): void {
|
||||
sendMsg("os", {
|
||||
command: pb.Msg.Command.WRITE_FILE_SYNC,
|
||||
writeFileSyncFilename: filename,
|
||||
writeFileSyncData: data,
|
||||
writeFileSyncPerm: perm
|
||||
});
|
||||
}
|
||||
|
|
|
@ -20,7 +20,8 @@ const EOL = "\n";
|
|||
const deno = {
|
||||
pub,
|
||||
sub,
|
||||
readFileSync: os.readFileSync
|
||||
readFileSync: os.readFileSync,
|
||||
writeFileSync: os.writeFileSync
|
||||
};
|
||||
|
||||
// tslint:disable-next-line:no-any
|
||||
|
|
16
tests.ts
16
tests.ts
|
@ -2,7 +2,7 @@
|
|||
// But it can also be run manually:
|
||||
// ./deno tests.ts
|
||||
import { test, assert, assertEqual } from "./deno_testing/testing.ts";
|
||||
import { readFileSync } from "deno";
|
||||
import { readFileSync, writeFileSync } from "deno";
|
||||
|
||||
test(async function tests_test() {
|
||||
assert(true);
|
||||
|
@ -21,8 +21,20 @@ test(async function tests_readFileSync() {
|
|||
assertEqual(pkg.name, "deno");
|
||||
});
|
||||
|
||||
test(async function tests_writeFileSync() {
|
||||
const enc = new TextEncoder();
|
||||
const data = enc.encode("Hello");
|
||||
// TODO need ability to get tmp dir.
|
||||
const fn = "/tmp/test.txt";
|
||||
writeFileSync("/tmp/test.txt", data, 0o666);
|
||||
const dataRead = readFileSync("/tmp/test.txt");
|
||||
const dec = new TextDecoder("utf-8");
|
||||
const actual = dec.decode(dataRead);
|
||||
assertEqual("Hello", actual);
|
||||
});
|
||||
|
||||
test(async function tests_fetch() {
|
||||
const response = await fetch('http://localhost:4545/package.json');
|
||||
const response = await fetch("http://localhost:4545/package.json");
|
||||
const json = await response.json();
|
||||
assertEqual(json.name, "deno");
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue