UNPKG

3.65 kBJavaScriptView Raw
1const fs = require("fs");
2const path = require('path');
3const install = require('./install');
4const plugin = require('./index');
5const logger = require('../utils/logger');
6const utils = require("../utils");
7const fse = require("fs-extra");
8const inquirer = require('inquirer');
9const regFun = (str) => str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
10
11function create(op) {
12 inquirer.prompt([{
13 type: 'input',
14 name: 'name',
15 default: () => {
16 if (typeof op.name !== 'string') op.name = "";
17 return op.name.trim() ? op.name.trim() : 'demo/sample';
18 },
19 message: "请输入插件仓库名称:",
20 validate: (value) => {
21 let pass = value.match(/^[a-z][a-z0-9\-_@]+\/[a-z][a-z0-9\-_@]+$/i);
22 if (pass) {
23 return true;
24 }
25 return '输入格式错误!(格式:username/pluginname)';
26 }
27 }, {
28 type: 'input',
29 name: 'desc',
30 message: '请输入插件描述(选填):'
31 }]).then(answers => {
32 op.name = answers.name.trim();
33 let newPath = path.resolve(op.rootDir, "plugins", op.name);
34 if (fse.pathExistsSync(newPath)) {
35 let lists = utils.fileDirDisplay(newPath);
36 if (lists.file.length > 0) {
37 logger.fatal('插件目录' + op.name + '已存在!');
38 }
39 }
40 //添加android
41 createProject(op.name, newPath);
42 fs.writeFileSync(path.resolve(newPath, "config.json"), JSON.stringify(utils.sortObject({
43 "desc": answers.desc,
44 "requireFormatName": utils.spritUpperCase(answers.name),
45 "requireName": answers.name,
46 }), null, "\t"), 'utf8');
47 //
48 install.changeSetting(op, true);
49 install.changeGradle(op, true);
50 install.cleanIdea(op);
51 install.invokeAndroid(op, () => {
52 install.changeProfile(op, true);
53 install.invokeIos(op, () => {
54 utils.pluginsJson(true, op);
55 plugin.eeuiScript(op.name, true, () => {
56 logger.success('插件' + op.name + '添加成功!');
57 });
58 });
59 });
60 }).catch(console.error);
61}
62
63function createProject(projectName, projectPath) {
64 let demoName = "PluginDemo";
65 let demoPath = path.resolve(__dirname, "template");
66 if (!fse.pathExistsSync(demoPath)) {
67 logger.fatal('模板文件不存在!');
68 }
69 let lists = utils.fileDirDisplay(demoPath);
70 //复制目录
71 for (let index in lists.dir) {
72 if (!lists.dir.hasOwnProperty(index)) continue;
73 let oldPath = lists.dir[index];
74 let newPath = oldPath.replace(new RegExp(regFun(demoPath), "g"), projectPath);
75 newPath = newPath.replace(new RegExp(regFun(demoName), "g"), utils.spritUpperCase(projectName));
76 fse.ensureDirSync(newPath);
77 }
78 //复制文件
79 for (let index in lists.file) {
80 if (!lists.file.hasOwnProperty(index)) continue;
81 let oldPath = lists.file[index];
82 let newPath = oldPath.replace(new RegExp(regFun(demoPath), "g"), projectPath);
83 newPath = newPath.replace(new RegExp(regFun(demoName), "g"), utils.spritUpperCase(projectName));
84 fse.ensureFileSync(newPath);
85 //
86 let result = fs.readFileSync(oldPath, 'utf8');
87 result = result.replace(new RegExp(regFun(demoName), "gm"), utils.spritUpperCase(projectName));
88 if (result) {
89 fs.writeFileSync(newPath, result, 'utf8');
90 } else {
91 fse.copySync(oldPath, newPath);
92 }
93 }
94}
95
96module.exports = {create};