adapter.js 6.7 KB
Newer Older
1 2
const path = require('path');
const _ = require('lodash');
3
const fs = require('fs');
B
baiy 已提交
4 5
// 运行平台适配
let platform = process.env.hasOwnProperty('npm_config_adapter') ? process.env.npm_config_adapter : "";
B
baiy 已提交
6
platform = ['chrome', 'utools', 'edge', 'firefox','web'].includes(platform) ? platform : "web"
B
baiy 已提交
7 8

const IS_CHROME = "chrome" === platform
9
const IS_EDGE = "edge" === platform
B
baiy 已提交
10
const IS_FIREFOX = "firefox" === platform
B
baiy 已提交
11
const IS_UTOOLS = "utools" === platform
12
const IS_CHROMIUM = ['chrome', 'edge'].includes(platform)
B
baiy 已提交
13 14
const IS_WEB = "web" === platform

15 16 17 18 19 20 21 22 23 24 25 26 27 28
const toolConfig = require('../config')
const tools = toolConfig.tool
const utoolsConfig = toolConfig.utools
const featureConfig = toolConfig.feature

const getToolFeatureTitle = (name, features = []) => {
    for (let i = 0; i < features.length; i++) {
        if (features[i]['name'] === name) {
            return features[i].title
        }
    }
    return name
}

29 30 31 32 33
// 删除文件
const removeFile = (filePath) => {
    fs.existsSync(filePath) && fs.unlinkSync(filePath)
}

