🚧 Add internal socket back and improve general structure
This commit is contained in:
+41
-20
@@ -4,25 +4,46 @@
|
||||
const chokidar = require('chokidar')
|
||||
const fs = require('fs')
|
||||
const mime = require('mime')
|
||||
const EventEmitter = require('../event-emitter.js')
|
||||
const socketIoClient = require('socket.io-client')
|
||||
|
||||
/**
|
||||
* Watcher class
|
||||
*/
|
||||
class Watcher extends EventEmitter
|
||||
class Watcher
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
constructor(_config)
|
||||
{
|
||||
super()
|
||||
|
||||
this.config = _config
|
||||
this.path = process.cwd()
|
||||
|
||||
this.setSocket()
|
||||
this.watch()
|
||||
}
|
||||
|
||||
/**
|
||||
* Set socket
|
||||
*/
|
||||
setSocket()
|
||||
{
|
||||
// Set up
|
||||
this.socket = socketIoClient(`${this.config.domain}/app`)
|
||||
|
||||
// Connect event
|
||||
this.socket.on('connect', () =>
|
||||
{
|
||||
this.socket.emit('start_project', { name: this.config.name, path: this.path })
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
{
|
||||
console.log('connected'.green.bold)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Watch
|
||||
* Listen to modifications on files and folders
|
||||
@@ -50,7 +71,7 @@ class Watcher extends EventEmitter
|
||||
|
||||
// Set up
|
||||
this.watcher = chokidar.watch(
|
||||
process.cwd(),
|
||||
this.path,
|
||||
{
|
||||
// ignored : /[\/\\]\./,
|
||||
ignored : regex,
|
||||
@@ -62,7 +83,7 @@ class Watcher extends EventEmitter
|
||||
this.watcher.on('add', (_path) =>
|
||||
{
|
||||
// Set up
|
||||
const relativePath = _path.replace(process.cwd(), '.')
|
||||
const relativePath = _path.replace(this.path, '.')
|
||||
const mimeType = mime.lookup(relativePath)
|
||||
const file = {}
|
||||
|
||||
@@ -92,8 +113,8 @@ class Watcher extends EventEmitter
|
||||
file.content = data.toString()
|
||||
}
|
||||
|
||||
// Trigger
|
||||
this.trigger('create_file', [file])
|
||||
// Emit
|
||||
this.socket.emit('create_file', file)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -108,7 +129,7 @@ class Watcher extends EventEmitter
|
||||
this.watcher.on('change', (_path) =>
|
||||
{
|
||||
// Set up
|
||||
const relativePath = _path.replace(process.cwd(), '.')
|
||||
const relativePath = _path.replace(this.path, '.')
|
||||
const mimeType = mime.lookup(relativePath)
|
||||
const file = {}
|
||||
|
||||
@@ -138,8 +159,8 @@ class Watcher extends EventEmitter
|
||||
file.content = data.toString()
|
||||
}
|
||||
|
||||
// Trigger
|
||||
this.trigger('update_file', [file])
|
||||
// Emit
|
||||
this.socket.emit('update_file', file)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -154,10 +175,10 @@ class Watcher extends EventEmitter
|
||||
this.watcher.on('unlink', (_path) =>
|
||||
{
|
||||
// Set up
|
||||
const relativePath = _path.replace(process.cwd(), '.')
|
||||
const relativePath = _path.replace(this.path, '.')
|
||||
|
||||
// Trigger
|
||||
this.trigger('delete_file', [relativePath])
|
||||
// Emit
|
||||
this.socket.emit('delete_file', relativePath)
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
@@ -170,10 +191,10 @@ class Watcher extends EventEmitter
|
||||
this.watcher.on('addDir', (_path) =>
|
||||
{
|
||||
// Set up
|
||||
const relativePath = _path.replace(process.cwd(), '.')
|
||||
const relativePath = _path.replace(this.path, '.')
|
||||
|
||||
// Trigger
|
||||
this.trigger('create_folder', [{ path: relativePath }])
|
||||
// Emit
|
||||
this.socket.emit('create_folder', { path: relativePath })
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
@@ -186,10 +207,10 @@ class Watcher extends EventEmitter
|
||||
this.watcher.on('unlinkDir', (_path) =>
|
||||
{
|
||||
// Set up
|
||||
const relativePath = _path.replace(process.cwd(), '.')
|
||||
const relativePath = _path.replace(this.path, '.')
|
||||
|
||||
// Trigger
|
||||
this.trigger('delete_folder', [{ path: relativePath }])
|
||||
// Emit
|
||||
this.socket.emit('delete_folder', { path: relativePath })
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
|
||||
Reference in New Issue
Block a user