2017-07-10 20:03:46 +00:00
import { BaseCommand } from './base-command.js' ;
2017-07-18 18:49:47 +00:00
import { _ , setLocale } from 'lib/locale.js' ;
import { app } from './app.js' ;
2017-07-10 20:03:46 +00:00
import { Setting } from 'lib/models/setting.js' ;
class Command extends BaseCommand {
usage ( ) {
2017-07-28 18:13:07 +00:00
return 'config [name] [value]' ;
2017-07-10 20:03:46 +00:00
}
description ( ) {
2017-07-24 18:58:11 +00:00
return _ ( "Gets or sets a config value. If [value] is not provided, it will show the value of [name]. If neither [name] nor [value] is provided, it will list the current configuration." ) ;
2017-07-10 20:03:46 +00:00
}
2017-07-24 19:47:01 +00:00
options ( ) {
return [
2017-08-22 17:57:35 +00:00
[ '-v, --verbose' , _ ( 'Also displays unset and hidden config variables.' ) ] ,
2017-07-24 19:47:01 +00:00
] ;
}
2017-07-10 20:03:46 +00:00
async action ( args ) {
2017-08-22 17:57:35 +00:00
const verbose = args . options . verbose ;
2017-07-24 18:58:11 +00:00
const renderKeyValue = ( name ) => {
const value = Setting . value ( name ) ;
if ( Setting . isEnum ( name ) ) {
2017-08-22 17:57:35 +00:00
return _ ( '%s = %s (%s)' , name , value , Setting . enumOptionsDoc ( name ) ) ;
2017-07-24 18:58:11 +00:00
} else {
return _ ( '%s = %s' , name , value ) ;
}
}
2017-07-10 20:03:46 +00:00
if ( ! args . name && ! args . value ) {
2017-08-22 17:57:35 +00:00
let keys = Setting . keys ( ! verbose , 'cli' ) ;
2017-07-10 20:03:46 +00:00
for ( let i = 0 ; i < keys . length ; i ++ ) {
2017-08-22 17:57:35 +00:00
const value = Setting . value ( keys [ i ] ) ;
if ( ! verbose && ! value ) continue ;
2017-10-07 16:30:27 +00:00
this . stdout ( renderKeyValue ( keys [ i ] ) ) ;
2017-07-10 20:03:46 +00:00
}
return ;
}
if ( args . name && ! args . value ) {
2017-10-07 16:30:27 +00:00
this . stdout ( renderKeyValue ( args . name ) ) ;
2017-07-10 20:03:46 +00:00
return ;
}
Setting . setValue ( args . name , args . value ) ;
2017-07-18 18:49:47 +00:00
if ( args . name == 'locale' ) {
setLocale ( Setting . value ( 'locale' ) ) ;
app ( ) . onLocaleChanged ( ) ;
}
2017-07-10 20:03:46 +00:00
await Setting . saveAll ( ) ;
}
}
module . exports = Command ;