Skip to content

Commit 08d0a72

Browse files
committed
Refactor plugin install.
Signed-off-by: Eric Wang <[email protected]>
1 parent 9a32c09 commit 08d0a72

File tree

2 files changed

+72
-60
lines changed

2 files changed

+72
-60
lines changed

lib/commands/plugin.js

+7-56
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
var fs = require('fs');
2-
var path = require('path');
3-
4-
var _ = require('underscore');
5-
var request = require('request');
62

73
var h = require('../helper');
84
var config = require('../config');
@@ -48,62 +44,17 @@ var cmd = {
4844
}
4945
};
5046

51-
function install(src) {
52-
var config = require('../config');
53-
// assume to be a raw plugin name if not js file.
54-
if (path.extname(src) !== '.js') {
55-
src = config.sys.urls.plugin.replace('$name', src);
56-
}
57-
58-
// copy to plugins folder
59-
var dst = path.join(Plugin.dir, path.basename(src));
60-
var dststream = fs.createWriteStream(dst);
61-
62-
log.debug('copying from ' + src);
63-
var srcstream = src.startsWith('https://') ? request(src) : fs.createReadStream(src);
64-
srcstream.on('response', function(resp) {
65-
if (resp.statusCode !== 200)
66-
srcstream.emit('error', 'HTTP Error: ' + resp.statusCode);
67-
});
68-
srcstream.on('error', function(e) {
69-
log.error(e);
70-
fs.unlinkSync(dst);
71-
});
72-
73-
var spin = h.spin('Downloading ' + src);
74-
srcstream.pipe(dststream);
75-
dststream.on('close', function() {
76-
spin.stop();
77-
log.debug('copied to ' + dst);
78-
79-
// install dependencies for current platform
80-
var plugin = require(path.relative(__dirname, dst));
81-
var deps = _.map(plugin.deps, function(x) {
82-
var parts = x.split(':');
83-
if (parts.length > 1 && parts[1] !== process.platform)
84-
return '';
85-
else
86-
return parts[0];
87-
}).join(' ').trim();
88-
if (deps.length === 0) return plugin.help();
89-
90-
var cmd = 'npm install --save ' + deps;
91-
spin = h.spin(cmd);
92-
log.debug(cmd);
93-
require('child_process').exec(cmd, {
94-
cwd: path.resolve(__dirname, '../..')
95-
}, function() {
96-
spin.stop();
97-
plugin.help();
98-
});
99-
});
100-
}
101-
10247
cmd.handler = function(argv) {
10348
session.argv = argv;
10449

10550
var name = argv.name;
106-
if (argv.install) return install(name);
51+
if (argv.install) {
52+
Plugin.install(name, function(e, plugin) {
53+
if (e) return log.error(e);
54+
plugin.help();
55+
});
56+
return;
57+
}
10758

10859
var plugins = Plugin.plugins;
10960
if (name) {

lib/plugin.js

+65-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
var fs = require('fs');
12
var path = require('path');
23

34
var _ = require('underscore');
5+
var request = require('request');
46

57
var h = require('./helper');
68
var config = require('./config');
@@ -12,7 +14,12 @@ function Plugin(id, name, ver, desc, deps) {
1214
this.ver = ver || 'default';
1315
this.desc = desc;
1416
this.enabled = true;
15-
this.deps = deps || [];
17+
18+
// only need deps for current platform
19+
this.deps = _.chain(deps || [])
20+
.filter(function(x) { return x.indexOf(':') < 0 || x.indexOf(':' + process.platform) > 0; })
21+
.map(function(x) { return x.split(':')[0]; })
22+
.value();
1623
}
1724

1825
Plugin.prototype.init = function() {
@@ -25,12 +32,25 @@ Plugin.prototype.setNext = function(next) {
2532
this.next = next;
2633
};
2734

35+
Plugin.prototype.install = function(cb) {
36+
if (this.deps.length === 0) return cb();
37+
38+
var cmd = 'npm install --save ' + this.deps.join(' ');
39+
log.debug(cmd);
40+
var spin = h.spin(cmd);
41+
require('child_process').exec(cmd, {cwd: Plugin.root}, function() {
42+
spin.stop();
43+
return cb();
44+
});
45+
};
46+
2847
Plugin.prototype.help = function() {};
2948

3049
Plugin.plugins = [];
3150

3251
Plugin.init = function(head) {
33-
Plugin.dir = path.resolve(__dirname, '../lib/plugins/');
52+
Plugin.dir = path.resolve(__dirname, 'plugins/');
53+
Plugin.root = path.resolve(__dirname, '../');
3454

3555
var plugins = [];
3656
h.getDirData(['lib', 'plugins']).forEach(function(f) {
@@ -66,8 +86,49 @@ Plugin.init = function(head) {
6686
Plugin.plugins = plugins;
6787
};
6888

69-
Plugin.fullpath = function(filename) {
70-
return path.join(Plugin.dir, filename);
89+
Plugin.copy = function(src, cb) {
90+
// FIXME: remove local file support?
91+
if (path.extname(src) !== '.js') {
92+
src = config.sys.urls.plugin.replace('$name', src);
93+
}
94+
var dst = Plugin.fullpath(src);
95+
96+
var srcstream = src.startsWith('https://') ? request(src) : fs.createReadStream(src);
97+
srcstream.on('response', function(resp) {
98+
if (resp.statusCode !== 200)
99+
srcstream.emit('error', 'HTTP Error: ' + resp.statusCode);
100+
});
101+
srcstream.on('error', function(e) {
102+
spin.stop();
103+
fs.unlinkSync(dst);
104+
return cb(e);
105+
});
106+
107+
var dststream = fs.createWriteStream(dst);
108+
dststream.on('close', function() {
109+
spin.stop();
110+
return cb(null, dst);
111+
});
112+
113+
log.debug('copying from ' + src);
114+
var spin = h.spin('Downloading ' + src);
115+
srcstream.pipe(dststream);
116+
};
117+
118+
Plugin.install = function(name, cb) {
119+
Plugin.copy(name, function(e, fullpath) {
120+
if (e) return log.error(e);
121+
log.debug('copied to ' + fullpath);
122+
123+
var plugin = require(fullpath);
124+
plugin.install(function() {
125+
return cb(null, plugin);
126+
});
127+
});
128+
};
129+
130+
Plugin.fullpath = function(file) {
131+
return path.join(Plugin.dir, path.basename(file));
71132
};
72133

73134
module.exports = Plugin;

0 commit comments

Comments
 (0)