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
})
}
}
@@ -6,17 +6,20 @@ const ip = require('ip')
const socketIoClient = require('socket.io-client')
const fs = require('fs')
const mime = require('mime')
const EventEmitter = require('../event-emitter.js')
/**
* Watcher class
*/
class Watcher
class Watcher extends EventEmitter
{
/**
* Constructor
*/
constructor(_options)
{
super()
this.setOptions(_options)
this.setWatcher()
this.setSocket()
+224
View File
@@ -0,0 +1,224 @@
'use strict'
class EventEmitter
{
/**
* Constructor
*/
constructor()
{
this.callbacks = {}
this.callbacks.base = {}
}
/**
* On
*/
on(_names, callback)
{
const that = this
// Errors
if(typeof _names === 'undefined' || _names === '')
{
console.warn('wrong names')
return false
}
if(typeof callback === 'undefined')
{
console.warn('wrong callback')
return false
}
// Resolve names
const names = this.resolveNames(names)
// Each name
names.forEach(function(_name)
{
// Resolve name
const name = that.resolveName(_name)
// Create namespace if not exist
if(!(that.callbacks[ name.namespace ] instanceof Object))
that.callbacks[ name.namespace ] = {}
// Create callback if not exist
if(!(that.callbacks[ name.namespace ][ name.value ] instanceof Array))
that.callbacks[ name.namespace ][ name.value ] = []
// Add callback
that.callbacks[ name.namespace ][ name.value ].push(callback)
})
return this
}
/**
* Off
*/
off(_names)
{
const that = this
// Errors
if(typeof _names === 'undefined' || _names === '')
{
console.warn('wrong name')
return false
}
// Resolve names
const names = this.resolveNames(_names)
// Each name
names.forEach(function(_name)
{
// Resolve name
const name = that.resolveName(_name)
// Remove namespace
if(name.namespace !== 'base' && name.value === '')
{
delete that.callbacks[ name.namespace ]
}
// Remove specific callback in namespace
else
{
// Default
if(name.namespace === 'base')
{
// Try to remove from each namespace
for(const namespace in that.callbacks)
{
if(that.callbacks[ namespace ] instanceof Object && that.callbacks[ namespace ][ name.value ] instanceof Array)
{
delete that.callbacks[ namespace ][ name.value ]
// Remove namespace if empty
if(Object.keys(that.callbacks[ namespace ]).length === 0)
delete that.callbacks[ namespace ]
}
}
}
// Specified namespace
else if(that.callbacks[ name.namespace ] instanceof Object && that.callbacks[ name.namespace ][ name.value ] instanceof Array)
{
delete that.callbacks[ name.namespace ][ name.value ]
// Remove namespace if empty
if(Object.keys(that.callbacks[ name.namespace ]).length === 0)
delete that.callbacks[ name.namespace ]
}
}
})
return this
}
/**
* Trigger
*/
trigger(_name, _args)
{
// Errors
if(typeof _name === 'undefined' || _name === '')
{
console.warn('wrong name')
return false
}
const that = this
let finalResult = null
let result = null
// Default args
const args = !(args instanceof Array) ? [] : _args
// Resolve names (should on have one event)
let name = this.resolveNames(_name)
// Resolve name
name = this.resolveName(name[ 0 ])
// Default namespace
if(name.namespace === 'base')
{
// Try to find callback in each namespace
for(const namespace in that.callbacks)
{
if(that.callbacks[ namespace ] instanceof Object && that.callbacks[ namespace ][ name.value ] instanceof Array)
{
that.callbacks[ namespace ][ name.value ].forEach(function(callback)
{
result = callback.apply(that,args)
if(typeof finalResult === 'undefined')
{
finalResult = result
}
})
}
}
}
// Specified namespace
else if(this.callbacks[ name.namespace ] instanceof Object)
{
if(name.value === '')
{
console.warn('wrong name')
return this
}
that.callbacks[ name.namespace ][ name.value ].forEach(function(callback)
{
result = callback.apply(that, args)
if(typeof finalResult === 'undefined')
finalResult = result
})
}
return finalResult
}
/**
* Resolve names
*/
resolveNames(_names)
{
let names = _names
names = names.replace(/[^a-zA-Z0-9 ,\/.]/g, '')
names = names.replace(/[,\/]+/g, ' ')
names = names.split(' ')
return names
}
/**
* Resolve name
*/
resolveName(name)
{
const newName = {}
const parts = name.split('.')
newName.original = name
newName.value = parts[ 0 ]
newName.namespace = 'base' // Base namespace
// Specified namespace
if(parts.length > 1 && parts[ 1 ] !== '')
{
newName.namespace = parts[ 1 ]
}
return newName
}
}
module.exports = EventEmitter