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
37
38
39
40
41
import { formatContent, readFile, rootDir, writeFile } from "../utils";
 
function getPlugin(name: string) {
    let code = readFile(rootDir(`./src/plugins/${name}/config.ts`));
 
    // 设置插件配置
    const set = (key: string, value: any) => {
        const regex = new RegExp(`(return\\s*{[^}]*?\\b${key}\\b\\s*:\\s*)([^,}]+)`);
        if (regex.test(code)) {
            code = code.replace(regex, `$1${JSON.stringify(value)}`);
        } else {
            const insertPos = code.indexOf("return {") + 8;
            code =
                code.slice(0, insertPos) +
                `\n  ${key}: ${JSON.stringify(value)},` +
                code.slice(insertPos);
        }
    };
 
    // 保存插件配置
    const save = async () => {
        const content = await formatContent(code);
        writeFile(rootDir(`./src/plugins/${name}/config.ts`), content);
    };
 
    return {
        set,
        save,
    };
}
 
// 修改插件
export async function updatePlugin(options: { name: string; enable: boolean }) {
    const plugin = getPlugin(options.name);
 
    if (options.enable !== undefined) {
        plugin.set("enable", options.enable);
    }
 
    await plugin.save();
}