UNPKG

9.31 kBJavaScriptView Raw
1const fs = require('fs');
2const fse = require("fs-extra");
3const path = require('path');
4const logger = require('../utils/logger');
5const utils = require('../utils');
6const ora = require('ora');
7const inquirer = require('inquirer');
8const request = require('request');
9const rimraf = require('rimraf');
10const install = require("./install");
11
12function add(op) {
13 if (typeof op.name !== 'string') op.name = "";
14 op.name = op.name.trim();
15 if (utils.count(op.name) === 0) {
16 logger.fatal(`请输入要添加的插件名称!`);
17 return;
18 }
19 op.baseUrl = utils.apiUrl() + 'plugins/client/';
20 //
21 if (typeof op.__nameBak === 'undefined') {
22 op.__nameBak = op.name;
23 }
24 op.__nameAlso = "";
25 if (utils.strExists(op.name, ',')) {
26 op.__nameAlso = utils.getMiddle(op.name, ',', null);
27 op.name = utils.getMiddle(op.name, null, ',').trim();
28 }
29 if (utils.strExists(op.__nameBak, ',')) {
30 op.simple = op.__nameAlso !== "";
31 }
32 //
33 let tmpMatch = (op.name + "").match(/^https:\/\/github.com\/([^/]+)\/([^/]+)\/*$/);
34 if (tmpMatch) {
35 op.name = tmpMatch[1] + "/" + tmpMatch[2];
36 op.isGithub = true;
37 } else {
38 op.isGithub = false;
39 }
40 //
41 let nextCallback = () => {
42 if (op.__nameAlso !== "") {
43 op.name = op.__nameAlso;
44 add(op);
45 } else if (op.__endExit === true) {
46 process.exit();
47 }
48 };
49 //
50 getInfo(op, (op) => {
51 op.callback = () => {
52 eeuiScript(op.name, true, () => {
53 nextCallback();
54 });
55 };
56 install.add(op);
57 });
58}
59
60function remove(op) {
61 if (typeof op.name !== 'string') op.name = "";
62 op.name = op.name.trim();
63 if (utils.count(op.name) === 0) {
64 selectRemove(op);
65 return;
66 }
67 op.baseUrl = utils.apiUrl() + 'plugins/client/';
68 //
69 if (typeof op.__nameBak === 'undefined') {
70 op.__nameBak = op.name;
71 }
72 op.__nameAlso = "";
73 if (utils.strExists(op.name, ',')) {
74 op.__nameAlso = utils.getMiddle(op.name, ',', null);
75 op.name = utils.getMiddle(op.name, null, ',').trim();
76 }
77 if (utils.strExists(op.__nameBak, ',')) {
78 op.simple = op.__nameAlso !== "";
79 }
80 //
81 let tmpMatch = (op.name + "").match(/^https:\/\/github.com\/([^/]+)\/([^/]+)\/*$/);
82 if (tmpMatch) {
83 op.name = tmpMatch[1] + "/" + tmpMatch[2];
84 op.isGithub = true;
85 } else {
86 op.isGithub = false;
87 }
88 //
89 let nextCallback = () => {
90 if (op.__nameAlso !== "") {
91 op.name = op.__nameAlso;
92 remove(op);
93 }
94 };
95 //
96 let func = () => {
97 op.callback = () => {
98 eeuiScript(op.name, false, () => {
99 let tmpPath = path.resolve(op.rootDir, "plugins", op.name);
100 rimraf(tmpPath, () => {
101 tmpPath = path.resolve(tmpPath, "../");
102 if (tmpPath !== path.resolve(op.rootDir, "plugins")) {
103 let files = fs.readdirSync(tmpPath);
104 if (files.length === 0) {
105 fse.removeSync(tmpPath);
106 }
107 }
108 nextCallback();
109 });
110 });
111 };
112 install.remove(op);
113 };
114 if (op.simple === true || utils.strExists(op.__nameBak, ',')) {
115 func();
116 }else{
117 inquirer.prompt([{
118 type: 'confirm',
119 message: `即将删除插件${op.name},是否确定删除?`,
120 name: 'ok',
121 }]).then(answers => {
122 if (answers.ok) {
123 func();
124 } else {
125 logger.fatal(`放弃删除${op.name}!`);
126 }
127 }).catch(console.error);
128 }
129}
130
131function selectRemove(op) {
132 let getDir = (dirPath, first) => {
133 let array = [];
134 let lists = fs.readdirSync(dirPath);
135 lists.forEach((filename) => {
136 let filedir = path.join(dirPath, filename);
137 let stats = utils.pathType(filedir);
138 if (stats === 2) {
139 if (typeof first === "undefined") {
140 array = array.concat(getDir(filedir, filename));
141 } else {
142 filename = first + "/" + filename;
143 if (["eeui/framework", "eeui/WeexSDK"].indexOf(filename) === -1) {
144 array.push({
145 name: (array.length + 1) + '.' + filename,
146 value: filename,
147 });
148 }
149 }
150 }
151 });
152 return array;
153 };
154 let choices = getDir(path.resolve(process.cwd(), 'plugins'));
155 if (choices.length === 0) {
156 logger.fatal(`没有找到可删除的插件!`);
157 }
158 inquirer.prompt([{
159 type: 'list',
160 name: 'name',
161 message: '请选择要删除的插件:',
162 choices: choices
163 }]).then(answers => {
164 op.name = answers.name;
165 remove(op);
166 });
167}
168
169function repair(callback) {
170 let rootDir = process.cwd();
171 let configFile = path.resolve(rootDir, "plugins/config.json");
172 let configInfo = utils.jsonParse(!fs.existsSync(configFile) ? {} : fs.readFileSync(configFile, 'utf8'));
173 //
174 let dependencies = utils.getObject(configInfo, 'dependencies');
175 if (!utils.isJson(dependencies) || utils.count(dependencies) === 0) {
176 callback();
177 return;
178 }
179 //
180 let func = () => {
181 utils.each(dependencies, (name, op) => {
182 delete dependencies[name];
183 op.rootDir = rootDir;
184 op.simple = utils.count(dependencies) > 0;
185 //
186 install.changeSetting(op, true);
187 install.changeGradle(op, true);
188 install.cleanIdea(op);
189 install.invokeAndroid(op, () => {
190 install.changeProfile(op, true);
191 install.invokeIos(op, () => {
192 logger.success('插件' + op.name + '添加成功!');
193 if (op.simple) {
194 func();
195 } else {
196 eeuiScript(null, true, () => {
197 if (typeof callback == "function") {
198 callback();
199 } else {
200 logger.success(`插件修复完成。`);
201 }
202 });
203 }
204 });
205 });
206 return false;
207 });
208 };
209 func();
210}
211
212function eeuiScript(assignName, isInstall, callback) {
213 let rootDir = process.cwd();
214 let jsArray = [];
215 //
216 if (typeof assignName === "string" && utils.count(assignName) > 0) {
217 let jsPath = path.resolve(rootDir, `plugins/${assignName}/script/${isInstall ? 'install' : 'uninstall'}.js`);
218 if (fs.existsSync(jsPath)) {
219 jsArray.push("node " + jsPath);
220 }
221 } else {
222 let configFile = path.resolve(rootDir, "plugins/config.json");
223 let configInfo = utils.jsonParse(!fs.existsSync(configFile) ? {} : fs.readFileSync(configFile, 'utf8'));
224 utils.each(utils.getObject(configInfo, 'dependencies'), (name) => {
225 let jsPath = path.resolve(rootDir, `plugins/${name}/script/${isInstall ? 'install' : 'uninstall'}.js`);
226 if (fs.existsSync(jsPath)) {
227 jsArray.push("node " + jsPath);
228 }
229 });
230 }
231 //
232 if (jsArray.length === 0) {
233 typeof callback === 'function' && callback();
234 } else {
235 let spinFetch = ora('run nodejs...');
236 let timeout = setTimeout(() => {
237 spinFetch.start();
238 }, 500);
239 utils.exec(jsArray.join(" && "), false).then(() => {
240 clearTimeout(timeout);
241 spinFetch.stop();
242 typeof callback === 'function' && callback();
243 });
244 }
245}
246
247function getInfo(op, callback) {
248 let spinFetch = ora('正在获取插件详情...');
249 spinFetch.start();
250 let reqGithub = () => {
251 request("https://api.github.com/repos/" + op.name, {
252 headers: {
253 'User-Agent': 'request'
254 }
255 }, (err, res, body) => {
256 spinFetch.stop();
257 let data = utils.jsonParse(body);
258 if (utils.runNum(data.id) === 0) {
259 logger.fatal(`获取插件失败:找不到相关插件!`);
260 }
261 op.isGithub = true;
262 callback(Object.assign(op, {
263 name: op.name,
264 fileinfo: [{
265 path: `https://github.com/${op.name}/archive/${data.default_branch || 'master'}.zip`
266 }]
267 }));
268 });
269 };
270 if (op.isGithub === true) {
271 reqGithub();
272 return;
273 }
274 //
275 request(utils.apiUrl() + 'plugins/client/' + op.name + '?version=' + utils.projectVersion(), (err, res, body) => {
276 let data = utils.jsonParse(body);
277 if (data.ret !== 1) {
278 reqGithub();
279 } else {
280 spinFetch.stop();
281 callback(Object.assign(op, data.data, {name: op.name}));
282 }
283 });
284}
285
286module.exports = {add, remove, repair, eeuiScript};