summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorEvgeniy Karagodin <ekaragodin@gmail.com>2019-06-25 23:05:41 +0700
committerRyan Dahl <ry@tinyclouds.org>2019-06-25 09:05:41 -0700
commitd089f9797830a2729cbd45cb4ea6312eb43a28de (patch)
treebf58f68b145696dc59282725b94dbfa8971a0e2c /js
parentc56df45355c8e68eabbfa62021e7ca7484115c0b (diff)
Add homeDir to Deno namespace (#2578)
Diffstat (limited to 'js')
-rw-r--r--js/deno.ts2
-rw-r--r--js/os.ts20
-rw-r--r--js/os_test.ts12
3 files changed, 32 insertions, 2 deletions
diff --git a/js/deno.ts b/js/deno.ts
index 3275d9353..0bc3c95c7 100644
--- a/js/deno.ts
+++ b/js/deno.ts
@@ -1,7 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
// Public deno module.
-export { noColor, pid, env, exit, isTTY, execPath } from "./os";
+export { noColor, pid, env, exit, isTTY, execPath, homeDir } from "./os";
export { chdir, cwd } from "./dir";
export {
File,
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;
+}
diff --git a/js/os_test.ts b/js/os_test.ts
index d3c9e6546..766cd1c3f 100644
--- a/js/os_test.ts
+++ b/js/os_test.ts
@@ -1,5 +1,11 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-import { test, testPerm, assert, assertEquals } from "./test_util.ts";
+import {
+ test,
+ testPerm,
+ assert,
+ assertEquals,
+ assertNotEquals
+} from "./test_util.ts";
testPerm({ env: true }, function envSuccess(): void {
const env = Deno.env();
@@ -32,3 +38,7 @@ test(function osPid(): void {
test(function osIsTTYSmoke(): void {
console.log(Deno.isTTY());
});
+
+test(function homeDir(): void {
+ assertNotEquals(Deno.homeDir(), "");
+});