🎨 Finish restructuring app

This commit is contained in:
brunosimon
2017-07-30 01:53:52 +02:00
parent fd18099109
commit ef8d9279af
8 changed files with 173 additions and 230 deletions
+67 -35
View File
@@ -20,10 +20,27 @@ class App
*/
constructor()
{
console.log()
console.log('---'.rainbow + 'keppler'.bold.white + '----------------------'.rainbow)
console.log()
this.setConfig()
this.setSite()
this.setSocket()
this.setProjects()
this.setSite(
() =>
{
this.setSocket()
this.setProjects()
},
() =>
{
console.log('app'.green.bold + ' - ' + 'keppler seems to be running already'.cyan)
},
() =>
{
console.log('error'.red)
}
)
this.setWatcher()
}
@@ -38,9 +55,9 @@ class App
const args = require('yargs')
.option('debug', {
alias: 'd',
describe: 'Use debug mode',
default: false,
type: 'boolean'
describe: 'Debug level',
default: 1,
type: 'number'
})
.option('name', {
alias: 'n',
@@ -110,9 +127,9 @@ class App
* Set site
* Instantiate site
*/
setSite()
setSite(_success, _error)
{
this.site = new Site(this.config)
this.site = new Site(this.config, _success, _error)
}
/**
@@ -122,21 +139,20 @@ class App
*/
setSocket()
{
this.sockets = {}
this.sockets.main = socketIo.listen(this.site.server)
this.sockets.app = this.sockets.main.of('/app')
this.config.socket = socketIo.listen(this.site.server)
console.log('ok')
// Create a socket for the watcher
const watcherSocket = this.config.socket.of('/watcher')
this.sockets.app.on('connection', (socket) =>
watcherSocket.on('connection', (socket) =>
{
// Set up
let project = null
// Debug
if(this.config.debug)
if(this.config.debug >= 1)
{
console.log('socket app'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan)
console.log('app > socket'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan)
}
// Start project
@@ -146,26 +162,34 @@ class App
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)
if(this.config.debug >= 1)
{
console.log('app > socket'.green.bold + ' - ' + 'start_project'.cyan + ' - ' + project.slug.cyan)
}
if(this.config.debug >= 2)
{
console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
}
// Open in browser
const url = this.config.domain + '/' + project.slug
opener(url)
console.log('app'.green.bold + ' - ' + url.yellow)
})
// Update file
socket.on('update_file', (data) =>
{
console.log(data)
project.files.createVersion(data.path, data.content)
// Debug
if(this.config.debug)
if(this.config.debug >= 1)
{
console.log('app > socket'.green.bold + ' - ' + 'update_file'.cyan + ' - ' + data.path.cyan)
}
if(this.config.debug >= 2)
{
console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
}
@@ -177,7 +201,11 @@ class App
project.files.create(data.path, data.content)
// Debug
if(this.config.debug)
if(this.config.debug >= 1)
{
console.log('app > socket'.green.bold + ' - ' + 'update_file'.cyan + ' - ' + data.path.cyan)
}
if(this.config.debug >= 2)
{
console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
}
@@ -189,7 +217,11 @@ class App
project.files.delete(data.path)
// Debug
if(this.config.debug)
if(this.config.debug >= 1)
{
console.log('app > socket'.green.bold + ' - ' + 'update_file'.cyan + ' - ' + data.path.cyan)
}
if(this.config.debug >= 2)
{
console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
}
@@ -201,14 +233,22 @@ class App
this.projects.deleteProject(project.slug)
// Debug
if(this.config.debug)
if(this.config.debug >= 1)
{
console.log('socket app'.green.bold + ' - ' + 'disconnect'.cyan + ' - ' + socket.id.cyan)
console.log('app > socket'.green.bold + ' - ' + 'disconnect'.cyan + ' - ' + socket.id.cyan)
}
})
})
}
/**
* Set projects
*/
setProjects()
{
this.projects = new Projects(this.config)
}
/**
* Set watcher
* Instantiate watcher
@@ -217,14 +257,6 @@ class App
{
this.watcher = new Watcher(this.config)
}
/**
* Set projects
*/
setProjects()
{
this.projects = new Projects({ socket: this.sockets.main, debug: this.config.debug })
}
}
module.exports = App