:zap:🚧 Remove internal socket and improve general structure
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
const express = require('express')
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/', function(request, response)
|
||||
{
|
||||
response.render('pages/index/projects.pug', {})
|
||||
})
|
||||
|
||||
router.get(/project\/(.+)/, function(request, response)
|
||||
{
|
||||
response.render(
|
||||
'pages/index/project.pug',
|
||||
{
|
||||
projectSlug: request.params['0']
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -9,7 +9,7 @@ const colors = require('colors')
|
||||
const ip = require('ip')
|
||||
const util = require('util')
|
||||
const path = require('path')
|
||||
const Projects = require('./models/projects.js')
|
||||
const Projects = require('../projects.js')
|
||||
const fs = require('fs')
|
||||
const opener = require('opener')
|
||||
|
||||
|
||||
@@ -1,224 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
class EventEmitter
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
constructor()
|
||||
{
|
||||
this.callbacks = {}
|
||||
this.callbacks.base = {}
|
||||
}
|
||||
|
||||
/**
|
||||
* On
|
||||
*/
|
||||
on(_names, callback)
|
||||
{
|
||||
const that = this
|
||||
|
||||
// Errors
|
||||
if(typeof _names === 'undefined' || _names === '')
|
||||
{
|
||||
console.warn('wrong names')
|
||||
return false
|
||||
}
|
||||
|
||||
if(typeof callback === 'undefined')
|
||||
{
|
||||
console.warn('wrong callback')
|
||||
return false
|
||||
}
|
||||
|
||||
// Resolve names
|
||||
const names = this.resolveNames(names)
|
||||
|
||||
// Each name
|
||||
names.forEach(function(_name)
|
||||
{
|
||||
// Resolve name
|
||||
const name = that.resolveName(_name)
|
||||
|
||||
// Create namespace if not exist
|
||||
if(!(that.callbacks[ name.namespace ] instanceof Object))
|
||||
that.callbacks[ name.namespace ] = {}
|
||||
|
||||
// Create callback if not exist
|
||||
if(!(that.callbacks[ name.namespace ][ name.value ] instanceof Array))
|
||||
that.callbacks[ name.namespace ][ name.value ] = []
|
||||
|
||||
// Add callback
|
||||
that.callbacks[ name.namespace ][ name.value ].push(callback)
|
||||
})
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Off
|
||||
*/
|
||||
off(_names)
|
||||
{
|
||||
const that = this
|
||||
|
||||
// Errors
|
||||
if(typeof _names === 'undefined' || _names === '')
|
||||
{
|
||||
console.warn('wrong name')
|
||||
return false
|
||||
}
|
||||
|
||||
// Resolve names
|
||||
const names = this.resolveNames(_names)
|
||||
|
||||
// Each name
|
||||
names.forEach(function(_name)
|
||||
{
|
||||
// Resolve name
|
||||
const name = that.resolveName(_name)
|
||||
|
||||
// Remove namespace
|
||||
if(name.namespace !== 'base' && name.value === '')
|
||||
{
|
||||
delete that.callbacks[ name.namespace ]
|
||||
}
|
||||
|
||||
// Remove specific callback in namespace
|
||||
else
|
||||
{
|
||||
// Default
|
||||
if(name.namespace === 'base')
|
||||
{
|
||||
// Try to remove from each namespace
|
||||
for(const namespace in that.callbacks)
|
||||
{
|
||||
if(that.callbacks[ namespace ] instanceof Object && that.callbacks[ namespace ][ name.value ] instanceof Array)
|
||||
{
|
||||
delete that.callbacks[ namespace ][ name.value ]
|
||||
|
||||
// Remove namespace if empty
|
||||
if(Object.keys(that.callbacks[ namespace ]).length === 0)
|
||||
delete that.callbacks[ namespace ]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Specified namespace
|
||||
else if(that.callbacks[ name.namespace ] instanceof Object && that.callbacks[ name.namespace ][ name.value ] instanceof Array)
|
||||
{
|
||||
delete that.callbacks[ name.namespace ][ name.value ]
|
||||
|
||||
// Remove namespace if empty
|
||||
if(Object.keys(that.callbacks[ name.namespace ]).length === 0)
|
||||
delete that.callbacks[ name.namespace ]
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger
|
||||
*/
|
||||
trigger(_name, _args)
|
||||
{
|
||||
// Errors
|
||||
if(typeof _name === 'undefined' || _name === '')
|
||||
{
|
||||
console.warn('wrong name')
|
||||
return false
|
||||
}
|
||||
|
||||
const that = this
|
||||
let finalResult = null
|
||||
let result = null
|
||||
|
||||
// Default args
|
||||
const args = !(args instanceof Array) ? [] : _args
|
||||
|
||||
// Resolve names (should on have one event)
|
||||
let name = this.resolveNames(_name)
|
||||
|
||||
// Resolve name
|
||||
name = this.resolveName(name[ 0 ])
|
||||
|
||||
// Default namespace
|
||||
if(name.namespace === 'base')
|
||||
{
|
||||
// Try to find callback in each namespace
|
||||
for(const namespace in that.callbacks)
|
||||
{
|
||||
if(that.callbacks[ namespace ] instanceof Object && that.callbacks[ namespace ][ name.value ] instanceof Array)
|
||||
{
|
||||
that.callbacks[ namespace ][ name.value ].forEach(function(callback)
|
||||
{
|
||||
result = callback.apply(that,args)
|
||||
|
||||
if(typeof finalResult === 'undefined')
|
||||
{
|
||||
finalResult = result
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Specified namespace
|
||||
else if(this.callbacks[ name.namespace ] instanceof Object)
|
||||
{
|
||||
if(name.value === '')
|
||||
{
|
||||
console.warn('wrong name')
|
||||
return this
|
||||
}
|
||||
|
||||
that.callbacks[ name.namespace ][ name.value ].forEach(function(callback)
|
||||
{
|
||||
result = callback.apply(that, args)
|
||||
|
||||
if(typeof finalResult === 'undefined')
|
||||
finalResult = result
|
||||
})
|
||||
}
|
||||
|
||||
return finalResult
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve names
|
||||
*/
|
||||
resolveNames(_names)
|
||||
{
|
||||
let names = _names
|
||||
names = names.replace(/[^a-zA-Z0-9 ,\/.]/g, '')
|
||||
names = names.replace(/[,\/]+/g, ' ')
|
||||
names = names.split(' ')
|
||||
|
||||
return names
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve name
|
||||
*/
|
||||
resolveName(name)
|
||||
{
|
||||
const newName = {}
|
||||
const parts = name.split('.')
|
||||
|
||||
newName.original = name
|
||||
newName.value = parts[ 0 ]
|
||||
newName.namespace = 'base' // Base namespace
|
||||
|
||||
// Specified namespace
|
||||
if(parts.length > 1 && parts[ 1 ] !== '')
|
||||
{
|
||||
newName.namespace = parts[ 1 ]
|
||||
}
|
||||
|
||||
return newName
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EventEmitter
|
||||
@@ -1,112 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
// Depedencies
|
||||
const diff = require('diff')
|
||||
const ids = require('../utils/ids.js')
|
||||
|
||||
class File
|
||||
{
|
||||
constructor(_options)
|
||||
{
|
||||
// Set up
|
||||
this.id = ids.getId()
|
||||
this.name = _options.name
|
||||
this.path = {}
|
||||
this.path.directory = _options.path
|
||||
this.path.full = this.path.directory + '/' + this.name
|
||||
this.versions = []
|
||||
this.socket = _options.socket
|
||||
|
||||
const nameParts = this.name.split('.')
|
||||
|
||||
if(nameParts.length > 1)
|
||||
{
|
||||
this.extension = nameParts[ nameParts.length - 1 ]
|
||||
}
|
||||
else
|
||||
{
|
||||
this.extension = ''
|
||||
}
|
||||
|
||||
// Create first version
|
||||
if(typeof _options.content !== 'undefined')
|
||||
{
|
||||
this.createVersion(_options.content)
|
||||
}
|
||||
}
|
||||
|
||||
createVersion(content)
|
||||
{
|
||||
// Create version
|
||||
const version = {}
|
||||
const lastVersion = this.getLastVersion()
|
||||
|
||||
version.date = new Date()
|
||||
version.content = content
|
||||
|
||||
if(!lastVersion)
|
||||
{
|
||||
version.diff = false
|
||||
}
|
||||
else if(version.content === '')
|
||||
{
|
||||
version.diff = [{ count: 1, added: true, removed: undefined, value: 'a' }]
|
||||
|
||||
if(lastVersion.content === '')
|
||||
{
|
||||
return false
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
version.diff = diff.diffLines(
|
||||
lastVersion.content,
|
||||
version.content,
|
||||
{
|
||||
// ignoreWhitespace: true
|
||||
}
|
||||
)
|
||||
|
||||
// No changed
|
||||
if(version.diff.length === 1 && lastVersion.content !== '')
|
||||
{
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Emit
|
||||
this.socket.emit('createVersion', { file: this.path.full, version })
|
||||
|
||||
// Save
|
||||
this.versions.push(version)
|
||||
}
|
||||
|
||||
getLastVersion()
|
||||
{
|
||||
if(this.versions.length === 0)
|
||||
return false
|
||||
|
||||
return this.versions[ this.versions.length - 1 ]
|
||||
}
|
||||
|
||||
describe()
|
||||
{
|
||||
// Set up
|
||||
const result = {}
|
||||
|
||||
result.id = this.id
|
||||
result.name = this.name
|
||||
result.path = this.path
|
||||
result.versions = this.versions
|
||||
result.extension = this.extension
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
destructor()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = File
|
||||
@@ -1,146 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
// Dependencies
|
||||
const paths = require('../utils/paths.js')
|
||||
const File = require('./file.js')
|
||||
|
||||
class Files
|
||||
{
|
||||
constructor(_options)
|
||||
{
|
||||
this.items = {}
|
||||
this.count = 0
|
||||
this.socket = _options.socket
|
||||
this.lastVersionDate = new Date()
|
||||
}
|
||||
|
||||
create(_path, _content)
|
||||
{
|
||||
// Set up
|
||||
const normalizedPath = paths.normalize(_path)
|
||||
const parsedPath = paths.parse(normalizedPath)
|
||||
|
||||
// Retrieve file
|
||||
let file = this.get(normalizedPath)
|
||||
|
||||
// File already exist
|
||||
if(file)
|
||||
{
|
||||
return false
|
||||
}
|
||||
|
||||
// Create
|
||||
file = new File({
|
||||
name : parsedPath.base,
|
||||
path : parsedPath.dir,
|
||||
content: _content,
|
||||
socket : this.socket
|
||||
})
|
||||
|
||||
// Save
|
||||
this.items[ normalizedPath ] = file
|
||||
this.count++
|
||||
this.lastVersionDate = new Date()
|
||||
|
||||
// Emit
|
||||
this.socket.emit('create_file', file.describe())
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
createVersion(_path, _content)
|
||||
{
|
||||
// Set up
|
||||
const normalizedPath = paths.normalize(_path)
|
||||
|
||||
// Retrieve file
|
||||
const file = this.get(normalizedPath, true)
|
||||
|
||||
if(typeof _content !== 'undefined')
|
||||
{
|
||||
// Create version
|
||||
file.createVersion(_content)
|
||||
}
|
||||
|
||||
// Save
|
||||
this.lastVersionDate = new Date()
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
get(_path, _forceCreation)
|
||||
{
|
||||
const forceCreation = typeof _forceCreation === 'undefined' ? false : _forceCreation
|
||||
|
||||
// Set up
|
||||
const normalizedPath = paths.normalize(_path)
|
||||
|
||||
// Retrieve file
|
||||
let file = this.items[ normalizedPath ]
|
||||
|
||||
// File found
|
||||
if(typeof file !== 'undefined')
|
||||
{
|
||||
return file
|
||||
}
|
||||
|
||||
// Force creation
|
||||
if(forceCreation)
|
||||
{
|
||||
file = this.create(normalizedPath)
|
||||
return file
|
||||
}
|
||||
|
||||
// Not found
|
||||
return false
|
||||
}
|
||||
|
||||
delete(_path)
|
||||
{
|
||||
// Set up
|
||||
const normalizedPath = paths.normalize(_path)
|
||||
|
||||
// Retrieve file
|
||||
const file = this.get(normalizedPath)
|
||||
|
||||
// File found
|
||||
if(file)
|
||||
{
|
||||
// Delete file
|
||||
file.destructor()
|
||||
delete this.items[ normalizedPath ]
|
||||
this.count--
|
||||
|
||||
// Save
|
||||
this.lastVersionDate = new Date()
|
||||
|
||||
// Emit
|
||||
this.socket.emit('delete_file', file.describe())
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
describe()
|
||||
{
|
||||
// Set up
|
||||
const result = {}
|
||||
|
||||
result.count = this.count
|
||||
result.items = {}
|
||||
|
||||
// Each file
|
||||
for(const _filePath in this.items)
|
||||
{
|
||||
const file = this.items[_filePath]
|
||||
|
||||
result.items[_filePath] = file.describe()
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Files
|
||||
@@ -1,76 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
// Dependencies
|
||||
const slug = require('slug')
|
||||
const Files = require('./files.js')
|
||||
|
||||
class Project
|
||||
{
|
||||
constructor(_options)
|
||||
{
|
||||
this.setOptions(_options)
|
||||
this.setName(_options.name)
|
||||
this.setSocket(_options.socket)
|
||||
|
||||
this.files = new Files({ socket: this.socket })
|
||||
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)
|
||||
{
|
||||
// Set up
|
||||
this.originalSocket = socket
|
||||
this.socket = this.originalSocket.of('/project/' + this.slug)
|
||||
|
||||
// Connection event
|
||||
this.socket.on('connection', (socket) =>
|
||||
{
|
||||
this.socket.emit('update_project', this.describe())
|
||||
|
||||
// Debug
|
||||
if(this.options.debug)
|
||||
{
|
||||
console.log('socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
describe()
|
||||
{
|
||||
// Set up
|
||||
const result = {}
|
||||
|
||||
result.name = this.name
|
||||
result.files = this.files.describe()
|
||||
result.date = this.date
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
destructor()
|
||||
{
|
||||
this.socket.emit('destruct')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Project
|
||||
@@ -1,138 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const Project = require('./project.js')
|
||||
|
||||
class Projects
|
||||
{
|
||||
constructor(_options)
|
||||
{
|
||||
this.all = {}
|
||||
|
||||
this.setOptions(_options)
|
||||
this.setSocket(_options.socket)
|
||||
}
|
||||
|
||||
/**
|
||||
* Set options
|
||||
*/
|
||||
setOptions(_options)
|
||||
{
|
||||
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')
|
||||
|
||||
// Connection event
|
||||
this.socket.on('connection', (socket) =>
|
||||
{
|
||||
this.socket.emit('update_projects', this.describe())
|
||||
|
||||
if(this.options.debug)
|
||||
{
|
||||
console.log('socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
createProject(_name)
|
||||
{
|
||||
// Create project
|
||||
const project = new Project({ name: _name, socket: this.originalSocket, debug: this.options.debug })
|
||||
let sameNameProject = this.all[ project.slug ]
|
||||
|
||||
// Try to found same name project
|
||||
while(typeof sameNameProject !== 'undefined')
|
||||
{
|
||||
// Found new number
|
||||
let lastNumber = sameNameProject.name.match(/\d+$/)
|
||||
let newNumber = 2
|
||||
|
||||
if(lastNumber && lastNumber.length)
|
||||
{
|
||||
lastNumber = ~~lastNumber[ 0 ]
|
||||
|
||||
newNumber = lastNumber + 1
|
||||
}
|
||||
|
||||
// Update project name
|
||||
project.set_name(_name + ' ' + newNumber)
|
||||
|
||||
// Try to found
|
||||
sameNameProject = this.all[ project.slug ]
|
||||
}
|
||||
|
||||
// Save
|
||||
this.all[ project.slug ] = project
|
||||
|
||||
// Emit
|
||||
this.socket.emit('update_projects', this.describe())
|
||||
|
||||
// Return
|
||||
return project
|
||||
}
|
||||
|
||||
deleteProject(_slug)
|
||||
{
|
||||
// Set up
|
||||
const project = this.all[ _slug ]
|
||||
|
||||
// Project found
|
||||
if(typeof project !== 'undefined')
|
||||
{
|
||||
// Delete
|
||||
delete this.all[ _slug ]
|
||||
project.destructor()
|
||||
|
||||
// Emit
|
||||
this.socket.emit('update_projects', this.describe())
|
||||
}
|
||||
}
|
||||
|
||||
getProjectBySlug(_slug)
|
||||
{
|
||||
// Find project
|
||||
const project = this.all[ _slug ]
|
||||
|
||||
// Found
|
||||
if(project)
|
||||
{
|
||||
return project
|
||||
}
|
||||
|
||||
// Not found
|
||||
return false
|
||||
}
|
||||
|
||||
describe()
|
||||
{
|
||||
const result = {}
|
||||
result.all = {}
|
||||
|
||||
for(const _slug in this.all)
|
||||
{
|
||||
const _project = this.all[ _slug ]
|
||||
|
||||
result.all[ _slug ] = {
|
||||
slug: _project.slug,
|
||||
name: _project.name,
|
||||
filesCount: _project.files.count,
|
||||
date: _project.date,
|
||||
lastUpdateDate: _project.files.last_version_date
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Projects
|
||||
@@ -1,20 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
class IDs
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
this.lastId = 0
|
||||
}
|
||||
|
||||
getId()
|
||||
{
|
||||
const lastId = ++this.lastId
|
||||
|
||||
return lastId
|
||||
}
|
||||
}
|
||||
|
||||
const ids = new IDs()
|
||||
|
||||
module.exports = ids
|
||||
@@ -1,38 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
// Dependencies
|
||||
const path = require('path')
|
||||
|
||||
class Paths
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
this.separator = path.sep
|
||||
}
|
||||
|
||||
normalize(_path)
|
||||
{
|
||||
if(_path === '.' || _path === '')
|
||||
{
|
||||
const path = '.'
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
const normalizedPath = './' + path.normalize(_path)
|
||||
|
||||
return normalizedPath
|
||||
}
|
||||
|
||||
parse(_path)
|
||||
{
|
||||
const normalizedPath = this.normalize(_path)
|
||||
const parsedPath = path.parse(normalizedPath)
|
||||
|
||||
return parsedPath
|
||||
}
|
||||
}
|
||||
|
||||
const paths = new Paths()
|
||||
|
||||
module.exports = paths
|
||||
@@ -1 +0,0 @@
|
||||
| Project
|
||||
@@ -1 +0,0 @@
|
||||
| Projects
|
||||
Reference in New Issue
Block a user