2016-10-09 15:14:02 -04:00
"use strict" ;
2017-07-18 05:36:24 -04:00
const program = require ( "commander" ) ;
const child = require ( "child_process" ) ;
const colors = require ( "colors/safe" ) ;
const fs = require ( "fs" ) ;
2017-12-06 21:23:46 -05:00
const Helper = require ( "../../helper" ) ;
const Utils = require ( "../utils" ) ;
2014-08-26 14:00:12 -04:00
2014-08-24 20:19:03 -04:00
program
. command ( "edit <name>" )
2016-12-15 01:13:43 -05:00
. description ( ` Edit user file located at ${ colors . green ( Helper . getUserConfigPath ( "<name>" ) ) } . ` )
2017-08-21 01:49:32 -04:00
. on ( "--help" , Utils . extraHelp )
2014-08-24 20:19:03 -04:00
. action ( function ( name ) {
2017-12-06 22:54:54 -05:00
if ( ! fs . existsSync ( Helper . getUsersPath ( ) ) ) {
log . error ( ` ${ Helper . getUsersPath ( ) } does not exist. ` ) ;
2017-07-18 05:36:24 -04:00
return ;
}
2017-12-06 21:23:46 -05:00
const ClientManager = require ( "../../clientManager" ) ;
2017-07-18 05:36:24 -04:00
2014-08-24 20:19:03 -04:00
var users = new ClientManager ( ) . getUsers ( ) ;
2017-08-23 01:42:59 -04:00
if ( users === undefined ) { // There was an error, already logged
return ;
}
2014-08-24 20:19:03 -04:00
if ( users . indexOf ( name ) === - 1 ) {
2016-12-15 01:13:43 -05:00
log . error ( ` User ${ colors . bold ( name ) } does not exist. ` ) ;
2014-08-24 20:19:03 -04:00
return ;
}
2016-09-20 01:33:09 -04:00
var child _spawn = child . spawn (
2014-10-09 04:27:36 -04:00
process . env . EDITOR || "vi" ,
2016-05-08 02:21:31 -04:00
[ Helper . getUserConfigPath ( name ) ] ,
2014-08-24 20:19:03 -04:00
{ stdio : "inherit" }
) ;
2016-09-20 01:33:09 -04:00
child _spawn . on ( "error" , function ( ) {
2016-12-15 01:13:43 -05:00
log . error ( ` Unable to open ${ colors . green ( Helper . getUserConfigPath ( name ) ) } . ${ colors . bold ( "$EDITOR" ) } is not set, and ${ colors . bold ( "vi" ) } was not found. ` ) ;
2016-09-20 01:33:09 -04:00
} ) ;
2014-08-24 20:19:03 -04:00
} ) ;