mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
Add homeDir to Deno namespace (#2578)
This commit is contained in:
parent
c56df45355
commit
d089f97978
5 changed files with 69 additions and 2 deletions
|
@ -74,6 +74,8 @@ union Any {
|
||||||
StatRes,
|
StatRes,
|
||||||
Symlink,
|
Symlink,
|
||||||
Truncate,
|
Truncate,
|
||||||
|
HomeDir,
|
||||||
|
HomeDirRes,
|
||||||
Utime,
|
Utime,
|
||||||
WorkerGetMessage,
|
WorkerGetMessage,
|
||||||
WorkerGetMessageRes,
|
WorkerGetMessageRes,
|
||||||
|
@ -446,6 +448,12 @@ table Truncate {
|
||||||
len: uint;
|
len: uint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
table HomeDir {}
|
||||||
|
|
||||||
|
table HomeDirRes {
|
||||||
|
path: string;
|
||||||
|
}
|
||||||
|
|
||||||
table Utime {
|
table Utime {
|
||||||
filename: string;
|
filename: string;
|
||||||
atime: uint64;
|
atime: uint64;
|
||||||
|
|
29
cli/ops.rs
29
cli/ops.rs
|
@ -241,6 +241,7 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
|
||||||
msg::Any::Stat => Some(op_stat),
|
msg::Any::Stat => Some(op_stat),
|
||||||
msg::Any::Symlink => Some(op_symlink),
|
msg::Any::Symlink => Some(op_symlink),
|
||||||
msg::Any::Truncate => Some(op_truncate),
|
msg::Any::Truncate => Some(op_truncate),
|
||||||
|
msg::Any::HomeDir => Some(op_home_dir),
|
||||||
msg::Any::Utime => Some(op_utime),
|
msg::Any::Utime => Some(op_utime),
|
||||||
msg::Any::Write => Some(op_write),
|
msg::Any::Write => Some(op_write),
|
||||||
|
|
||||||
|
@ -1718,6 +1719,34 @@ fn op_metrics(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn op_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::HomeDirRes::create(builder, &msg::HomeDirResArgs { path });
|
||||||
|
|
||||||
|
ok_buf(serialize_response(
|
||||||
|
cmd_id,
|
||||||
|
builder,
|
||||||
|
msg::BaseArgs {
|
||||||
|
inner: Some(inner.as_union_value()),
|
||||||
|
inner_type: msg::Any::HomeDirRes,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
fn op_resources(
|
fn op_resources(
|
||||||
_state: &ThreadSafeState,
|
_state: &ThreadSafeState,
|
||||||
base: &msg::Base<'_>,
|
base: &msg::Base<'_>,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
// Public deno module.
|
// 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 { chdir, cwd } from "./dir";
|
||||||
export {
|
export {
|
||||||
File,
|
File,
|
||||||
|
|
20
js/os.ts
20
js/os.ts
|
@ -130,3 +130,23 @@ export function start(source?: string): msg.StartRes {
|
||||||
|
|
||||||
return startResMsg;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// 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 {
|
testPerm({ env: true }, function envSuccess(): void {
|
||||||
const env = Deno.env();
|
const env = Deno.env();
|
||||||
|
@ -32,3 +38,7 @@ test(function osPid(): void {
|
||||||
test(function osIsTTYSmoke(): void {
|
test(function osIsTTYSmoke(): void {
|
||||||
console.log(Deno.isTTY());
|
console.log(Deno.isTTY());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test(function homeDir(): void {
|
||||||
|
assertNotEquals(Deno.homeDir(), "");
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue