UNPKG

3.47 kBJavaScriptView Raw
1const inquirer = require("inquirer");
2const allPluginFile = require("../res/plugin.json");
3const allUPesnPluginFile = require("../res/upesnPlugin.json");
4const utils = require("./m_util.js");
5const mtldev = require("mtl-dev-sdk");
6const fse = require('fs-extra');
7const shell = require("shelljs");
8const promptList = [
9 {
10 type: "list",
11 message: "请选择你要添加的插件:",
12 name: "pluginId",
13 choices: allPluginFile,
14 filter: function(val) {
15 return val;
16 }
17 }
18];
19function addPlugin(pluginName) {
20 if (!utils.isMtlProject()) {
21 return;
22 }
23
24 // if (pluginName) {
25 mtldev.serchPlugins({ pluginId: pluginName }, res => {
26 if (res.code == 200) {
27 let plugins = res.data.plugins || [];
28 // utils.consoleLog(`cli : ${JSON.stringify(plugins)}`);
29 selectPlugin(plugins);
30 } else {
31 utils.consoleLog(`cli : ${JSON.stringify(res)}`);
32 }
33 });
34 return;
35 // }
36
37 // var proj = JSON.parse(fse.readFileSync("./project.json").toString());
38 // if(proj.config.sourceType=="default"){
39 // promptList[0].message ="请选择插件模板:upesnPlugin是代表友空间壳插件;mtlPlugin是代表mtl默认插件。",
40 // promptList[0].name = "pluginMode",
41 // promptList[0].choices = ["upesnPlugin", "mtlPlugin"];
42 // inquirer.prompt(promptList).then(answers => {
43 // let sourceType = answers.pluginMode;
44 // utils.consoleLog("pluginMode=="+sourceType);
45 // // 更新project.json 文件的构建来源
46 // proj.config.sourceType= sourceType;
47 // fse.writeFileSync("./project.json", formatJson(proj),{flag:'w',encoding:'utf-8',mode:'0666'});
48 // selectPluginJsonFile(sourceType);
49 // });
50 // }else{
51 // var tempProj = JSON.parse(fse.readFileSync("./project.json").toString());
52
53 // selectPluginJsonFile(tempProj.config.sourceType);
54 // }
55
56}
57
58function selectPluginJsonFile(sourceType) {
59 utils.consoleLog("sourceType=="+sourceType);
60 if(sourceType !="upesnPlugin"){
61 selectPlugin(allPluginFile);
62 }else{
63 selectPlugin(allUPesnPluginFile);
64 }
65}
66
67
68function formatJson(data) {
69 return JSON.stringify(data,null,4);
70}
71
72function selectPlugin(plugins) {
73 utils.consoleLog(`plugins size : ${plugins.length}`);
74 if (!plugins || plugins.length <= 0) return utils.consoleLog("没有可用模板");
75 let _newPlugins = plugins.map(item => {
76 let { pluginId, pluginId: id, userId: owner ,version: version} = item;
77 const newItem = {
78 name: pluginId,
79 value: {
80 id,
81 version ,
82 name: item.name || pluginId,
83 owner,
84 parameters: JSON.parse(item.parameters.toString()) || [],
85 description: item.description || ""
86 }
87 };
88
89 // console.log("new: ", newItem);
90 return newItem;
91 });
92 // console.log("_newPlugins: ", _newPlugins);
93 promptList[0].choices = _newPlugins;
94 promptList[0].message ="请选择你要添加的插件:",
95 promptList[0].name = "pluginId",
96 inquirer.prompt(promptList).then(answers => {
97 let name = answers.pluginId;
98 // utils.consoleLog("---answers.pluginId:"+JSON.stringify(name));
99 // 下载插件的JS文件到工程中 ,做用户调试使用
100 let retJS = mtldev.downloadPluginJS(name,shell.pwd().toString());
101 // utils.consoleLog("--retJS--:"+JSON.stringify(retJS));
102 let ret = mtldev.setMTLPlugin(name);
103 // utils.consoleLog(ret);
104 // utils.consoleLog("操作完成");
105 });
106}
107
108module.exports = {
109 addPlugin
110};