🎨 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.directory = _options.path
this.path.full = this.path.directory + '/' + this.name this.path.full = this.path.directory + '/' + this.name
this.versions = [] this.versions = []
this.socket = _options.socket this.projectSocket = _options.projectSocket
const nameParts = this.name.split('.') const nameParts = this.name.split('.')
@@ -75,7 +75,7 @@ class File
} }
// Emit // Emit
this.socket.emit('createVersion', { file: this.path.full, version }) this.projectSocket.emit('createVersion', { file: this.path.full, version })
// Save // Save
this.versions.push(version) this.versions.push(version)
+11 -9
View File
@@ -6,11 +6,13 @@ const File = require('./file.js')
class Files class Files
{ {
constructor(_options) constructor(_config, _options)
{ {
this.items = {} this.config = _config
this.count = 0
this.socket = _options.socket this.items = {}
this.count = 0
this.projectSocket = _options.projectSocket
this.lastVersionDate = new Date() this.lastVersionDate = new Date()
} }
@@ -31,10 +33,10 @@ class Files
// Create // Create
file = new File({ file = new File({
name : parsedPath.base, name: parsedPath.base,
path : parsedPath.dir, path: parsedPath.dir,
content: _content, content: _content,
socket : this.socket projectSocket: this.projectSocket
}) })
// Save // Save
@@ -43,7 +45,7 @@ class Files
this.lastVersionDate = new Date() this.lastVersionDate = new Date()
// Emit // Emit
this.socket.emit('create_file', file.describe()) this.projectSocket.emit('create_file', file.describe())
return file return file
} }
@@ -115,7 +117,7 @@ class Files
this.lastVersionDate = new Date() this.lastVersionDate = new Date()
// Emit // Emit
this.socket.emit('delete_file', file.describe()) this.projectSocket.emit('delete_file', file.describe())
return true return true
} }
+67 -35
View File
@@ -20,10 +20,27 @@ class App
*/ */
constructor() constructor()
{ {
console.log()
console.log('---'.rainbow + 'keppler'.bold.white + '----------------------'.rainbow)
console.log()
this.setConfig() this.setConfig()
this.setSite() this.setSite(
this.setSocket() () =>
this.setProjects() {
this.setSocket()
this.setProjects()
},
() =>
{
console.log('app'.green.bold + ' - ' + 'keppler seems to be running already'.cyan)
},
() =>
{
console.log('error'.red)
}
)
this.setWatcher() this.setWatcher()
} }
@@ -38,9 +55,9 @@ class App
const args = require('yargs') const args = require('yargs')
.option('debug', { .option('debug', {
alias: 'd', alias: 'd',
describe: 'Use debug mode', describe: 'Debug level',
default: false, default: 1,
type: 'boolean' type: 'number'
}) })
.option('name', { .option('name', {
alias: 'n', alias: 'n',
@@ -110,9 +127,9 @@ class App
* Set site * Set site
* Instantiate 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() setSocket()
{ {
this.sockets = {} this.config.socket = socketIo.listen(this.site.server)
this.sockets.main = socketIo.listen(this.site.server)
this.sockets.app = this.sockets.main.of('/app')
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 // Set up
let project = null let project = null
// Debug // 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 // Start project
@@ -146,26 +162,34 @@ class App
this.site.createProject(project.slug, data.path) 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 // 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 })) 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 // Update file
socket.on('update_file', (data) => socket.on('update_file', (data) =>
{ {
console.log(data)
project.files.createVersion(data.path, data.content) project.files.createVersion(data.path, data.content)
// Debug // 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 })) console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
} }
@@ -177,7 +201,11 @@ class App
project.files.create(data.path, data.content) project.files.create(data.path, data.content)
// Debug // 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 })) console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
} }
@@ -189,7 +217,11 @@ class App
project.files.delete(data.path) project.files.delete(data.path)
// Debug // 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 })) console.log(util.inspect(project.files.describe(), { depth: null, colors: true }))
} }
@@ -201,14 +233,22 @@ class App
this.projects.deleteProject(project.slug) this.projects.deleteProject(project.slug)
// Debug // 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 * Set watcher
* Instantiate watcher * Instantiate watcher
@@ -217,14 +257,6 @@ class App
{ {
this.watcher = new Watcher(this.config) this.watcher = new Watcher(this.config)
} }
/**
* Set projects
*/
setProjects()
{
this.projects = new Projects({ socket: this.sockets.main, debug: this.config.debug })
}
} }
module.exports = App module.exports = App
+13 -27
View File
@@ -6,51 +6,37 @@ const Files = require('./files.js')
class Project class Project
{ {
constructor(_options) constructor(_config, _options)
{ {
this.setOptions(_options) this.config = _config
this.setName(_options.name) 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() this.date = new Date()
} }
/**
* Set options
*/
setOptions(_options)
{
if(typeof _options.debug === 'undefined')
{
_options.debug = false
}
// Save
this.options = _options
}
setName(_name) setName(_name)
{ {
this.name = _name this.name = _name
this.slug = slug(this.name, { lower: true }) this.slug = slug(this.name, { lower: true })
} }
setSocket(socket) setSocket()
{ {
// Set up // Save original socket and create a channel for this specific project
this.originalSocket = socket this.projectSocket = this.config.socket.of('/project/' + this.slug)
this.socket = this.originalSocket.of('/project/' + this.slug)
// Connection event // 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 // 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() destructor()
{ {
this.socket.emit('destruct') this.projectSocket.emit('destruct')
} }
} }
+13 -28
View File
@@ -4,42 +4,27 @@ const Project = require('./project.js')
class Projects class Projects
{ {
constructor(_options) constructor(_config)
{ {
this.config = _config
this.all = {} this.all = {}
this.setOptions(_options) this.setSocket()
this.setSocket(_options.socket)
} }
/** setSocket()
* Set options
*/
setOptions(_options)
{ {
if(typeof _options.debug === 'undefined') // Save original socket and create a channel for projects
{ this.projectsSocket = this.config.socket.of('/projects')
_options.debug = false
}
// Save
this.options = _options
}
setSocket(socket)
{
// Set up
this.originalSocket = socket
this.socket = this.originalSocket.of('/projects')
// Connection event // 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) createProject(_name)
{ {
// Create project // 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 ] let sameNameProject = this.all[ project.slug ]
// Try to found same name project // Try to found same name project
@@ -75,7 +60,7 @@ class Projects
this.all[ project.slug ] = project this.all[ project.slug ] = project
// Emit // Emit
this.socket.emit('update_projects', this.describe()) this.projectsSocket.emit('update_projects', this.describe())
// Return // Return
return project return project
@@ -94,7 +79,7 @@ class Projects
project.destructor() project.destructor()
// Emit // Emit
this.socket.emit('update_projects', this.describe()) this.projectsSocket.emit('update_projects', this.describe())
} }
} }
+34 -111
View File
@@ -7,9 +7,7 @@ const http = require('http')
const colors = require('colors') const colors = require('colors')
const util = require('util') const util = require('util')
const path = require('path') const path = require('path')
const Projects = require('./projects.js')
const fs = require('fs') const fs = require('fs')
const opener = require('opener')
/** /**
* Site class * Site class
@@ -19,14 +17,15 @@ class Site
/** /**
* Constructor * Constructor
*/ */
constructor(_config) constructor(_config, _success, _alreadyRunning, _error)
{ {
this.config = _config this.config = _config
this.success = _success
this.alreadyRunning = _alreadyRunning
this.error = _error
this.setExpress() this.setExpress()
this.setServer() this.setServer()
// this.setSocket()
// this.setModels()
// this.setDummy() // this.setDummy()
} }
@@ -145,15 +144,6 @@ class Site
// // console.log(util.inspect(project.files.describe(), { depth: null, colors: true })) // // 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 * Set express
* Start express and set controllers * Start express and set controllers
@@ -181,24 +171,48 @@ class Site
// Server already running // Server already running
if(error.code === 'EADDRINUSE') if(error.code === 'EADDRINUSE')
{ {
// Debug if(this.config.debug >= 1)
if(this.config.debug)
{ {
console.log('site already running'.green.bold) console.log('site'.green.bold + ' - ' + 'already running'.cyan)
}
// Callback
if(typeof this.alreadyRunning === 'function')
{
this.alreadyRunning()
} }
return 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 // Start
this.server.listen(this.config.port, () => this.server.listen(this.config.port, () =>
{ {
// URL // URL
console.log(colors.green('---------------------------')) if(this.config.debug >= 1)
console.log('site'.green.bold + ' - ' + 'started'.cyan) {
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)) 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 module.exports = Site
+32 -17
View File
@@ -28,8 +28,8 @@ class Watcher
*/ */
setSocket() setSocket()
{ {
// Set up // Connect to watcher socket
this.socket = socketIoClient(`${this.config.domain}/app`) this.socket = socketIoClient(`${this.config.domain}/watcher`)
// Connect event // Connect event
this.socket.on('connect', () => this.socket.on('connect', () =>
@@ -39,7 +39,17 @@ class Watcher
// Debug // Debug
if(this.config.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)
} }
}) })
} }
@@ -51,7 +61,7 @@ class Watcher
watch() watch()
{ {
// Create ignored regex // Create ignored regex
const regexs = [] const regexs = []
const Minimatch = require('minimatch').Minimatch const Minimatch = require('minimatch').Minimatch
for(const _excludeKey in this.config.exclude) for(const _excludeKey in this.config.exclude)
@@ -73,12 +83,17 @@ class Watcher
this.watcher = chokidar.watch( this.watcher = chokidar.watch(
this.path, this.path,
{ {
// ignored : /[\/\\]\./, // ignored: /[\/\\]\./,
ignored : regex, ignored: regex,
ignoreInitial: true ignoreInitial: true
} }
) )
if(this.config.debug >= 1)
{
console.log('watcher'.green.bold + ' - ' + 'start watching'.cyan + ' - ' + this.path.cyan)
}
// Add event // Add event
this.watcher.on('add', (_path) => this.watcher.on('add', (_path) =>
{ {
@@ -119,9 +134,9 @@ class Watcher
}) })
// Debug // 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 // 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, '.') const relativePath = _path.replace(this.path, '.')
// Emit // Emit
this.socket.emit('delete_file', relativePath) this.socket.emit('delete_file', { path: relativePath })
// Debug // 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 }) this.socket.emit('create_folder', { path: relativePath })
// Debug // 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 }) this.socket.emit('delete_folder', { path: relativePath })
// Debug // 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", "main": "bin/index.js",
"scripts": { "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": { "repository": {
"type": "git", "type": "git",