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