1 | const fse = require("fs-extra");
|
2 | const mtldev = require("mtl-dev-sdk");
|
3 | const utils = require("./m_util.js");
|
4 | const inquirer = require("inquirer");
|
5 | const shell = require("shelljs");
|
6 | const join = require("path").join;
|
7 |
|
8 | const promptList = [
|
9 | {
|
10 | type: "list",
|
11 | message: "请选择工程模板:",
|
12 | name: "name",
|
13 | choices: ["html", "react"],
|
14 | filter: function(val) {
|
15 |
|
16 | return val.toLowerCase();
|
17 | }
|
18 | }
|
19 | ];
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | async function createApp(an, tl) {
|
28 | if (!an) {
|
29 | return utils.consoleLog(" 必须录入工程名称 ,例如 :mtl c demo");
|
30 | }
|
31 | if (!utils.isVerifyProjectName(an)) {
|
32 | return utils.consoleLog("工程名称不能包含特殊字符,长度不能超过64位。");
|
33 | }
|
34 | if (fse.existsSync(an)) {
|
35 | return utils.consoleLog(
|
36 | "本地已存在- " + an + " -工程 ,同一目录下工程不能重名!!!"
|
37 | );
|
38 | }
|
39 | let projects = mtldev.getProjectInfos();
|
40 | if (!projects) {
|
41 | return utils.consoleLog("当前没有模板");
|
42 | }
|
43 | getProjectOptionByTl(tl, projects, an);
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 | function getProjectOptionByTl(tl, projects, an) {
|
49 | let options = {
|
50 | ...defConfig,
|
51 | projectName: an
|
52 | };
|
53 | if (tl) {
|
54 | downloadProject(tl, options);
|
55 | return;
|
56 | }
|
57 | let list = Object.keys(projects);
|
58 | utils.consoleLog(list);
|
59 | promptList[0].choices = list;
|
60 | inquirer.prompt(promptList).then(answers => {
|
61 | options.staticFilePath = answers.name == "html" ? "app/" : "build/";
|
62 | promptList[0].choices = Object.keys(projects[answers.name]);
|
63 | inquirer.prompt(promptList).then(answers => {
|
64 | utils.consoleLog(answers.name);
|
65 | downloadProject(answers.name, options);
|
66 | });
|
67 | });
|
68 | }
|
69 |
|
70 |
|
71 | function downloadProject(tl, options) {
|
72 | let workspace = shell.pwd().toString();
|
73 | mtldev.initWorkspace(workspace);
|
74 | let result = mtldev.downloadProjectByTemplate(workspace, tl, options);
|
75 | let code = result.code;
|
76 | if (code == 200) {
|
77 | utils.consoleLog(`工程创建完成: ${options.projectName}`);
|
78 |
|
79 | cpScript(join(workspace,options.projectName));
|
80 | utils.consoleLog("脚本更新完成");
|
81 | fse.removeSync(join(workspace,options.projectName,".git"));
|
82 | } else {
|
83 | utils.consoleLog(JSON.stringify(result));
|
84 | }
|
85 | }
|
86 |
|
87 | function cpScript(workspace){
|
88 | workspace = workspace || shell.pwd().toString();
|
89 | utils.consoleLog(`正在更新脚本`);
|
90 | fse.copySync(
|
91 | join(__dirname, `../res/script/${mtldev.technologyStack()}`),
|
92 | join(workspace, "script")
|
93 | );
|
94 |
|
95 | }
|
96 |
|
97 | function updateRegistry(){
|
98 | mtldev.updateRegistry(res =>{
|
99 | utils.consoleLog(res);
|
100 | });
|
101 | }
|
102 |
|
103 | const defConfig = {
|
104 | setStatusBar: {
|
105 | showStatusBar: true,
|
106 | isScreenEdge: false,
|
107 | color: "",
|
108 | isStatusBarDefault: true
|
109 | },
|
110 | serviceUrl: {
|
111 | uploadUrl:
|
112 | "https://mdoctor.yonyoucloud.com/mtldebugger/mtl/file/uploadToOSS",
|
113 | downloadUrl:
|
114 | "https://mdoctor.yonyoucloud.com/mtldebugger/mtl/stream/download"
|
115 | },
|
116 | userInfo: {
|
117 | userName: "ump",
|
118 | userId: "32",
|
119 | passWord: "",
|
120 | appCode: ""
|
121 | },
|
122 | debugServerAddress: "mdoctor.yonyoucloud.com",
|
123 | buildServerAddress: "mtlb.yyuap.com",
|
124 | buildServerAddressPort: "8050",
|
125 | IOSCerAndProvision: {
|
126 | name: "UAPMOBILE_DIS_299",
|
127 | pwd: ""
|
128 | },
|
129 | AndroidCer: {
|
130 | name: "ump",
|
131 | pwd: ""
|
132 | }
|
133 | };
|
134 |
|
135 | module.exports = {
|
136 | createApp,
|
137 | cpScript,
|
138 | updateRegistry
|
139 | };
|