1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00
denoland-deno/cli/js/ops/os.ts

70 lines
1.3 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 const env = {
get: getEnv,
toObject(): { [key: string]: string } {
return sendSync("op_env");
},
set: setEnv,
};
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");
}