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

const IS_CHROME = "chrome" === platform
const IS_UTOOLS = "utools" === platform
const IS_WEB = "web" === platform

11 12 13 14 15 16 17 18 19 20 21 22 23 24
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
}

B
baiy 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
const chromeConfigWrite = () => {
    let fs = require('fs');
    // 移除环境配置文件
    let manifestPath = path.join(__dirname, '../../public/manifest.json');
    fs.unlink(manifestPath, () => {
    });
    if (IS_CHROME) {
        fs.readFile(path.join(__dirname, "../adapter/chrome/manifest.json"), 'utf8', function (err, files) {
            if (err) return console.log(err);
            let result = files.replace(/##version##/g, process.env.npm_package_version);
            fs.writeFile(manifestPath, result, 'utf8', function (err) {
                if (err) return console.log(err);
            });
B
i18n  
baiy 已提交
38
        });
B
baiy 已提交
39 40 41
    }
}

B
baiy 已提交
42 43 44 45 46 47 48 49 50 51
const utoolsConfigWrite = () => {
    let fs = require('fs');
    // 移除环境配置文件
    let fileArr = ['plugin.json', 'README.md']
    fileArr.forEach((file) => {
        let filePath = path.join(__dirname, '../../public/' + file);
        fs.unlink(filePath, () => {
        });
    })
    if (IS_UTOOLS) {
B
baiy 已提交
52 53 54
        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);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
            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 已提交
106 107 108 109 110 111 112

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

B
baiy 已提交
116 117 118
            let result = files
                .replace(/##version##/g, process.env.npm_package_version)
                .replace(/"##features##"/g, JSON.stringify(features));
B
baiy 已提交
119 120 121 122 123 124 125
            fs.writeFile(pluginPath, result, 'utf8', function (err) {
                if (err) return console.log(err);
            });
        });
        let readmePath = path.join(__dirname, '../../public/README.md');
        fs.copyFile(path.join(__dirname, "../../README.md"), readmePath, function (err) {
            if (err) return console.log(err);
B
baiy 已提交
126 127 128 129 130 131 132 133 134 135
        });
    }
}

module.exports = {
    platform: platform,
    isChrome: IS_CHROME,
    isWeb: IS_WEB,
    isUtools: IS_UTOOLS,
    initialize: function () {
B
baiy 已提交
136 137
        chromeConfigWrite();
        utoolsConfigWrite();
B
baiy 已提交
138
    }
B
baiy 已提交
139
}