UNPKG

2.78 kBJavaScriptView Raw
1const fse = require("fs-extra"); // fs-extra 扩展包
2const mtldev = require("mtl-dev-sdk");
3const utils = require("./m_util.js");
4const inquirer = require("inquirer");
5const shell = require("shelljs");
6const join = require("path").join;
7
8const promptList = [
9 {
10 type: "list",
11 message: "请选择插件模板:",
12 name: "name",
13 choices: ["helloWord"],
14 filter: function(val) {
15 // 使用filter将回答变为小写
16 return val;
17 }
18 }
19];
20
21/**
22 * MTL工程 创建插件
23 * @param {String} pName 验证插件名称是否正确
24 * @param {String} tl 插件模板
25 */
26
27async function createPlugin( source ) {
28
29 // if (!pName) {
30 // return utils.consoleLog(" 必须录入页面名称");
31 // }
32 if (utils.isMtlProject()) {
33 return utils.consoleLog(" 请不要在 mtl 工程中创建原生插件 ,这样会导致工程报错!!!"); ;
34 }
35 // if (!utils.isVerifyProjectName(pName)) {
36 // return utils.consoleLog("插件模板名称不能包含特殊字符");
37 // }
38 // if (fse.existsSync(pName)) {
39 // return utils.consoleLog(
40 // "本地已存在- " + pName + " - ,同一目录下不能重名!!!"
41 // );
42 // }
43 let plugins;
44 if (source=="mtl"){
45 plugins = mtldev.getPluginInfos(source);
46 }else{
47 plugins = mtldev.getPluginInfos("upesn");
48 }
49 if (!plugins || plugins.length <= 0) {
50 return utils.consoleLog("当前没有模板可用-");
51 }
52 getPluginsOptionByTl(plugins);
53}
54/**
55 * 选择模板,生成配置文件
56 */
57function getPluginsOptionByTl( plugins) {
58 utils.consoleLog(JSON.stringify(plugins));
59 let list = [];
60 let urls ={};
61 for (let key in plugins){
62 let va = plugins[key].name;
63 // let va = plugins[key];
64 urls[va] = plugins[key].url;
65 list.push(va)
66 }
67 if(list.length <=0 ){
68 return utils.consoleLog("前没有模板可用");
69 }
70 promptList[0].choices = list;
71 inquirer.prompt(promptList).then(answers => {
72 utils.consoleLog(answers.name);
73
74 for (let key in plugins){
75 if(answers.name.trim() == plugins[key].name ){
76 downloadPluginTl(answers.name,plugins[key].url);
77 }
78 }
79
80 });
81}
82
83//根据模板下载工程
84function downloadPluginTl(tl,url) {
85 utils.consoleLog("插件模板下载地址:"+url);
86 let workspace = shell.pwd().toString();
87 if (fse.existsSync(tl)) {
88 return utils.consoleLog(
89 "本地已存在- " + tl + " 插件 ,同一目录下不能重名!如果想要下载新模板,可以将本地模板文件夹改名。"
90 );
91 }
92 let result = mtldev.downloadPluginByTemplate(workspace, tl, url);
93 let code = result.code;
94 if (code == 200) {
95 utils.consoleLog(`插件模板创建完成: ${tl}`);
96 } else {
97 utils.consoleLog(JSON.stringify(result));
98 }
99}
100
101module.exports = {
102 createPlugin
103};
104