Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
71 lines (65 loc) · 1.92 KB
/
Copy pathutils.ts
File metadata and controls
71 lines (65 loc) · 1.92 KB
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
/* eslint-disable @typescript-eslint/ban-ts-comment */
import process from 'node:process'
import path from 'node:path'
import { chmod, readdir, stat } from 'node:fs/promises'
import { execSync } from 'node:child_process'
import { createConsola } from 'consola'
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const sudo = require('sudo-prompt')
export const consola = createConsola({
level: 3,
}).withTag('reverse-proxy')
export async function chmodRecursive(_path: string, mode: number) {
await chmod(_path, mode)
if ((await stat(_path)).isDirectory()) {
const children = await readdir(_path)
for (const child of children)
await chmodRecursive(path.join(_path, child), mode)
}
}
export function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T> {
let called = false
return (...args: any[]) => {
if (called)
return
called = true
return fn(...args)
}
}
export function isAdmin() {
if (process.platform === 'win32') {
try {
execSync('net session', { stdio: 'ignore' })
return true
}
catch {
return false
}
}
else {
return process.getuid && process.getuid() === 0
}
}
export function runAsAdmin(command: string, name?: string, fn?: (error: any, stdout: any, stderr: any) => void) {
return new Promise<boolean>((resolve, reject) => {
consola.log('exec command: ', command)
sudo.exec(command, {
name: `unpluginHttpsReverseProxy ${name}`,
}, (error: any, stdout: any, stderr: any) => {
try {
fn?.(error, stdout, stderr)
}
finally {
if (error) {
consola.error(error)
// eslint-disable-next-line prefer-promise-reject-errors
reject(false)
// eslint-disable-next-line no-unsafe-finally
return
}
consola.success(stdout)
resolve(true)
}
})
})
}