Skip to content

Commit

Permalink
Add userHomeDir to Deno namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ekaragodin committed Jun 25, 2019
1 parent d1482c6 commit 1e7df87
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
8 changes: 8 additions & 0 deletions cli/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ union Any {
StatRes,
Symlink,
Truncate,
UserHomeDir,
UserHomeDirRes,
Utime,
WorkerGetMessage,
WorkerGetMessageRes,
Expand Down Expand Up @@ -446,6 +448,12 @@ table Truncate {
len: uint;
}

table UserHomeDir {}

table UserHomeDirRes {
path: string;
}

table Utime {
filename: string;
atime: uint64;
Expand Down
30 changes: 30 additions & 0 deletions cli/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
msg::Any::Stat => Some(op_stat),
msg::Any::Symlink => Some(op_symlink),
msg::Any::Truncate => Some(op_truncate),
msg::Any::UserHomeDir => Some(op_user_home_dir),
msg::Any::Utime => Some(op_utime),
msg::Any::Write => Some(op_write),

Expand Down Expand Up @@ -1718,6 +1719,35 @@ fn op_metrics(
))
}

fn op_user_home_dir(
_state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
assert!(data.is_none());
let cmd_id = base.cmd_id();

let builder = &mut FlatBufferBuilder::new();
let path = dirs::home_dir()
.unwrap_or_default()
.into_os_string()
.into_string()
.unwrap_or_default();
let path = Some(builder.create_string(&path));
let inner =
msg::UserHomeDirRes::create(builder, &msg::UserHomeDirResArgs { path });

ok_buf(serialize_response(
cmd_id,
builder,
msg::BaseArgs {
inner: Some(inner.as_union_value()),
inner_type: msg::Any::UserHomeDirRes,
..Default::default()
},
))
}

fn op_resources(
_state: &ThreadSafeState,
base: &msg::Base<'_>,
Expand Down
2 changes: 1 addition & 1 deletion js/deno.ts
Original file line number Diff line number Diff line change
@@ -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, userHomeDir } from "./os";
export { chdir, cwd } from "./dir";
export {
File,
Expand Down
19 changes: 19 additions & 0 deletions js/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,22 @@ export function start(source?: string): msg.StartRes {

return startResMsg;
}

/**
* Returns the current user's home directory.
*/
export function userHomeDir(): string {
const builder = flatbuffers.createBuilder();
const inner = msg.UserHomeDir.createUserHomeDir(builder);
const baseRes = sendSync(builder, msg.Any.UserHomeDir, inner)!;
assert(msg.Any.UserHomeDirRes === baseRes.innerType());
const res = new msg.UserHomeDirRes();
assert(baseRes.inner(res) != null);
const path = res.path();

if (!path) {
throw new Error("Could not get home directory.");
}

return path;
}
12 changes: 11 additions & 1 deletion js/os_test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -32,3 +38,7 @@ test(function osPid(): void {
test(function osIsTTYSmoke(): void {
console.log(Deno.isTTY());
});

test(function userHomeDir(): void {
assertNotEquals(Deno.userHomeDir(), "");
});

0 comments on commit 1e7df87

Please sign in to comment.