summaryrefslogtreecommitdiff
path: root/cli/js/ops/os.ts
blob: 30aa6d0d48239ac805b5a5fca293af9aa8bb5c22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 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[] {
  return sendSync("op_loadavg");
}

export function hostname(): string {
  return sendSync("op_hostname");
}

export function osRelease(): string {
  return sendSync("op_os_release");
}

export function exit(code = 0): never {
  sendSync("op_exit", { code });
  throw new Error("Code not reachable");
}

function setEnv(key: string, value: string): void {
  sendSync("op_set_env", { key, value });
}

function getEnv(key: string): string | undefined {
  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 {
    return sendSync("op_get_dir", { kind });
  } catch (error) {
    if (error instanceof errors.PermissionDenied) {
      throw error;
    }
    return null;
  }
}

export function execPath(): string {
  return sendSync("op_exec_path");
}