Improve parameters using yargs

This commit is contained in:
brunosimon
2017-07-23 00:50:05 +02:00
parent 2770fad856
commit 47dafb763a
5 changed files with 606 additions and 118 deletions
+74 -62
View File
@@ -3,8 +3,7 @@
// Depedencies
const ip = require('ip')
const Site = require('./site')
const Watcher = require('./watcher')
const defaultConfig = require('./config.default.json')
const Watcher = require('./watcher.js')
/**
* App class
@@ -14,73 +13,86 @@ class App
/**
* Constructor
*/
constructor(_options)
constructor()
{
this.setArguments()
this.setOptions(_options)
this.setSite()
this.setConfig()
// this.setSite()
this.setWatcher()
}
/**
* Set options
* Set config
*/
setOptions(_options)
setConfig()
{
const options = typeof _options === 'object' ? _options : {}
// Defaults
if(typeof options.debug === 'undefined')
const args = require('yargs')
.option('debug', {
alias: 'd',
describe: 'Use debug mode',
default: false,
type: 'boolean'
})
.option('name', {
alias: 'n',
describe: 'Project name',
default: '',
type: 'string'
})
.option('port', {
alias: 'p',
describe: 'Port to use',
default: 1571,
type: 'number'
})
.option('exclude', {
alias: 'e',
describe: 'Files to exclude',
default:
[
'**/.DS_Store',
'node_modules/**',
'vendor/**',
'.git'
],
type: 'array'
})
.option('initial-send', {
alias: 'i',
describe: 'Send current file in folder immediately',
default: false,
type: 'boolean'
})
.option('max-file-size', {
alias: 'm',
describe: 'Maximum file size in octets',
default: 99999,
type: 'number'
})
.argv
const config = {}
config.debug = args.debug
config.name = args.name
config.port = args.port
config.exclude = args.exclude
config.initialSend = args['initial-send']
config.maxFileSize = args['max-file-size']
config.domain = `http://${ip.address()}:${config.port}`
if(config.name === '')
{
if(this.arguments.length > 1)
if(args._.length > 0 && typeof args._[0] === 'string' && args._[0] !== '')
{
options.debug = this.arguments[ this.arguments.length - 1 ] === 'true'
config.name = args._[0]
}
else
{
options.debug = defaultConfig.debug
config.name = 'No name'
}
}
if(typeof options.port === 'undefined')
{
options.port = defaultConfig.port
}
if(typeof options.domain === 'undefined')
{
options.domain = `http://${ip.address()}:${options.port}`
}
if(typeof options.maxFileSize === 'undefined')
{
options.maxFileSize = defaultConfig.maxFileSize
}
if(typeof options.exclude === 'undefined')
{
options.exclude = defaultConfig.exclude
}
// Save
this.options = options
}
/**
* Set arguments
* Retrieve arguments and test if missing
*/
setArguments()
{
// Set up
this.arguments = process.argv.slice(2)
// Missing project name
if(this.arguments.length === 0)
{
// Stop process
throw new Error('Missing arguments: first argument should be the projet name'.red)
}
this.config = config
}
/**
@@ -90,9 +102,9 @@ class App
setSite()
{
this.site = new Site({
port : this.options.port,
domain: this.options.domain,
debug : this.options.debug
port: this.config.port,
domain: this.config.domain,
debug: this.config.debug
})
}
@@ -103,12 +115,12 @@ class App
setWatcher()
{
this.watcher = new Watcher({
port : this.options.port,
domain : this.options.domain,
debug : this.options.debug,
maxFileSize: this.options.maxFileSize,
exclude : this.options.exclude,
name : this.arguments[ 0 ]
port: this.config.port,
domain: this.config.domain,
debug: this.config.debug,
maxFileSize: this.config.maxFileSize,
exclude: this.config.exclude,
name: this.config.name
})
}
}