2019-08-14 11:03:02 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-08-23 01:30:14 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2019-09-15 14:48:25 -04:00
|
|
|
use crate::colors;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::fs as deno_fs;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops::json_op;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
|
|
|
use crate::version;
|
|
|
|
use atty;
|
|
|
|
use deno::*;
|
2019-08-23 01:30:14 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::env;
|
2019-09-27 19:09:42 -04:00
|
|
|
use sys_info;
|
2019-08-14 11:03:02 -04:00
|
|
|
use url::Url;
|
|
|
|
|
2019-09-06 12:57:15 -04:00
|
|
|
/// BUILD_OS and BUILD_ARCH match the values in Deno.build. See js/build.ts.
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
static BUILD_OS: &str = "mac";
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
static BUILD_OS: &str = "linux";
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
static BUILD_OS: &str = "win";
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
static BUILD_ARCH: &str = "x64";
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
|
|
|
i.register_op("exit", s.core_op(json_op(s.stateful_op(op_exit))));
|
|
|
|
i.register_op("is_tty", s.core_op(json_op(s.stateful_op(op_is_tty))));
|
|
|
|
i.register_op("env", s.core_op(json_op(s.stateful_op(op_env))));
|
|
|
|
i.register_op("exec_path", s.core_op(json_op(s.stateful_op(op_exec_path))));
|
|
|
|
i.register_op("set_env", s.core_op(json_op(s.stateful_op(op_set_env))));
|
|
|
|
i.register_op("get_env", s.core_op(json_op(s.stateful_op(op_get_env))));
|
|
|
|
i.register_op("home_dir", s.core_op(json_op(s.stateful_op(op_home_dir))));
|
|
|
|
i.register_op("hostname", s.core_op(json_op(s.stateful_op(op_hostname))));
|
|
|
|
i.register_op("start", s.core_op(json_op(s.stateful_op(op_start))));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_start(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
_args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
2019-11-04 10:38:52 -05:00
|
|
|
let gs = &state.global_state;
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
Ok(JsonOp::Sync(json!({
|
|
|
|
"cwd": deno_fs::normalize_path(&env::current_dir().unwrap()),
|
|
|
|
"pid": std::process::id(),
|
2019-11-04 10:38:52 -05:00
|
|
|
"argv": gs.argv,
|
|
|
|
"mainModule": gs.main_module.as_ref().map(|x| x.to_string()),
|
|
|
|
"debugFlag": gs.flags.log_level.map_or(false, |l| l == log::Level::Debug),
|
|
|
|
"versionFlag": gs.flags.version,
|
2019-08-26 08:50:21 -04:00
|
|
|
"v8Version": version::v8(),
|
|
|
|
"denoVersion": version::DENO,
|
2019-09-15 18:36:27 -04:00
|
|
|
"tsVersion": version::TYPESCRIPT,
|
2019-09-15 14:48:25 -04:00
|
|
|
"noColor": !colors::use_color(),
|
2019-09-06 12:57:15 -04:00
|
|
|
"os": BUILD_OS,
|
|
|
|
"arch": BUILD_ARCH,
|
2019-08-26 08:50:21 -04:00
|
|
|
})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_home_dir(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
_args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
2019-08-14 11:03:02 -04:00
|
|
|
state.check_env()?;
|
|
|
|
let path = dirs::home_dir()
|
|
|
|
.unwrap_or_default()
|
|
|
|
.into_os_string()
|
|
|
|
.into_string()
|
|
|
|
.unwrap_or_default();
|
2019-08-26 08:50:21 -04:00
|
|
|
Ok(JsonOp::Sync(json!(path)))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_exec_path(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-23 01:30:14 -04:00
|
|
|
_args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
2019-08-14 11:03:02 -04:00
|
|
|
state.check_env()?;
|
2019-08-23 01:30:14 -04:00
|
|
|
let current_exe = env::current_exe().unwrap();
|
|
|
|
// Now apply URL parser to current exe to get fully resolved path, otherwise
|
|
|
|
// we might get `./` and `../` bits in `exec_path`
|
2019-08-14 11:03:02 -04:00
|
|
|
let exe_url = Url::from_file_path(current_exe).unwrap();
|
2019-08-23 01:30:14 -04:00
|
|
|
let path = exe_url.to_file_path().unwrap();
|
|
|
|
Ok(JsonOp::Sync(json!(path)))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SetEnv {
|
|
|
|
key: String,
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_set_env(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: SetEnv = serde_json::from_value(args)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
state.check_env()?;
|
2019-08-26 08:50:21 -04:00
|
|
|
env::set_var(args.key, args.value);
|
|
|
|
Ok(JsonOp::Sync(json!({})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_env(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-23 01:30:14 -04:00
|
|
|
_args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
2019-08-14 11:03:02 -04:00
|
|
|
state.check_env()?;
|
2019-08-23 01:30:14 -04:00
|
|
|
let v = env::vars().collect::<HashMap<String, String>>();
|
|
|
|
Ok(JsonOp::Sync(json!(v)))
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-10-02 11:55:28 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct GetEnv {
|
|
|
|
key: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_get_env(
|
2019-10-02 11:55:28 -04:00
|
|
|
state: &ThreadSafeState,
|
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: GetEnv = serde_json::from_value(args)?;
|
|
|
|
state.check_env()?;
|
|
|
|
let r = match env::var(args.key) {
|
|
|
|
Err(env::VarError::NotPresent) => json!([]),
|
|
|
|
v => json!([v?]),
|
|
|
|
};
|
|
|
|
Ok(JsonOp::Sync(r))
|
|
|
|
}
|
|
|
|
|
2019-08-23 01:30:14 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Exit {
|
|
|
|
code: i32,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_exit(
|
2019-08-23 01:30:14 -04:00
|
|
|
_s: &ThreadSafeState,
|
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: Exit = serde_json::from_value(args)?;
|
|
|
|
std::process::exit(args.code)
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_is_tty(
|
2019-08-23 01:30:14 -04:00
|
|
|
_s: &ThreadSafeState,
|
|
|
|
_args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
Ok(JsonOp::Sync(json!({
|
|
|
|
"stdin": atty::is(atty::Stream::Stdin),
|
|
|
|
"stdout": atty::is(atty::Stream::Stdout),
|
|
|
|
"stderr": atty::is(atty::Stream::Stderr),
|
|
|
|
})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2019-09-27 19:09:42 -04:00
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_hostname(
|
2019-09-27 19:09:42 -04:00
|
|
|
state: &ThreadSafeState,
|
|
|
|
_args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
state.check_env()?;
|
|
|
|
let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_owned());
|
|
|
|
Ok(JsonOp::Sync(json!(hostname)))
|
|
|
|
}
|