1+ var fs = require ( 'fs' ) ;
12var path = require ( 'path' ) ;
23
34var _ = require ( 'underscore' ) ;
5+ var request = require ( 'request' ) ;
46
57var h = require ( './helper' ) ;
68var 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
1825Plugin . 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+
2847Plugin . prototype . help = function ( ) { } ;
2948
3049Plugin . plugins = [ ] ;
3150
3251Plugin . 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
73134module . exports = Plugin ;
0 commit comments