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