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