Skip to content

Commit 6c7948f

Browse files
committed
[Plugin] support enable/disable.
Signed-off-by: Eric Wang <[email protected]>
1 parent 948cf45 commit 6c7948f

File tree

3 files changed

+77
-22
lines changed

3 files changed

+77
-22
lines changed

lib/commands/plugin.js

+52-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
var fs = require('fs');
22
var path = require('path');
33

4-
var _ = require('underscore');
54
var sprintf = require('sprintf-js').sprintf;
65

76
var h = require('../helper');
@@ -19,9 +18,21 @@ var cmd = {
1918
describe: 'Install plugin',
2019
default: false
2120
},
22-
delete: {
21+
enable: {
22+
alias: 'e',
23+
type: 'boolean',
24+
describe: 'Enable plugin',
25+
default: false
26+
},
27+
disable: {
2328
alias: 'd',
2429
type: 'boolean',
30+
describe: 'Disable plugin',
31+
default: false
32+
},
33+
delete: {
34+
alias: 'D',
35+
type: 'boolean',
2536
describe: 'Delete plugin',
2637
default: false
2738
}
@@ -36,32 +47,58 @@ cmd.handler = function(argv) {
3647
if (!name || !fs.existsSync(name))
3748
return log.error('Plugin not found!');
3849

39-
var newName = path.resolve(__dirname, '../plugins/' + path.basename(name));
50+
var newName = path.join(Plugin.dir, path.basename(name));
4051
fs.createReadStream(name).pipe(fs.createWriteStream(newName));
4152
return;
4253
}
4354

44-
if (argv.delete) {
45-
var f = _.find(h.getDirData(['lib', 'plugins']), function(f) {
46-
return f.data !== null && f.data.name === name;
55+
var plugins = Plugin.plugins;
56+
if (name) {
57+
plugins = plugins.filter(function(p) {
58+
return p.name === name;
4759
});
48-
if (!f) return log.error('Plugin not found!');
60+
}
4961

50-
fs.unlink(f.fullpath, function(e) {
62+
if (!argv.enable && !argv.disable && !argv.delete) {
63+
plugins.forEach(function(p) {
64+
log.info(sprintf('%s %-20s %-15s %s',
65+
h.prettyText('', p.enabled), p.name, p.ver, p.desc));
66+
});
67+
return;
68+
}
69+
70+
if (plugins.length === 0)
71+
return log.error('Plugin not found!');
72+
var plugin = plugins[0];
73+
var oldname = Plugin.fullpath(plugin.file);
74+
var newname;
75+
76+
if (argv.enable) {
77+
if (plugin.file[0] !== '.') return;
78+
newname = Plugin.fullpath(plugin.file.substr(1));
79+
80+
fs.rename(oldname, newname, function(e) {
5181
if (e) log.error(e.message);
5282
});
5383
return;
5484
}
5585

56-
var plugins = Plugin.plugins;
57-
if (name) {
58-
plugins = plugins.filter(function(p) {
59-
return p.name === name;
86+
if (argv.disable) {
87+
if (plugin.file[0] === '.') return;
88+
newname = Plugin.fullpath('.' + plugin.file);
89+
90+
fs.rename(oldname, newname, function(e) {
91+
if (e) log.error(e.message);
92+
});
93+
return;
94+
}
95+
96+
if (argv.delete) {
97+
fs.unlink(oldname, function(e) {
98+
if (e) log.error(e.message);
6099
});
100+
return;
61101
}
62-
plugins.forEach(function(p) {
63-
log.info(sprintf('%-20s %-15s %s', p.name, p.ver, p.desc));
64-
});
65102
};
66103

67104
module.exports = cmd;

lib/helper.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ h.getDirData = function(paths) {
140140
paths.unshift(__dirname);
141141
var dir = path.join.apply(path, paths);
142142

143-
return _.map(fs.readdirSync(dir), function(filename) {
144-
var fullpath = path.join(dir, filename);
145-
var ext = path.extname(filename);
143+
return _.map(fs.readdirSync(dir), function(file) {
144+
var fullpath = path.join(dir, file);
145+
var ext = path.extname(file);
146146

147-
var name = path.basename(filename, ext);
147+
var name = path.basename(file, ext);
148148
var data = null;
149149

150150
switch (ext) {
@@ -157,7 +157,7 @@ h.getDirData = function(paths) {
157157
default:
158158
break;
159159
}
160-
return {name: name, data: data, fullpath: fullpath};
160+
return {name: name, data: data, file: file};
161161
});
162162
};
163163

lib/plugin.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var path = require('path');
2+
13
var _ = require('underscore');
24

35
var h = require('./helper');
@@ -9,6 +11,7 @@ function Plugin(id, name, ver, desc) {
911
this.name = name;
1012
this.ver = ver || 'default';
1113
this.desc = desc;
14+
this.enabled = true;
1215
}
1316

1417
Plugin.prototype.init = function() {
@@ -23,15 +26,25 @@ Plugin.prototype.setNext = function(next) {
2326
Plugin.plugins = [];
2427

2528
Plugin.init = function(head) {
29+
Plugin.dir = path.resolve(__dirname, '../lib/plugins/');
30+
2631
var plugins = [];
2732
h.getDirData(['lib', 'plugins']).forEach(function(f) {
2833
var p = f.data;
2934
if (!p) return;
3035

36+
p.file = f.file;
37+
if (f.name[0] === '.') p.enabled = false;
38+
3139
log.trace('found plugin: ' + p.name + '=' + p.ver);
32-
p.init();
40+
if (p.enabled) {
41+
p.init();
42+
log.trace('inited plugin: ' + p.name);
43+
} else {
44+
log.trace('skipped plugin: ' + p.name);
45+
}
46+
3347
plugins.push(p);
34-
log.trace('inited plugin: ' + p.name);
3548
});
3649

3750
// chain the plugins together
@@ -42,10 +55,15 @@ Plugin.init = function(head) {
4255

4356
var last = head;
4457
plugins.forEach(function(p) {
58+
if (!p.enabled) return;
4559
last.setNext(p);
4660
last = p;
4761
});
4862
Plugin.plugins = plugins;
4963
};
5064

65+
Plugin.fullpath = function(filename) {
66+
return path.join(Plugin.dir, filename);
67+
};
68+
5169
module.exports = Plugin;

0 commit comments

Comments
 (0)