wangzhibo
6 天以前 7bd866831780abcf5a59c4cbb7d1be456eea7c99
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
import { error, readFile, rootDir, writeFile } from "../utils";
import { config } from "../config";
 
function getPath() {
    return rootDir(`.${config.type == "admin" ? "/src" : ""}/config/proxy.ts`);
}
 
export async function updateProxy(data: { name: string }) {
    let code = readFile(getPath());
 
    const regex = /const\s+value\s*=\s*['"]([^'"]+)['"]/;
    if (regex.test(code)) {
        code = code.replace(regex, `const value = '${data.name}'`);
    }
 
    writeFile(getPath(), code);
}
 
export function getProxyTarget(proxy: any) {
    const code = readFile(getPath());
 
    const regex = /const\s+value\s*=\s*['"]([^'"]+)['"]/;
    const match = code.match(regex);
 
    if (match) {
        const value = match[1];
 
        try {
            const { target, rewrite } = proxy[`/${value}/`];
            return target + rewrite(`/${value}`);
        } catch (err) {
            error(`[cool-proxy] Error:${value} → ` + getPath());
            return "";
        }
    }
}