1 | const inquirer = require("inquirer");
|
2 | const allPluginFile = require("../res/plugin.json");
|
3 | const allUPesnPluginFile = require("../res/upesnPlugin.json");
|
4 | const utils = require("./m_util.js");
|
5 | const mtldev = require("mtl-dev-sdk");
|
6 | const fse = require('fs-extra');
|
7 | const shell = require("shelljs");
|
8 | const promptList = [
|
9 | {
|
10 | type: "list",
|
11 | message: "请选择你要添加的插件:",
|
12 | name: "pluginId",
|
13 | choices: allPluginFile,
|
14 | filter: function(val) {
|
15 | return val;
|
16 | }
|
17 | }
|
18 | ];
|
19 | function addPlugin(pluginName) {
|
20 | if (!utils.isMtlProject()) {
|
21 | return;
|
22 | }
|
23 |
|
24 |
|
25 | mtldev.serchPlugins({ pluginId: pluginName }, res => {
|
26 | if (res.code == 200) {
|
27 | let plugins = res.data.plugins || [];
|
28 |
|
29 | selectPlugin(plugins);
|
30 | } else {
|
31 | utils.consoleLog(`cli : ${JSON.stringify(res)}`);
|
32 | }
|
33 | });
|
34 | return;
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | }
|
57 |
|
58 | function selectPluginJsonFile(sourceType) {
|
59 | utils.consoleLog("sourceType=="+sourceType);
|
60 | if(sourceType !="upesnPlugin"){
|
61 | selectPlugin(allPluginFile);
|
62 | }else{
|
63 | selectPlugin(allUPesnPluginFile);
|
64 | }
|
65 | }
|
66 |
|
67 |
|
68 | function formatJson(data) {
|
69 | return JSON.stringify(data,null,4);
|
70 | }
|
71 |
|
72 | function 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 |
|
90 | return newItem;
|
91 | });
|
92 |
|
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 |
|
99 |
|
100 | let retJS = mtldev.downloadPluginJS(name,shell.pwd().toString());
|
101 |
|
102 | let ret = mtldev.setMTLPlugin(name);
|
103 |
|
104 |
|
105 | });
|
106 | }
|
107 |
|
108 | module.exports = {
|
109 | addPlugin
|
110 | };
|