2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-09-02 17:07:11 -04:00
|
|
|
import { sendSync } from "./dispatch_json.ts";
|
2020-03-08 13:09:22 +01:00
|
|
|
import { errors } from "../errors.ts";
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2020-02-23 00:46:52 +01:00
|
|
|
export function loadavg(): number[] {
|
2020-02-25 09:14:27 -05:00
|
|
|
return sendSync("op_loadavg");
|
2020-02-23 00:46:52 +01:00
|
|
|
}
|
2019-02-03 06:05:30 +03:00
|
|
|
|
2019-09-27 16:09:42 -07:00
|
|
|
export function hostname(): string {
|
2020-02-25 09:14:27 -05:00
|
|
|
return sendSync("op_hostname");
|
2019-09-27 16:09:42 -07:00
|
|
|
}
|
|
|
|
|
2020-02-24 14:35:45 +01:00
|
|
|
export function osRelease(): string {
|
2020-02-25 09:14:27 -05:00
|
|
|
return sendSync("op_os_release");
|
2020-02-24 14:35:45 +01:00
|
|
|
}
|
|
|
|
|
2019-08-22 22:30:14 -07:00
|
|
|
export function exit(code = 0): never {
|
2020-02-25 09:14:27 -05:00
|
|
|
sendSync("op_exit", { code });
|
2020-03-11 15:49:53 +01:00
|
|
|
throw new Error("Code not reachable");
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
|
2019-03-10 04:30:38 +11:00
|
|
|
function setEnv(key: string, value: string): void {
|
2020-02-25 09:14:27 -05:00
|
|
|
sendSync("op_set_env", { key, value });
|
2019-03-10 04:30:38 +11:00
|
|
|
}
|
|
|
|
|
2019-10-02 11:55:28 -04:00
|
|
|
function getEnv(key: string): string | undefined {
|
2020-02-25 09:14:27 -05:00
|
|
|
return sendSync("op_get_env", { key })[0];
|
2019-10-02 11:55:28 -04:00
|
|
|
}
|
|
|
|
|
2020-04-29 20:48:19 +02:00
|
|
|
export const env = {
|
|
|
|
get: getEnv,
|
|
|
|
toObject(): { [key: string]: string } {
|
|
|
|
return sendSync("op_env");
|
|
|
|
},
|
|
|
|
set: setEnv,
|
|
|
|
};
|
2019-01-29 11:41:28 +10:00
|
|
|
|
2019-12-18 09:29:00 -05:00
|
|
|
type DirKind =
|
|
|
|
| "home"
|
|
|
|
| "cache"
|
|
|
|
| "config"
|
2019-12-21 11:30:13 +00:00
|
|
|
| "executable"
|
2019-12-18 09:29:00 -05:00
|
|
|
| "data"
|
|
|
|
| "data_local"
|
|
|
|
| "audio"
|
|
|
|
| "desktop"
|
|
|
|
| "document"
|
|
|
|
| "download"
|
|
|
|
| "font"
|
|
|
|
| "picture"
|
|
|
|
| "public"
|
|
|
|
| "template"
|
2020-03-02 01:05:04 +01:00
|
|
|
| "tmp"
|
2019-12-18 09:29:00 -05:00
|
|
|
| "video";
|
2019-12-15 13:14:20 +08:00
|
|
|
|
2019-12-21 00:06:07 +00:00
|
|
|
export function dir(kind: DirKind): string | null {
|
|
|
|
try {
|
2020-02-25 09:14:27 -05:00
|
|
|
return sendSync("op_get_dir", { kind });
|
2019-12-21 00:06:07 +00:00
|
|
|
} catch (error) {
|
2020-02-24 15:48:35 -05:00
|
|
|
if (error instanceof errors.PermissionDenied) {
|
2019-12-21 00:06:07 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2019-06-25 23:05:41 +07:00
|
|
|
}
|
2019-08-06 14:05:47 -07:00
|
|
|
|
|
|
|
export function execPath(): string {
|
2020-02-25 09:14:27 -05:00
|
|
|
return sendSync("op_exec_path");
|
2019-08-06 14:05:47 -07:00
|
|
|
}
|