1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 23:59:59 -05:00
denoland-deno/cli/js/ops/os.ts

79 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-01-02 15:13:47 -05:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { sendSync } from "./dispatch_json.ts";
import { errors } from "../errors.ts";
export function loadavg(): number[] {
2020-02-25 09:14:27 -05:00
return sendSync("op_loadavg");
}
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
}
export function osRelease(): string {
2020-02-25 09:14:27 -05:00
return sendSync("op_os_release");
}
export function exit(code = 0): never {
2020-02-25 09:14:27 -05:00
sendSync("op_exit", { code });
throw new Error("Code not reachable");
}
function setEnv(key: string, value: string): void {
2020-02-25 09:14:27 -05:00
sendSync("op_set_env", { key, value });
}
function getEnv(key: string): string | undefined {
2020-02-25 09:14:27 -05:00
return sendSync("op_get_env", { key })[0];
}
export function env(): { [index: string]: string };
export function env(key: string): string | undefined;
export function env(
key?: string
): { [index: string]: string } | string | undefined {
if (key) {
return getEnv(key);
}
2020-02-25 09:14:27 -05:00
const env = sendSync("op_env");
return new Proxy(env, {
set(obj, prop: string, value: string): boolean {
setEnv(prop, value);
return Reflect.set(obj, prop, value);
},
});
}
type DirKind =
| "home"
| "cache"
| "config"
| "executable"
| "data"
| "data_local"
| "audio"
| "desktop"
| "document"
| "download"
| "font"
| "picture"
| "public"
| "template"
| "tmp"
| "video";
export function dir(kind: DirKind): string | null {
try {
2020-02-25 09:14:27 -05:00
return sendSync("op_get_dir", { kind });
} catch (error) {
2020-02-24 15:48:35 -05:00
if (error instanceof errors.PermissionDenied) {
throw error;
}
return null;
}
2019-06-25 12:05:41 -04:00
}
export function execPath(): string {
2020-02-25 09:14:27 -05:00
return sendSync("op_exec_path");
}