11var fs = require ( 'fs' ) ;
22var path = require ( 'path' ) ;
33
4- var _ = require ( 'underscore' ) ;
54var sprintf = require ( 'sprintf-js' ) . sprintf ;
65
76var 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
67104module . exports = cmd ;
0 commit comments