-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathplugin.js
More file actions
110 lines (95 loc) · 2.58 KB
/
plugin.js
File metadata and controls
110 lines (95 loc) · 2.58 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var fs = require('fs');
var path = require('path');
var h = require('../helper');
var log = require('../log');
var Plugin = require('../plugin');
var session = require('../session');
var cmd = {
command: 'plugin [name]>',
desc: 'show plugins',
builder: {
install: {
alias: 'i',
type: 'boolean',
describe: 'Install plugin',
default: false
},
enable: {
alias: 'e',
type: 'boolean',
describe: 'Enable plugin',
default: false
},
disable: {
alias: 'd',
type: 'boolean',
describe: 'Disable plugin',
default: false
},
delete: {
alias: 'D',
type: 'boolean',
describe: 'Delete plugin',
default: false
}
}
};
function install(name) {
// copy to plugins folder
var newname = path.join(Plugin.dir, path.basename(name));
var src = fs.createReadStream(name);
src.pipe(fs.createWriteStream(newname));
src.on('end', function() {
log.debug('copied to ' + newname);
// install dependencies
var plugin = require(path.relative(__dirname, newname));
if (plugin.deps === undefined || plugin.deps.length === 0) return;
var cmd = 'npm install --save ' + plugin.deps.join(' ');
log.debug(cmd);
require('child_process').execSync(cmd, {
cwd: path.resolve(__dirname, '../..')
});
});
}
cmd.handler = function(argv) {
session.argv = argv;
var name = argv.name;
if (argv.install) {
if (!name || !fs.existsSync(name)) return log.error('Plugin not found!');
return install(name);
}
var plugins = Plugin.plugins;
if (name) {
plugins = plugins.filter(function(p) {
return p.name === name;
});
}
if (!argv.enable && !argv.disable && !argv.delete) {
plugins.forEach(function(p) {
log.printf('%s %-18s %-15s %s', h.prettyText('', p.enabled), p.name, p.ver, p.desc);
});
return;
}
if (plugins.length === 0) return log.error('Plugin not found!');
var plugin = plugins[0];
var oldname = Plugin.fullpath(plugin.file);
var newname;
if (argv.enable) {
if (plugin.file[0] !== '.') return;
newname = Plugin.fullpath(plugin.file.substr(1));
fs.rename(oldname, newname, function(e) {
if (e) log.error(e.message);
});
} else if (argv.disable) {
if (plugin.file[0] === '.') return;
newname = Plugin.fullpath('.' + plugin.file);
fs.rename(oldname, newname, function(e) {
if (e) log.error(e.message);
});
} else if (argv.delete) {
fs.unlink(oldname, function(e) {
if (e) log.error(e.message);
});
}
};
module.exports = cmd;