🎨 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
+2 -2
View File
@@ -15,7 +15,7 @@ class File
this.path.directory = _options.path
this.path.full = this.path.directory + '/' + this.name
this.versions = []
this.socket = _options.socket
this.projectSocket = _options.projectSocket
const nameParts = this.name.split('.')
@@ -75,7 +75,7 @@ class File
}
// Emit
this.socket.emit('createVersion', { file: this.path.full, version })
this.projectSocket.emit('createVersion', { file: this.path.full, version })
// Save
this.versions.push(version)
+9 -7
View File
@@ -6,11 +6,13 @@ const File = require('./file.js')
class Files
{
constructor(_options)
constructor(_config, _options)
{
this.config = _config
this.items = {}
this.count = 0
this.socket = _options.socket
this.projectSocket = _options.projectSocket
this.lastVersionDate = new Date()
}
@@ -31,10 +33,10 @@ class Files
// Create
file = new File({
name : parsedPath.base,
path : parsedPath.dir,
name: parsedPath.base,
path: parsedPath.dir,
content: _content,
socket : this.socket
projectSocket: this.projectSocket
})
// Save
@@ -43,7 +45,7 @@ class Files
this.lastVersionDate = new Date()
// Emit
this.socket.emit('create_file', file.describe())
this.projectSocket.emit('create_file', file.describe())
return file
}
@@ -115,7 +117,7 @@ class Files
this.lastVersionDate = new Date()
// Emit
this.socket.emit('delete_file', file.describe())
this.projectSocket.emit('delete_file', file.describe())
return true
}
+65 -33
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.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
+13 -27
View File
@@ -6,51 +6,37 @@ const Files = require('./files.js')
class Project
{
constructor(_options)
constructor(_config, _options)
{
this.setOptions(_options)
this.config = _config
this.setName(_options.name)
this.setSocket(_options.socket)
this.setSocket()
this.files = new Files({ socket: this.socket })
this.files = new Files(this.config, { projectSocket: this.projectSocket })
this.date = new Date()
}
/**
* Set options
*/
setOptions(_options)
{
if(typeof _options.debug === 'undefined')
{
_options.debug = false
}
// Save
this.options = _options
}
setName(_name)
{
this.name = _name
this.slug = slug(this.name, { lower: true })
}
setSocket(socket)
setSocket()
{
// Set up
this.originalSocket = socket
this.socket = this.originalSocket.of('/project/' + this.slug)
// Save original socket and create a channel for this specific project
this.projectSocket = this.config.socket.of('/project/' + this.slug)
// Connection event
this.socket.on('connection', (socket) =>
this.projectSocket.on('connection', (socket) =>
{
this.socket.emit('update_project', this.describe())
this.projectSocket.emit('update_project', this.describe())
// Debug
if(this.options.debug)
if(this.config.debug >= 1)
{
console.log('socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan)
console.log('project > socket'.green.bold + ' - ' + 'connection'.cyan + ' - ' + socket.id.cyan)
}
})
}
@@ -69,7 +55,7 @@ class Project
destructor()
{
this.socket.emit('destruct')
this.projectSocket.emit('destruct')
}
}
+13 -28
View File
@@ -4,42 +4,27 @@ const Project = require('./project.js')
class Projects
{
constructor(_options)
constructor(_config)
{
this.config = _config
this.all = {}
this.setOptions(_options)
this.setSocket(_options.socket)
this.setSocket()
}
/**
* Set options
*/
setOptions(_options)
setSocket()
{
if(typeof _options.debug === 'undefined')
{
_options.debug = false
}
// Save
this.options = _options
}
setSocket(socket)
{
// Set up
this.originalSocket = socket
this.socket = this.originalSocket.of('/projects')
// Save original socket and create a channel for projects
this.projectsSocket = this.config.socket.of('/projects')
// Connection event
this.socket.on('connection', (socket) =>
this.projectsSocket.on('connection', (socket) =>
{
this.socket.emit('update_projects', this.describe())
this.projectsSocket.emit('update_projects', this.describe())
if(this.options.debug)
if(this.config.debug >= 1)
{
console.log('socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan)
console.log('projects > socket'.green.bold + ' - ' + 'connection'.cyan + ' - ' + socket.id.cyan)
}
})
}
@@ -47,7 +32,7 @@ class Projects
createProject(_name)
{
// Create project
const project = new Project({ name: _name, socket: this.originalSocket, debug: this.options.debug })
const project = new Project(this.config, { name: _name })
let sameNameProject = this.all[ project.slug ]
// Try to found same name project
@@ -75,7 +60,7 @@ class Projects
this.all[ project.slug ] = project
// Emit
this.socket.emit('update_projects', this.describe())
this.projectsSocket.emit('update_projects', this.describe())
// Return
return project
@@ -94,7 +79,7 @@ class Projects
project.destructor()
// Emit
this.socket.emit('update_projects', this.describe())
this.projectsSocket.emit('update_projects', this.describe())
}
}
+33 -110
View File
@@ -7,9 +7,7 @@ const http = require('http')
const colors = require('colors')
const util = require('util')
const path = require('path')
const Projects = require('./projects.js')
const fs = require('fs')
const opener = require('opener')
/**
* Site class
@@ -19,14 +17,15 @@ class Site
/**
* Constructor
*/
constructor(_config)
constructor(_config, _success, _alreadyRunning, _error)
{
this.config = _config
this.success = _success
this.alreadyRunning = _alreadyRunning
this.error = _error
this.setExpress()
this.setServer()
// this.setSocket()
// this.setModels()
// this.setDummy()
}
@@ -145,15 +144,6 @@ class Site
// // console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
// }
// /**
// * Set models
// */
// setModels()
// {
// // Set up
// this.projects = new Projects({ socket: this.sockets.main, debug: this.config.debug })
// }
/**
* Set express
* Start express and set controllers
@@ -181,24 +171,48 @@ class Site
// Server already running
if(error.code === 'EADDRINUSE')
{
// Debug
if(this.config.debug)
if(this.config.debug >= 1)
{
console.log('site already running'.green.bold)
console.log('site'.green.bold + ' - ' + 'already running'.cyan)
}
// Callback
if(typeof this.alreadyRunning === 'function')
{
this.alreadyRunning()
}
return
}
else
{
if(this.config.debug >= 1)
{
console.log('site'.green.bold + ' - ' + 'unknow error'.cyan)
console.log(error)
}
console.log(error.message)
// Callback
if(typeof this.error === 'function')
{
this.alreadyRunning()
}
}
})
// Start
this.server.listen(this.config.port, () =>
{
// URL
console.log(colors.green('---------------------------'))
if(this.config.debug >= 1)
{
console.log('site'.green.bold + ' - ' + 'started'.cyan)
}
if(typeof this.success === 'function')
{
this.success()
}
})
}
@@ -214,97 +228,6 @@ class Site
this.express.use('/' + _slug + '/files', express.static(_path))
}
// /**
// * Set socket
// */
// setSocket()
// {
// // Set up
// this.sockets = {}
// this.sockets.main = socketIo.listen(this.server)
// this.sockets.app = this.sockets.main.of('/app')
// // App connection event
// 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)
// const url = this.config.url
// console.log('site'.green.bold + ' - ' + url.cyan)
// // Open in browser
// opener(url)
// // Debug
// if(this.config.debug)
// {
// console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
// }
// })
// // Update file
// socket.on('update_file', (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.delete_project(project.slug)
// // Debug
// if(this.config.debug)
// {
// console.log('socket app'.green.bold + ' - ' + 'disconnect'.cyan + ' - ' + socket.id.cyan)
// }
// })
// })
// }
}
module.exports = Site
+31 -16
View File
@@ -28,8 +28,8 @@ class Watcher
*/
setSocket()
{
// Set up
this.socket = socketIoClient(`${this.config.domain}/app`)
// Connect to watcher socket
this.socket = socketIoClient(`${this.config.domain}/watcher`)
// Connect event
this.socket.on('connect', () =>
@@ -39,7 +39,17 @@ class Watcher
// Debug
if(this.config.debug)
{
console.log('connected'.green.bold)
console.log('watcher > socket'.green.bold + ' - ' + 'connect'.cyan)
}
})
// Disconnect event
this.socket.on('disconnect', () =>
{
// Debug
if(this.config.debug)
{
console.log('watcher > socket'.green.bold + ' - ' + 'disconnect'.cyan)
}
})
}
@@ -73,12 +83,17 @@ class Watcher
this.watcher = chokidar.watch(
this.path,
{
// ignored : /[\/\\]\./,
ignored : regex,
// ignored: /[\/\\]\./,
ignored: regex,
ignoreInitial: true
}
)
if(this.config.debug >= 1)
{
console.log('watcher'.green.bold + ' - ' + 'start watching'.cyan + ' - ' + this.path.cyan)
}
// Add event
this.watcher.on('add', (_path) =>
{
@@ -119,9 +134,9 @@ class Watcher
})
// Debug
if(this.config.debug)
if(this.config.debug >= 1)
{
console.log('add:'.green.bold, relativePath)
console.log('watcher'.green.bold + ' - ' + 'add'.cyan + ' - ' + relativePath.cyan)
}
})
@@ -165,9 +180,9 @@ class Watcher
})
// Debug
if(this.config.debug)
if(this.config.debug >= 1)
{
console.log('change:'.green.bold, relativePath)
console.log('watcher'.green.bold + ' - ' + 'change'.cyan + ' - ' + relativePath.cyan)
}
})
@@ -178,12 +193,12 @@ class Watcher
const relativePath = _path.replace(this.path, '.')
// Emit
this.socket.emit('delete_file', relativePath)
this.socket.emit('delete_file', { path: relativePath })
// Debug
if(this.config.debug)
if(this.config.debug >= 1)
{
console.log('unlink:'.green.bold, relativePath)
console.log('watcher'.green.bold + ' - ' + 'unlink'.cyan + ' - ' + relativePath.cyan)
}
})
@@ -197,9 +212,9 @@ class Watcher
this.socket.emit('create_folder', { path: relativePath })
// Debug
if(this.config.debug)
if(this.config.debug >= 1)
{
console.log('addDir:'.green.bold, relativePath)
console.log('watcher'.green.bold + ' - ' + 'addDir'.cyan + ' - ' + relativePath.cyan)
}
})
@@ -213,9 +228,9 @@ class Watcher
this.socket.emit('delete_folder', { path: relativePath })
// Debug
if(this.config.debug)
if(this.config.debug >= 1)
{
console.log('unlinkDir:'.green.bold, relativePath)
console.log('watcher'.green.bold + ' - ' + 'unlinkDir'.cyan + ' - ' + relativePath.cyan)
}
})
}
+1 -1
View File
@@ -7,7 +7,7 @@
},
"main": "bin/index.js",
"scripts": {
"demo-folder": "cd test/demo-folder && node ../../bin 'Demo folder' -d"
"demo-folder": "cd test/demo-folder && node ../../bin 'Demo folder' --debug 1"
},
"repository": {
"type": "git",