1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

Add writeFileSync

This commit is contained in:
Ryan Dahl 2018-05-28 14:39:02 -04:00
parent bce0ecbdb8
commit 46117017c4
6 changed files with 55 additions and 3 deletions

5
deno.d.ts vendored
View file

@ -4,4 +4,9 @@ declare module "deno" {
function pub(channel: string, payload: Uint8Array): null | ArrayBuffer; function pub(channel: string, payload: Uint8Array): null | ArrayBuffer;
function readFileSync(filename: string): Uint8Array; function readFileSync(filename: string): Uint8Array;
function writeFileSync(
filename: string,
data: Uint8Array,
perm: number
): void;
} }

View file

@ -21,6 +21,7 @@ message Msg {
FETCH_RES = 10; FETCH_RES = 10;
READ_FILE_SYNC = 11; READ_FILE_SYNC = 11;
READ_FILE_SYNC_RES = 12; READ_FILE_SYNC_RES = 12;
WRITE_FILE_SYNC = 13;
} }
Command command = 1; Command command = 1;
@ -89,4 +90,10 @@ message Msg {
// READ_FILE_SYNC_RES // READ_FILE_SYNC_RES
bytes read_file_sync_data = 120; 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
View file

@ -34,6 +34,9 @@ func InitOS() {
os.Exit(int(msg.ExitCode)) os.Exit(int(msg.ExitCode))
case Msg_READ_FILE_SYNC: case Msg_READ_FILE_SYNC:
return ReadFileSync(msg.ReadFileSyncFilename) return ReadFileSync(msg.ReadFileSyncFilename)
case Msg_WRITE_FILE_SYNC:
return WriteFileSync(msg.WriteFileSyncFilename, msg.WriteFileSyncData,
msg.WriteFileSyncPerm)
default: default:
panic("[os] Unexpected message " + string(buf)) panic("[os] Unexpected message " + string(buf))
} }
@ -176,3 +179,14 @@ func ReadFileSync(filename string) []byte {
check(err) check(err)
return out 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
View file

@ -48,3 +48,16 @@ export function readFileSync(filename: string): Uint8Array {
}); });
return res.readFileSyncData; 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
});
}

View file

@ -20,7 +20,8 @@ const EOL = "\n";
const deno = { const deno = {
pub, pub,
sub, sub,
readFileSync: os.readFileSync readFileSync: os.readFileSync,
writeFileSync: os.writeFileSync
}; };
// tslint:disable-next-line:no-any // tslint:disable-next-line:no-any

View file

@ -2,7 +2,7 @@
// But it can also be run manually: // But it can also be run manually:
// ./deno tests.ts // ./deno tests.ts
import { test, assert, assertEqual } from "./deno_testing/testing.ts"; import { test, assert, assertEqual } from "./deno_testing/testing.ts";
import { readFileSync } from "deno"; import { readFileSync, writeFileSync } from "deno";
test(async function tests_test() { test(async function tests_test() {
assert(true); assert(true);
@ -21,8 +21,20 @@ test(async function tests_readFileSync() {
assertEqual(pkg.name, "deno"); 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() { 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(); const json = await response.json();
assertEqual(json.name, "deno"); assertEqual(json.name, "deno");
}); });