1 | const inquirer = require("inquirer");
|
2 | const allPluginFile = require("../res/plugin.json");
|
3 | const utils = require("./m_util.js");
|
4 | const mtldev = require("mtl-dev-sdk");
|
5 | const fse = require('fs-extra');
|
6 | const promptList = [
|
7 | {
|
8 | type: "list",
|
9 | message: "请选择你要添加的插件:",
|
10 | name: "pluginId",
|
11 | choices: allPluginFile,
|
12 | filter: function (val) {
|
13 | return val;
|
14 | }
|
15 | }
|
16 | ];
|
17 | function addUserPlugin() {
|
18 | if (!utils.isMtlProject()) {
|
19 | return;
|
20 | }
|
21 | var proj = JSON.parse(fse.readFileSync("./project.json").toString());
|
22 | if(proj.config.userInfo.userId=="32"||proj.config.userInfo.userName=="ump"){
|
23 | utils.consoleLog(`😢 😢 😢 当前是系统默认账户,不能通过此命令来添加用户自定义的插件 😢 😢 😢 `);
|
24 | utils.consoleLog(`!!!请执行 mtl login 命令完成登录,然后再通过此命令添加用户在构建网站上传的插件!!!`);
|
25 | return;
|
26 | }
|
27 |
|
28 | mtldev.serchUserPlugins({ userId: proj.config.userInfo.userId }, res => {
|
29 | if (res.code == 200) {
|
30 | let plugins = res.data.plugins || [];
|
31 | selectPlugin(plugins);
|
32 | } else {
|
33 | utils.consoleLog(`cli : ${JSON.stringify(res)}`);
|
34 | }
|
35 | });
|
36 | return;
|
37 | }
|
38 |
|
39 | function selectPlugin(plugins) {
|
40 | utils.consoleLog(`plugins size : ${plugins.length}`);
|
41 | if (!plugins || plugins.length <= 0) return utils.consoleLog("没有可用模板");
|
42 | let _newPlugins = plugins.map(item => {
|
43 | let { cordovaName, cordovaName: id, userId: owner } = item;
|
44 | const newItem = {
|
45 | name: cordovaName,
|
46 | value: {
|
47 | id,
|
48 | name: item.name || cordovaName,
|
49 | owner,
|
50 | parameters: JSON.parse(item.parameters.toString()) || [],
|
51 | description: item.description || ""
|
52 | }
|
53 | };
|
54 | console.log("new: ", newItem);
|
55 | return newItem;
|
56 | });
|
57 |
|
58 | promptList[0].choices = _newPlugins;
|
59 | promptList[0].message = "请选择你要添加的插件:",
|
60 | promptList[0].name = "pluginId",
|
61 | inquirer.prompt(promptList).then(answers => {
|
62 | let name = answers.pluginId;
|
63 | utils.consoleLog(name);
|
64 | let ret = mtldev.setMTLPlugin(name);
|
65 | utils.consoleLog(ret);
|
66 | utils.consoleLog("操作完成");
|
67 | });
|
68 | }
|
69 |
|
70 | module.exports = {
|
71 | addUserPlugin
|
72 | };
|