🚧 Add internal socket back and improve general structure
This commit is contained in:
+111
-71
@@ -2,7 +2,7 @@
|
||||
|
||||
// Depedencies
|
||||
const ip = require('ip')
|
||||
const Site = require('./site')
|
||||
const Site = require('./site.js')
|
||||
const Watcher = require('./watcher.js')
|
||||
const Projects = require('./projects.js')
|
||||
const socketIo = require('socket.io')
|
||||
@@ -21,13 +21,9 @@ class App
|
||||
constructor()
|
||||
{
|
||||
this.setConfig()
|
||||
// this.setSite()
|
||||
|
||||
this.socket = socketIo.listen()
|
||||
|
||||
this.projects = new Projects({ socket: this.socket, debug: this.config.debug })
|
||||
this.project = this.projects.createProject(this.config.name)
|
||||
|
||||
this.setSite()
|
||||
this.setSocket()
|
||||
this.setProjects()
|
||||
this.setWatcher()
|
||||
}
|
||||
|
||||
@@ -36,6 +32,9 @@ class App
|
||||
*/
|
||||
setConfig()
|
||||
{
|
||||
const config = {}
|
||||
|
||||
// From arguments
|
||||
const args = require('yargs')
|
||||
.option('debug', {
|
||||
alias: 'd',
|
||||
@@ -81,14 +80,12 @@ class App
|
||||
})
|
||||
.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 === '')
|
||||
{
|
||||
@@ -102,6 +99,9 @@ class App
|
||||
}
|
||||
}
|
||||
|
||||
// Custom
|
||||
config.domain = `http://${ip.address()}${config.port !== 80 ? ':' + config.port : ''}`
|
||||
|
||||
// Save
|
||||
this.config = config
|
||||
}
|
||||
@@ -115,6 +115,100 @@ class App
|
||||
this.site = new Site(this.config)
|
||||
}
|
||||
|
||||
/**
|
||||
* Set socket
|
||||
* Instantiate socket with socket io and associate it with the express server
|
||||
* /app socket is used to communicate between watchers and app (because multiple watchers can co-exist)
|
||||
*/
|
||||
setSocket()
|
||||
{
|
||||
this.sockets = {}
|
||||
this.sockets.main = socketIo.listen(this.site.server)
|
||||
this.sockets.app = this.sockets.main.of('/app')
|
||||
|
||||
console.log('ok')
|
||||
|
||||
this.sockets.app.on('connection', (socket) =>
|
||||
{
|
||||
// Set up
|
||||
let project = null
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
{
|
||||
console.log('socket app'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan)
|
||||
}
|
||||
|
||||
// Start project
|
||||
socket.on('start_project', (data) =>
|
||||
{
|
||||
project = this.projects.createProject(data.name)
|
||||
|
||||
this.site.createProject(project.slug, data.path)
|
||||
|
||||
// Open in browser
|
||||
const url = this.config.domain + '/' + project.slug
|
||||
opener(url)
|
||||
console.log('app'.green.bold + ' - ' + url)
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
{
|
||||
console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
|
||||
}
|
||||
})
|
||||
|
||||
// Update file
|
||||
socket.on('update_file', (data) =>
|
||||
{
|
||||
console.log(data)
|
||||
project.files.createVersion(data.path, data.content)
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
{
|
||||
console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
|
||||
}
|
||||
})
|
||||
|
||||
// Create file
|
||||
socket.on('create_file', (data) =>
|
||||
{
|
||||
project.files.create(data.path, data.content)
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
{
|
||||
console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
|
||||
}
|
||||
})
|
||||
|
||||
// Delete file
|
||||
socket.on('delete_file', (data) =>
|
||||
{
|
||||
project.files.delete(data.path)
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
{
|
||||
console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
|
||||
}
|
||||
})
|
||||
|
||||
// Disconnect
|
||||
socket.on('disconnect', () =>
|
||||
{
|
||||
this.projects.deleteProject(project.slug)
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
{
|
||||
console.log('socket app'.green.bold + ' - ' + 'disconnect'.cyan + ' - ' + socket.id.cyan)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Set watcher
|
||||
* Instantiate watcher
|
||||
@@ -122,68 +216,14 @@ class App
|
||||
setWatcher()
|
||||
{
|
||||
this.watcher = new Watcher(this.config)
|
||||
}
|
||||
|
||||
const url = this.config.domain + '/project/' + this.project.slug
|
||||
|
||||
console.log('server'.green.bold + ' - ' + url.cyan)
|
||||
|
||||
// Open in browser
|
||||
opener(url)
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
{
|
||||
console.log(util.inspect(this.project.files.describe(), { depth: null, colors: true }))
|
||||
}
|
||||
|
||||
// Update file
|
||||
this.watcher.on('update_file', (data) =>
|
||||
{
|
||||
console.log(data)
|
||||
this.project.files.createVersion(data.path, data.content)
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
{
|
||||
console.log(util.inspect(this.project.files.describe(), { depth: null, colors: true }))
|
||||
}
|
||||
})
|
||||
|
||||
// Create file
|
||||
this.watcher.on('create_file', (data) =>
|
||||
{
|
||||
this.project.files.create(data.path, data.content)
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
{
|
||||
console.log(util.inspect(this.project.files.describe(), { depth: null, colors: true }))
|
||||
}
|
||||
})
|
||||
|
||||
// Delete file
|
||||
this.watcher.on('delete_file', (data) =>
|
||||
{
|
||||
this.project.files.delete(data.path)
|
||||
|
||||
// Debug
|
||||
if(this.config.debug)
|
||||
{
|
||||
console.log(util.inspect(this.project.files.describe(), { depth: null, colors: true }))
|
||||
}
|
||||
})
|
||||
|
||||
// // Disconnect
|
||||
// this.watcher.on('disconnect', () =>
|
||||
// {
|
||||
// this.projects.delete_project(this.project.slug)
|
||||
|
||||
// // Debug
|
||||
// if(this.config.debug)
|
||||
// {
|
||||
// console.log('socket app'.green.bold + ' - ' + 'disconnect'.cyan + ' - ' + socket.id.cyan)
|
||||
// }
|
||||
// })
|
||||
/**
|
||||
* Set projects
|
||||
*/
|
||||
setProjects()
|
||||
{
|
||||
this.projects = new Projects({ socket: this.sockets.main, debug: this.config.debug })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user