B
fix  
baiy 已提交
34 35 36 37 38 39
const chromeConfigWrite = {
    remove(){},
    write(){
        if (!IS_CHROME){
            return;
        }
40 41 42 43 44 45 46
        fs.writeFileSync(
            path.join(__dirname, '../../public/manifest.json'),
            fs.readFileSync(path.join(__dirname, "../adapter/chrome/manifest.json")).toString().replace(/##version##/g, process.env.npm_package_version)
        );
    }
}

B
fix  
baiy 已提交
47 48 49 50 51 52
const edgeConfigWrite = {
    remove(){},
    write(){
        if (!IS_EDGE){
            return;
        }
53 54 55 56 57 58 59
        fs.writeFileSync(
            path.join(__dirname, '../../public/manifest.json'),
            fs.readFileSync(path.join(__dirname, "../adapter/edge/manifest.json")).toString().replace(/##version##/g, process.env.npm_package_version)
        );
    }
}

B
fix  
baiy 已提交
60 61 62 63 64 65 66 67 68
const chromiumConfigWrite = {
    remove(){
        removeFile(path.join(__dirname, '../../public/manifest.json'));
        removeFile(path.join(__dirname, '../../public/background.js'));
    },
    write(){
        if (!IS_CHROMIUM){
            return;
        }
B
baiy 已提交
69 70 71 72 73 74 75
        fs.copyFileSync(
            path.join(__dirname, "../adapter/chromium/background.js"),
            path.join(__dirname, '../../public/background.js')
        );
    }
}

B
fix  
baiy 已提交
76 77 78 79 80 81 82 83 84
const firefoxConfigWrite ={
    remove(){
        removeFile(path.join(__dirname, '../../public/manifest.json'));
        removeFile(path.join(__dirname, '../../public/background.js'));
    },
    write(){
        if (!IS_FIREFOX){
            return;
        }
B
baiy 已提交
85 86 87 88 89 90 91 92
        fs.copyFileSync(
            path.join(__dirname, "../adapter/firefox/background.js"),
            path.join(__dirname, '../../public/background.js')
        );
        fs.writeFileSync(
            path.join(__dirname, '../../public/manifest.json'),
            fs.readFileSync(path.join(__dirname, "../adapter/firefox/manifest.json")).toString().replace(/##version##/g, process.env.npm_package_version)
        );
B
baiy 已提交
93 94 95
    }
}

B
fix  
baiy 已提交
96 97 98 99 100 101 102 103 104 105


const utoolsConfigWrite = {
    remove(){
        removeFile(path.join(__dirname, '../../public/plugin.json'));
    },
    write(){
        if (!IS_UTOOLS){
            return;
        }
B
baiy 已提交
106 107 108
        let pluginPath = path.join(__dirname, '../../public/plugin.json');
        fs.readFile(path.join(__dirname, "../adapter/utools/plugin.json"), 'utf8', function (err, files) {
            if (err) return console.log(err);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
            let utoolsToolFeature = {};
            for (let tool of tools) {
                // 初始化数据
                let code = "ctool-" + tool.name;
                let toolFeatures = featureConfig.hasOwnProperty(tool.name) ? featureConfig[tool.name] : []
                if (!utoolsToolFeature.hasOwnProperty(code)) {
                    utoolsToolFeature[code] = {
                        "code": code,
                        "explain": tool.title,
                        "cmds": []
                    }
                    if (toolFeatures.length > 0) {
                        for (let toolFeature of toolFeatures) {
                            let toolFeatureCode = code + '-' + toolFeature['name']
                            utoolsToolFeature[toolFeatureCode] = {
                                "code": toolFeatureCode,
                                "explain": tool.title + ' - ' + toolFeature['title'],
                                "cmds": []
                            }
                        }
                    }
                }

                // 关键字
                let keyword = utoolsConfig['keyword'].hasOwnProperty(tool.name) ? utoolsConfig['keyword'][tool.name] : []
                utoolsToolFeature[code].cmds.push(
                    ...Array.from(new Set([tool.name, tool.title, "ctool-" + tool.name, ...keyword]))
                )

                // cmds手动配置
                let cmds = utoolsConfig['cmds'].hasOwnProperty(tool.name) ? utoolsConfig['cmds'][tool.name] : []
                if (!cmds.length) {
                    continue;
                }

                for (let _cmd of cmds) {
                    let cmd = _.cloneDeep(_cmd);
                    if (!cmd.hasOwnProperty('feature')) {
                        cmd['label'] = tool.title
                        utoolsToolFeature[code].cmds.push(cmd)
                        continue;
                    }
                    let toolFeatureCode = code + '-' + cmd.feature
                    if (utoolsToolFeature.hasOwnProperty(toolFeatureCode)) {
                        cmd['label'] = tool.title + ' - ' + getToolFeatureTitle(cmd.feature, toolFeatures)
                        delete cmd.feature
                        utoolsToolFeature[toolFeatureCode].cmds.push(cmd)
                    }
                }
            }
B
baiy 已提交
159 160 161 162 163 164 165

            let features = [
                {
                    "code": "ctool",
                    "explain": "程序开发常用工具",
                    "cmds": ['ctool', '程序开发常用工具']
                },
166 167 168
                ...Object.values(utoolsToolFeature)
            ];

B
baiy 已提交
169 170 171
            let result = files
                .replace(/##version##/g, process.env.npm_package_version)
                .replace(/"##features##"/g, JSON.stringify(features));
172
            fs.writeFileSync(pluginPath, result);
B
baiy 已提交
173 174 175 176 177 178 179
        });
    }
}

module.exports = {
    platform: platform,
    isChrome: IS_CHROME,
180
    isChromium: IS_CHROMIUM,
B
baiy 已提交
181
    isFirefox: IS_FIREFOX,
182
    isEdge: IS_EDGE,
B
baiy 已提交
183 184 185
    isWeb: IS_WEB,
    isUtools: IS_UTOOLS,
    initialize: function () {
B
fix  
baiy 已提交
186 187 188 189 190 191 192 193 194 195 196 197
        // 移除配置文件
        chromiumConfigWrite.remove();
        chromeConfigWrite.remove();
        edgeConfigWrite.remove();
        firefoxConfigWrite.remove();
        utoolsConfigWrite.remove();
        // 添加配置文件
        chromiumConfigWrite.write();
        chromeConfigWrite.write();
        edgeConfigWrite.write();
        firefoxConfigWrite.write();
        utoolsConfigWrite.write();
B
baiy 已提交
198
    }
B
baiy 已提交
199
}