:zap:🚧 Remove internal socket and improve general structure

This commit is contained in:
brunosimon
2017-07-23 01:45:49 +02:00
parent 47dafb763a
commit cfcf81364f
17 changed files with 109 additions and 357 deletions
+26 -82
View File
@@ -2,8 +2,6 @@
// Depedencies
const chokidar = require('chokidar')
const ip = require('ip')
const socketIoClient = require('socket.io-client')
const fs = require('fs')
const mime = require('mime')
const EventEmitter = require('../event-emitter.js')
@@ -16,82 +14,28 @@ class Watcher extends EventEmitter
/**
* Constructor
*/
constructor(_options)
constructor(_config)
{
super()
this.setOptions(_options)
this.setWatcher()
this.setSocket()
this.config = _config
this.watch()
}
/**
* Set options
* Watch
* Listen to modifications on files and folders
*/
setOptions(_options)
{
const options = typeof _options !== 'object' ? {} : _options
// Defaults
if(typeof options.debug === 'undefined')
{
options.debug = false
}
if(typeof options.port === 'undefined')
{
options.port = 1571
}
if(typeof options.domain === 'undefined')
{
options.domain = `http://${ip.address()}:${options.port}`
}
if(typeof options.maxFileSize === 'undefined')
{
options.maxFileSize = 2000
}
// Save
this.options = options
}
/**
* Set socket
* Connect to site
*/
setSocket()
{
// Set up
this.socket = socketIoClient(`${this.options.domain}/app`)
// Connect event
this.socket.on('connect', () =>
{
this.socket.emit('start_project', { name: this.options.name })
// Debug
if(this.options.debug)
{
console.log('connected'.green.bold)
}
})
}
/**
* Set watcher
* Listen to modifications on file and folders
*/
setWatcher()
watch()
{
// Create ignored regex
const regexs = []
const Minimatch = require('minimatch').Minimatch
for(const _excludeKey in this.options.exclude)
for(const _excludeKey in this.config.exclude)
{
const _exclude = this.options.exclude[ _excludeKey ]
const _exclude = this.config.exclude[_excludeKey]
const minimatch = new Minimatch(_exclude, { dot: true })
let regex = '' + minimatch.makeRe()
@@ -135,7 +79,7 @@ class Watcher extends EventEmitter
fs.stat(_path, (error, stats) =>
{
// Max file size
if(this.options.maxFileSize < stats.size)
if(this.config.maxFileSize < stats.size)
{
file.canRead = false
}
@@ -148,13 +92,13 @@ class Watcher extends EventEmitter
file.content = data.toString()
}
// Send
this.socket.emit('create_file', file)
// Trigger
this.trigger('create_file', [file])
})
})
// Debug
if(this.options.debug)
if(this.config.debug)
{
console.log('add:'.green.bold, relativePath)
}
@@ -181,7 +125,7 @@ class Watcher extends EventEmitter
fs.stat(_path, (error, stats) =>
{
// Test max file size
if(this.options.maxFileSize < stats.size)
if(this.config.maxFileSize < stats.size)
{
file.canRead = false
}
@@ -194,13 +138,13 @@ class Watcher extends EventEmitter
file.content = data.toString()
}
// Send
this.socket.emit('update_file', file)
// Trigger
this.trigger('update_file', [file])
})
})
// Debug
if(this.options.debug)
if(this.config.debug)
{
console.log('change:'.green.bold, relativePath)
}
@@ -212,11 +156,11 @@ class Watcher extends EventEmitter
// Set up
const relativePath = _path.replace(process.cwd(), '.')
// Send
this.socket.emit('delete_file', { path: relativePath })
// Trigger
this.trigger('delete_file', [relativePath])
// Debug
if(this.options.debug)
if(this.config.debug)
{
console.log('unlink:'.green.bold, relativePath)
}
@@ -228,11 +172,11 @@ class Watcher extends EventEmitter
// Set up
const relativePath = _path.replace(process.cwd(), '.')
// Send
this.socket.emit('create_folder', { path: relativePath })
// Trigger
this.trigger('create_folder', [{ path: relativePath }])
// Debug
if(this.options.debug)
if(this.config.debug)
{
console.log('addDir:'.green.bold, relativePath)
}
@@ -244,11 +188,11 @@ class Watcher extends EventEmitter
// Set up
const relativePath = _path.replace(process.cwd(), '.')
// Send
this.socket.emit('delete_folder', { path: relativePath })
// Trigger
this.trigger('delete_folder', [{ path: relativePath }])
// Debug
if(this.options.debug)
if(this.config.debug)
{
console.log('unlinkDir:'.green.bold, relativePath)
}