-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathplugin.js
More file actions
79 lines (74 loc) · 2.49 KB
/
plugin.js
File metadata and controls
79 lines (74 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const path = require('path');
const fs = require('fs-extra');
const utils = require('../utils.js');
const shell = require('shelljs');
const _ = require('underscore');
let root, config;
module.exports = {
setOptions: function (yargs) {
yargs.option('name', {
alias: 'n',
describe: '插件名,需要写前缀 yapi-plugin- '
})
yargs.option('build', {
alias: 'b',
default: true,
describe: '是否编译客户端代码,true 为编译, false 为不编译, 默认为 true'
})
},
run: function (argv) {
try {
root = process.cwd();
let configFilepath = path.resolve(root, 'config.json');
if (!utils.fileExist(configFilepath)) {
throw new Error('项目目录找不到配置文件 config.json ');
}
if( !shell.which('ykit')){
throw new Error('需要安装 ykit ');
}
let name = argv.name;
config = require(configFilepath);
if (!config.plugins || !Array.isArray(config.plugins)) {
config.plugins = [];
}
if (!name) {
throw new Error('请输入需要安装的插件Name, yapi-cli plugin --name yapi-plugin-*** ')
}
if (name.indexOf('yapi-plugin-') !== 0) {
throw new Error('插件name 前缀必需是 yapi-plugin-')
}
let pluginName = name.substr('yapi-plugin-'.length)
if (_.find(config.plugins, plugin=>{
if(!plugin) return null;
if(typeof plugin === 'string'){
return plugin == pluginName;
}else if(typeof plugin === 'object'){
return plugin.name == pluginName
}
})) {
shell.cd('vendors');
utils.log('正在下载插件...');
shell.exec('npm install --registry https://registry.npm.taobao.org ' + name);
utils.log('更新插件成功')
}else{
shell.cd('vendors');
utils.log('正在下载插件...');
shell.exec('npm install --registry https://registry.npm.taobao.org ' + name);
utils.log('安装插件成功')
config.plugins.push({
name: pluginName
})
}
fs.writeFileSync(configFilepath, JSON.stringify(config, null, ' '));
if(argv.build === true){
utils.log('正在安装依赖...');
shell.exec('npm install --registry https://registry.npm.taobao.org');
shell.exec('ykit pack -m')
utils.log('编译客户端成功,请重启服务器')
}
} catch (e) {
utils.log(e.message);
}
},
desc: '插件安装'
}