-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.ts
47 lines (43 loc) · 1.47 KB
/
io.ts
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
import { colorNamespace, selectColor } from "./colors.ts";
declare const process: typeof import("node:process");
function deno() {
const encoder = new TextEncoder();
const env = (variable: string) =>
Deno.permissions.querySync?.({ name: "env", variable })?.state === "prompt"
? ""
: Deno.env.get(variable) ?? "";
const useColor = !Deno.noColor && Deno.stderr?.isTerminal() !== false;
const log = (namespace: string, message: string) => {
if (useColor) namespace = colorNamespace(namespace);
if (Deno.stderr) {
Deno.stderr.writeSync(encoder.encode(`${namespace} ${message}\n`));
} else { // Deno Deploy
console.debug(namespace, message);
}
};
const DEBUG = env("DEBUG");
return { DEBUG, log };
}
function node() {
const useColor = !process.env.NO_COLOR && process.stderr.isTTY;
const log = (namespace: string, message: string) => {
if (useColor) namespace = colorNamespace(namespace);
process.stderr.write(`${namespace} ${message}\n`);
};
const { DEBUG = "" } = process.env;
return { DEBUG, log };
}
function cfw() {
const log = (namespace: string, message: string) => {
const color = selectColor(namespace).toString(16);
console.debug("%c%s", `color: #${color}`, namespace, message);
};
// @ts-ignore CFW
const DEBUG: string = globalThis.env?.DEBUG ?? "";
return { DEBUG, log };
}
// deno-fmt-ignore
export const { DEBUG, log } =
"Deno" in globalThis ? deno()
: "process" in globalThis ? node()
: cfw();