This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from zsqk/deno-fn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
117 lines (106 loc) · 2.88 KB
/
mod.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 依赖 Deno 的常用函数
import { readAll } from 'https://deno.land/[email protected]/streams/conversion.ts';
import { parseEnv } from './js/parse-env.ts';
import { getRunData } from './deno/run.ts';
/**
* [Deno] 解析 JSON 格式的 Deno.Reader 为 JS 对象
* @deprecated 因为 Deno 支持 Request 所以不再需要该功能.
* @param reqBody
* @returns
*/
export async function readBody(reqBody?: Deno.Reader): Promise<unknown> {
if (reqBody === undefined) {
return {};
}
const buf: Uint8Array = await readAll(reqBody);
const str = new TextDecoder('utf-8').decode(buf);
return JSON.parse(str);
}
/**
* [Deno] Load Environment Variables
* @param path environment file
*/
export function loadENV(
path = './.env',
): [Error | null, Record<string, string>] {
let error: Error | null = null;
const res: Record<string, string> = {};
try {
Deno.readTextFileSync(path)
.split('\n')
.forEach((v) => {
const i = v.indexOf('=');
if (i === -1) {
return;
}
const k = v.slice(0, i);
const val = v.slice(i + 1);
res[k] = val;
Deno.env.set(k, val);
});
} catch (err) {
error = err;
}
return [error, res];
}
export { getRunData };
type ComputeInfo = {
hostname: string;
os: string;
version: string;
};
/**
* [Deno] 获取计算机信息
*/
export async function getComputeInfo(): Promise<ComputeInfo> {
let hostname = 'unknown';
let os: string = Deno.build.os;
let version = '';
// get hostname
if (Deno.build.os === 'darwin' || Deno.build.os === 'linux') {
const p = new Deno.Command('hostname');
const s = await p.output();
hostname = new TextDecoder().decode(s.stdout).replace('\n', '');
}
// get os
if (os === 'darwin') {
os = 'mac';
}
// get version
if (Deno.build.os === 'darwin') {
const p = new Deno.Command('sw_vers', {
args: ['-productVersion'],
});
version = new TextDecoder().decode(p.outputSync().stdout).replace('\n', '');
}
if (Deno.build.os === 'linux') {
const p = new Deno.Command('cat', {
args: ['/etc/os-release'],
});
// cat /etc/os-release demo
// PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
// NAME="Debian GNU/Linux"
// VERSION_ID="9"
// VERSION="9 (stretch)"
// ID=debian
// HOME_URL="https://www.debian.org/"
// SUPPORT_URL="https://www.debian.org/support"
// BUG_REPORT_URL="https://bugs.debian.org/"
const osRelease = parseEnv(new TextDecoder().decode(p.outputSync().stdout));
version = (osRelease.get('ID') ?? 'linux') +
(osRelease.get('VERSION_ID') ?? '');
}
return { hostname, os, version };
}
/**
* [Deno] 根据计算机信息自动生成名称
*/
export async function getComputeKey(): Promise<string> {
const { hostname, os } = await getComputeInfo();
return ''.concat(
os,
hostname,
'-',
Deno.env.get('USER') ?? 'unknown',
);
}