generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
58 lines (48 loc) · 1.66 KB
/
utils.js
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
import * as EXTS from '@shgysk8zer0/consts/exts.js';
import * as MIMES from '@shgysk8zer0/consts/mimes.js';
import * as STATUS from '@shgysk8zer0/consts/status.js';
import * as STATUS_TEXT from '@shgysk8zer0/consts/status-text.js';
import { extname } from 'node:path';
export async function openLink(url, base) {
if (typeof url === 'string') {
return openLink(new URL(url, base));
} else if (! (url instanceof URL)) {
throw new TypeError('url must be a string or URL object.');
} else if (typeof window !== 'undefined') { // browser
globalThis.window.open(url);
} else if (typeof process !== 'undefined') { // node
const { exec } = await import('node:child_process');
await new Promise((resolve, reject) => {
switch (process.platform) {
case 'linux':
case 'freebsd':
case 'openbsd':
case 'netbsd':
exec(`xdg-open "${url.href}"`, err => err instanceof Error ? reject(err) : resolve());
break;
case 'darwin':
exec(`open "${url.href}"`, err => err instanceof Error ? reject(err) : resolve());
break;
case 'win32':
exec(`start "" "${url.href}"`, err => err instanceof Error ? reject(err) : resolve());
break;
default:
throw new Error(`Unknown platform: ${process.platform}.`);
}
});
}
}
export function getStatusText(code) {
const [key] = Object.entries(STATUS).find(([,stat]) => stat === code) ?? [];
return STATUS_TEXT[key] ?? '';
}
export function getMimeType(path) {
const ext = extname(path).toLowerCase();
console.log({ path, ext });
if (ext.length !== 0) {
const [key] = Object.entries(EXTS).find(([,exts]) => exts.includes(ext)) ?? [];
return MIMES[key] ?? '';
} else {
return '';
}
}