From d089f9797830a2729cbd45cb4ea6312eb43a28de Mon Sep 17 00:00:00 2001 From: Evgeniy Karagodin Date: Tue, 25 Jun 2019 23:05:41 +0700 Subject: Add homeDir to Deno namespace (#2578) --- js/os.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'js/os.ts') diff --git a/js/os.ts b/js/os.ts index 0af4098c5..558f47efd 100644 --- a/js/os.ts +++ b/js/os.ts @@ -130,3 +130,23 @@ export function start(source?: string): msg.StartRes { return startResMsg; } + +/** + * Returns the current user's home directory. + * Does not require elevated privileges. + */ +export function homeDir(): string { + const builder = flatbuffers.createBuilder(); + const inner = msg.HomeDir.createHomeDir(builder); + const baseRes = sendSync(builder, msg.Any.HomeDir, inner)!; + assert(msg.Any.HomeDirRes === baseRes.innerType()); + const res = new msg.HomeDirRes(); + assert(baseRes.inner(res) != null); + const path = res.path(); + + if (!path) { + throw new Error("Could not get home directory."); + } + + return path; +} -- cgit v1.2.3