1
+ var fs = require ( 'fs' ) ;
1
2
var path = require ( 'path' ) ;
2
3
3
4
var _ = require ( 'underscore' ) ;
5
+ var request = require ( 'request' ) ;
4
6
5
7
var h = require ( './helper' ) ;
6
8
var config = require ( './config' ) ;
@@ -12,7 +14,12 @@ function Plugin(id, name, ver, desc, deps) {
12
14
this . ver = ver || 'default' ;
13
15
this . desc = desc ;
14
16
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 ( ) ;
16
23
}
17
24
18
25
Plugin . prototype . init = function ( ) {
@@ -25,12 +32,25 @@ Plugin.prototype.setNext = function(next) {
25
32
this . next = next ;
26
33
} ;
27
34
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
+
28
47
Plugin . prototype . help = function ( ) { } ;
29
48
30
49
Plugin . plugins = [ ] ;
31
50
32
51
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 , '../' ) ;
34
54
35
55
var plugins = [ ] ;
36
56
h . getDirData ( [ 'lib' , 'plugins' ] ) . forEach ( function ( f ) {
@@ -66,8 +86,49 @@ Plugin.init = function(head) {
66
86
Plugin . plugins = plugins ;
67
87
} ;
68
88
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 ) ) ;
71
132
} ;
72
133
73
134
module . exports = Plugin ;
0 commit comments