🚧 Add internal socket back and improve general structure

This commit is contained in:
brunosimon
2017-07-29 01:09:01 +02:00
parent cfcf81364f
commit fd18099109
4 changed files with 462 additions and 423 deletions
+109 -69
View File
@@ -2,7 +2,7 @@
// Depedencies // Depedencies
const ip = require('ip') const ip = require('ip')
const Site = require('./site') const Site = require('./site.js')
const Watcher = require('./watcher.js') const Watcher = require('./watcher.js')
const Projects = require('./projects.js') const Projects = require('./projects.js')
const socketIo = require('socket.io') const socketIo = require('socket.io')
@@ -21,13 +21,9 @@ class App
constructor() constructor()
{ {
this.setConfig() this.setConfig()
// this.setSite() this.setSite()
this.setSocket()
this.socket = socketIo.listen() this.setProjects()
this.projects = new Projects({ socket: this.socket, debug: this.config.debug })
this.project = this.projects.createProject(this.config.name)
this.setWatcher() this.setWatcher()
} }
@@ -36,6 +32,9 @@ class App
*/ */
setConfig() setConfig()
{ {
const config = {}
// From arguments
const args = require('yargs') const args = require('yargs')
.option('debug', { .option('debug', {
alias: 'd', alias: 'd',
@@ -81,14 +80,12 @@ class App
}) })
.argv .argv
const config = {}
config.debug = args.debug config.debug = args.debug
config.name = args.name config.name = args.name
config.port = args.port config.port = args.port
config.exclude = args.exclude config.exclude = args.exclude
config.initialSend = args['initial-send'] config.initialSend = args['initial-send']
config.maxFileSize = args['max-file-size'] config.maxFileSize = args['max-file-size']
config.domain = `http://${ip.address()}:${config.port}`
if(config.name === '') if(config.name === '')
{ {
@@ -102,6 +99,9 @@ class App
} }
} }
// Custom
config.domain = `http://${ip.address()}${config.port !== 80 ? ':' + config.port : ''}`
// Save // Save
this.config = config this.config = config
} }
@@ -115,6 +115,100 @@ class App
this.site = new Site(this.config) 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 * Set watcher
* Instantiate watcher * Instantiate watcher
@@ -122,68 +216,14 @@ class App
setWatcher() setWatcher()
{ {
this.watcher = new Watcher(this.config) 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) => * Set projects
*/
setProjects()
{ {
console.log(data) this.projects = new Projects({ socket: this.sockets.main, debug: this.config.debug })
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)
// }
// })
} }
} }
+310
View File
@@ -0,0 +1,310 @@
'use strict'
// Dependencies
const express = require('express')
const helmet = require('helmet')
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
*/
class Site
{
/**
* Constructor
*/
constructor(_config)
{
this.config = _config
this.setExpress()
this.setServer()
// this.setSocket()
// this.setModels()
// this.setDummy()
}
// /**
// * Set Dummy
// */
// setDummy()
// {
// if(!this.config.debug)
// {
// return
// }
// // Default project
// const project = this.projects.createProject('Dummy project')
// // // Same name projects
// // let project_2 = this.projects.createProject('dummy')
// // let project_3 = this.projects.createProject('dummy')
// // let project_4 = this.projects.createProject('dummy')
// // let project_5 = this.projects.createProject('dummy')
// // Some file
// project.files.createVersion('./folder-test/test-4.css', fs.readFileSync('../test-folder/folder-1/test-4.css', 'utf8'))
// project.files.createVersion('./folder-test/depth-test/test-3.js', fs.readFileSync('../test-folder/folder-2/test-3.js', 'utf8'))
// project.files.createVersion('./folder-test/depth-test/test-3.js', fs.readFileSync('../test-folder/folder-2/test-3-diff-1.js', 'utf8'))
// project.files.createVersion('./folder-test/depth-test/test-3.js', fs.readFileSync('../test-folder/folder-2/test-3-diff-2.js', 'utf8'))
// project.files.createVersion('./folder-test/depth-test/test-3.js', fs.readFileSync('../test-folder/folder-2/test-3-diff-3.js', 'utf8'))
// project.files.createVersion('./folder-test/depth-test/test-3.js', fs.readFileSync('../test-folder/folder-2/test-3-diff-4.js', 'utf8'))
// project.files.createVersion('./folder-test/test-1.html', fs.readFileSync('../test-folder/test-1.html', 'utf8'))
// project.files.createVersion('./folder-test/test-2.php', fs.readFileSync('../test-folder/test-2.php', 'utf8'))
// project.files.createVersion('./folder-test/big-one.txt', 'line\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline')
// project.files.createVersion('./folder-test/loooooooooooooooooooooooooooooooong-one.txt', 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz')
// project.files.createVersion('./icons/test.js', 'Test icon')
// project.files.createVersion('./icons/test.html', 'Test icon')
// project.files.createVersion('./icons/test.sass', 'Test icon')
// project.files.createVersion('./icons/test.scss', 'Test icon')
// project.files.createVersion('./icons/test.less', 'Test icon')
// project.files.createVersion('./icons/test.stylus', 'Test icon')
// project.files.createVersion('./icons/test.styl', 'Test icon')
// project.files.createVersion('./icons/test.css', 'Test icon')
// project.files.createVersion('./icons/test.php', 'Test icon')
// project.files.createVersion('./icons/test.json', 'Test icon')
// project.files.createVersion('./icons/test.jade', 'Test icon')
// project.files.createVersion('./icons/test.pug', 'Test icon')
// project.files.createVersion('./icons/test.md', 'Test icon')
// project.files.createVersion('./icons/test.sql', 'Test icon')
// project.files.createVersion('./icons/test.htaccess', 'Test icon')
// project.files.createVersion('./icons/test.htpasswd', 'Test icon')
// project.files.createVersion('./icons/test.yml', 'Test icon')
// project.files.createVersion('./icons/test.svg', 'Test icon')
// project.files.createVersion('./icons/test.eot', 'Test icon')
// project.files.createVersion('./icons/test.ttf', 'Test icon')
// project.files.createVersion('./icons/test.woff', 'Test icon')
// project.files.createVersion('./icons/test.woff2', 'Test icon')
// project.files.createVersion('./icons/test.jpeg', 'Test icon')
// project.files.createVersion('./icons/test.jpg', 'Test icon')
// project.files.createVersion('./icons/test.tiff', 'Test icon')
// project.files.createVersion('./icons/test.gif', 'Test icon')
// project.files.createVersion('./icons/test.bmp', 'Test icon')
// project.files.createVersion('./icons/test.png', 'Test icon')
// project.files.createVersion('./icons/test.webp', 'Test icon')
// project.files.createVersion('./icons/test.mpeg', 'Test icon')
// project.files.createVersion('./icons/test.mpg', 'Test icon')
// project.files.createVersion('./icons/test.mp4', 'Test icon')
// project.files.createVersion('./icons/test.amv', 'Test icon')
// project.files.createVersion('./icons/test.wmv', 'Test icon')
// project.files.createVersion('./icons/test.mov', 'Test icon')
// project.files.createVersion('./icons/test.avi', 'Test icon')
// project.files.createVersion('./icons/test.ogv', 'Test icon')
// project.files.createVersion('./icons/test.mkv', 'Test icon')
// project.files.createVersion('./icons/test.webm', 'Test icon')
// project.files.createVersion('./icons/test.mp3', 'Test icon')
// project.files.createVersion('./icons/test.wav', 'Test icon')
// project.files.createVersion('./icons/test.ogg', 'Test icon')
// project.files.createVersion('./icons/test.raw', 'Test icon')
// project.files.createVersion('./icons/test.zip', 'Test icon')
// project.files.createVersion('./icons/test.rar', 'Test icon')
// project.files.createVersion('./icons/test.7z', 'Test icon')
// project.files.createVersion('./icons/test.gz', 'Test icon')
// project.files.createVersion('./icons/test.txt', 'Test icon')
// project.files.createVersion('./icons/test.coffee', 'Test icon')
// project.files.createVersion('./icons/test.gitignore', 'Test icon')
// project.files.createVersion('./icons/test.gitkeep', 'Test icon')
// project.files.createVersion('./icons/test.xml', 'Test icon')
// project.files.createVersion('./icons/test.twig', 'Test icon')
// project.files.createVersion('./icons/test.c', 'Test icon')
// project.files.createVersion('./icons/test.h', 'Test icon')
// project.files.createVersion('./icons/test.pwet', 'Test icon')
// // project.files.createVersion('./toto/tata/lorem.txt', '123456789')
// // project.files.createVersion('./toto/tata/lorem.txt', '1aze')
// // project.files.createVersion('./toto/tata/ipsum.txt', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia asperiores iure, animi voluptatibus ut officiis. Molestias, quod perferendis hic totam doloremque, porro aperiam enim tenetur, maxime inventore consequuntur nisi in?')
// // Adding file versions
// let counting = 0
// setInterval(function()
// {
// project.files.createVersion('./folder-test/multi-version.txt', 'test: ' + counting++)
// }, 2000)
// // Creating and deleting file
// let toggle = true
// setInterval(function()
// {
// if(toggle)
// project.files.create('./folder-test/toggle.txt', 'content')
// else
// project.files.delete('./folder-test/toggle.txt', 'content')
// toggle = !toggle
// }, 3000)
// // // Log
// // 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
*/
setExpress()
{
// Set up
this.express = express()
this.express.use(helmet())
this.express.set('view engine', 'pug')
this.express.set('views', path.join(__dirname, 'views'))
}
/**
* Set server
*/
setServer()
{
// Set up
this.server = http.createServer(this.express)
// Error event
this.server.on('error', (error) =>
{
// Server already running
if(error.code === 'EADDRINUSE')
{
// Debug
if(this.config.debug)
{
console.log('site already running'.green.bold)
}
return
}
console.log(error.message)
})
// Start
this.server.listen(this.config.port, () =>
{
// URL
console.log(colors.green('---------------------------'))
console.log('site'.green.bold + ' - ' + 'started'.cyan)
})
}
/**
* Create project
*/
createProject(_slug, _path)
{
this.express.get('/' + _slug, (request, response) =>
{
response.send('<a href="/' + _slug + '/files/empty.txt" download>download</a>')
})
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
-332
View File
@@ -1,332 +0,0 @@
'use strict'
// Dependencies
const express = require('express')
const helmet = require('helmet')
const http = require('http')
const socketIo = require('socket.io')
const colors = require('colors')
const ip = require('ip')
const util = require('util')
const path = require('path')
const Projects = require('../projects.js')
const fs = require('fs')
const opener = require('opener')
/**
* Site class
*/
class Site
{
/**
* Constructor
*/
constructor(_options)
{
this.setOptions(_options)
this.setExpress()
this.setServer()
this.setSocket()
this.setModels()
this.setDummy()
}
/**
* Set options
*/
setOptions(_options)
{
const options = typeof _options === 'object' ? _options : {}
// Defaults
if(typeof options.debug === 'undefined')
{
options.debug = false
}
if(typeof options.port === 'undefined')
{
options.port = 1571
}
if(typeof options.domain === 'undefined')
{
options.domain = `http://${ip.address()}:${options.port}`
}
// Save
this.options = options
}
/**
* Set Dummy
*/
setDummy()
{
if(!this.options.debug)
{
return
}
// Default project
const project = this.projects.createProject('Dummy project')
// // Same name projects
// let project_2 = this.projects.createProject('dummy')
// let project_3 = this.projects.createProject('dummy')
// let project_4 = this.projects.createProject('dummy')
// let project_5 = this.projects.createProject('dummy')
// Some file
project.files.createVersion('./folder-test/test-4.css', fs.readFileSync('../test-folder/folder-1/test-4.css', 'utf8'))
project.files.createVersion('./folder-test/depth-test/test-3.js', fs.readFileSync('../test-folder/folder-2/test-3.js', 'utf8'))
project.files.createVersion('./folder-test/depth-test/test-3.js', fs.readFileSync('../test-folder/folder-2/test-3-diff-1.js', 'utf8'))
project.files.createVersion('./folder-test/depth-test/test-3.js', fs.readFileSync('../test-folder/folder-2/test-3-diff-2.js', 'utf8'))
project.files.createVersion('./folder-test/depth-test/test-3.js', fs.readFileSync('../test-folder/folder-2/test-3-diff-3.js', 'utf8'))
project.files.createVersion('./folder-test/depth-test/test-3.js', fs.readFileSync('../test-folder/folder-2/test-3-diff-4.js', 'utf8'))
project.files.createVersion('./folder-test/test-1.html', fs.readFileSync('../test-folder/test-1.html', 'utf8'))
project.files.createVersion('./folder-test/test-2.php', fs.readFileSync('../test-folder/test-2.php', 'utf8'))
project.files.createVersion('./folder-test/big-one.txt', 'line\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline')
project.files.createVersion('./folder-test/loooooooooooooooooooooooooooooooong-one.txt', 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz')
project.files.createVersion('./icons/test.js', 'Test icon')
project.files.createVersion('./icons/test.html', 'Test icon')
project.files.createVersion('./icons/test.sass', 'Test icon')
project.files.createVersion('./icons/test.scss', 'Test icon')
project.files.createVersion('./icons/test.less', 'Test icon')
project.files.createVersion('./icons/test.stylus', 'Test icon')
project.files.createVersion('./icons/test.styl', 'Test icon')
project.files.createVersion('./icons/test.css', 'Test icon')
project.files.createVersion('./icons/test.php', 'Test icon')
project.files.createVersion('./icons/test.json', 'Test icon')
project.files.createVersion('./icons/test.jade', 'Test icon')
project.files.createVersion('./icons/test.pug', 'Test icon')
project.files.createVersion('./icons/test.md', 'Test icon')
project.files.createVersion('./icons/test.sql', 'Test icon')
project.files.createVersion('./icons/test.htaccess', 'Test icon')
project.files.createVersion('./icons/test.htpasswd', 'Test icon')
project.files.createVersion('./icons/test.yml', 'Test icon')
project.files.createVersion('./icons/test.svg', 'Test icon')
project.files.createVersion('./icons/test.eot', 'Test icon')
project.files.createVersion('./icons/test.ttf', 'Test icon')
project.files.createVersion('./icons/test.woff', 'Test icon')
project.files.createVersion('./icons/test.woff2', 'Test icon')
project.files.createVersion('./icons/test.jpeg', 'Test icon')
project.files.createVersion('./icons/test.jpg', 'Test icon')
project.files.createVersion('./icons/test.tiff', 'Test icon')
project.files.createVersion('./icons/test.gif', 'Test icon')
project.files.createVersion('./icons/test.bmp', 'Test icon')
project.files.createVersion('./icons/test.png', 'Test icon')
project.files.createVersion('./icons/test.webp', 'Test icon')
project.files.createVersion('./icons/test.mpeg', 'Test icon')
project.files.createVersion('./icons/test.mpg', 'Test icon')
project.files.createVersion('./icons/test.mp4', 'Test icon')
project.files.createVersion('./icons/test.amv', 'Test icon')
project.files.createVersion('./icons/test.wmv', 'Test icon')
project.files.createVersion('./icons/test.mov', 'Test icon')
project.files.createVersion('./icons/test.avi', 'Test icon')
project.files.createVersion('./icons/test.ogv', 'Test icon')
project.files.createVersion('./icons/test.mkv', 'Test icon')
project.files.createVersion('./icons/test.webm', 'Test icon')
project.files.createVersion('./icons/test.mp3', 'Test icon')
project.files.createVersion('./icons/test.wav', 'Test icon')
project.files.createVersion('./icons/test.ogg', 'Test icon')
project.files.createVersion('./icons/test.raw', 'Test icon')
project.files.createVersion('./icons/test.zip', 'Test icon')
project.files.createVersion('./icons/test.rar', 'Test icon')
project.files.createVersion('./icons/test.7z', 'Test icon')
project.files.createVersion('./icons/test.gz', 'Test icon')
project.files.createVersion('./icons/test.txt', 'Test icon')
project.files.createVersion('./icons/test.coffee', 'Test icon')
project.files.createVersion('./icons/test.gitignore', 'Test icon')
project.files.createVersion('./icons/test.gitkeep', 'Test icon')
project.files.createVersion('./icons/test.xml', 'Test icon')
project.files.createVersion('./icons/test.twig', 'Test icon')
project.files.createVersion('./icons/test.c', 'Test icon')
project.files.createVersion('./icons/test.h', 'Test icon')
project.files.createVersion('./icons/test.pwet', 'Test icon')
// project.files.createVersion('./toto/tata/lorem.txt', '123456789')
// project.files.createVersion('./toto/tata/lorem.txt', '1aze')
// project.files.createVersion('./toto/tata/ipsum.txt', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia asperiores iure, animi voluptatibus ut officiis. Molestias, quod perferendis hic totam doloremque, porro aperiam enim tenetur, maxime inventore consequuntur nisi in?')
// Adding file versions
let counting = 0
setInterval(function()
{
project.files.createVersion('./folder-test/multi-version.txt', 'test: ' + counting++)
}, 2000)
// Creating and deleting file
let toggle = true
setInterval(function()
{
if(toggle)
project.files.create('./folder-test/toggle.txt', 'content')
else
project.files.delete('./folder-test/toggle.txt', 'content')
toggle = !toggle
}, 3000)
// // Log
// 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.options.debug })
}
/**
* Set express
* Start express and set controllers
*/
setExpress()
{
// Set up
this.express = express()
this.express.use(helmet())
this.express.set('view engine', 'pug')
this.express.set('views', path.join(__dirname, 'views'))
this.express.use(express.static(path.join(__dirname, 'public')))
this.express.locals.domain = this.options.domain
// Controllers
this.express.use('/', require('./controllers/index.js'))
}
/**
* Set server
*/
setServer()
{
// Set up
this.server = http.createServer(this.express)
// Error event
this.server.on('error', (error) =>
{
// Server already running
if(error.code === 'EADDRINUSE')
{
// Debug
if(this.options.debug)
{
console.log('server already running'.green.bold)
}
return
}
console.log(error.message)
})
// Start
this.server.listen(this.options.port, () =>
{
// URL
console.log(colors.green('---------------------------'))
console.log('server'.green.bold + ' - ' + 'started'.cyan)
console.log('server'.green.bold + ' - ' + this.options.domain.cyan)
})
}
/**
* 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.options.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.options.domain + '/project/' + project.slug
console.log('server'.green.bold + ' - ' + url.cyan)
// Open in browser
opener(url)
// Debug
if(this.options.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.options.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.options.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.options.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.options.debug)
{
console.log('socket app'.green.bold + ' - ' + 'disconnect'.cyan + ' - ' + socket.id.cyan)
}
})
})
}
}
module.exports = Site
+41 -20
View File
@@ -4,25 +4,46 @@
const chokidar = require('chokidar') const chokidar = require('chokidar')
const fs = require('fs') const fs = require('fs')
const mime = require('mime') const mime = require('mime')
const EventEmitter = require('../event-emitter.js') const socketIoClient = require('socket.io-client')
/** /**
* Watcher class * Watcher class
*/ */
class Watcher extends EventEmitter class Watcher
{ {
/** /**
* Constructor * Constructor
*/ */
constructor(_config) constructor(_config)
{ {
super()
this.config = _config this.config = _config
this.path = process.cwd()
this.setSocket()
this.watch() this.watch()
} }
/**
* Set socket
*/
setSocket()
{
// Set up
this.socket = socketIoClient(`${this.config.domain}/app`)
// Connect event
this.socket.on('connect', () =>
{
this.socket.emit('start_project', { name: this.config.name, path: this.path })
// Debug
if(this.config.debug)
{
console.log('connected'.green.bold)
}
})
}
/** /**
* Watch * Watch
* Listen to modifications on files and folders * Listen to modifications on files and folders
@@ -50,7 +71,7 @@ class Watcher extends EventEmitter
// Set up // Set up
this.watcher = chokidar.watch( this.watcher = chokidar.watch(
process.cwd(), this.path,
{ {
// ignored : /[\/\\]\./, // ignored : /[\/\\]\./,
ignored : regex, ignored : regex,
@@ -62,7 +83,7 @@ class Watcher extends EventEmitter
this.watcher.on('add', (_path) => this.watcher.on('add', (_path) =>
{ {
// Set up // Set up
const relativePath = _path.replace(process.cwd(), '.') const relativePath = _path.replace(this.path, '.')
const mimeType = mime.lookup(relativePath) const mimeType = mime.lookup(relativePath)
const file = {} const file = {}
@@ -92,8 +113,8 @@ class Watcher extends EventEmitter
file.content = data.toString() file.content = data.toString()
} }
// Trigger // Emit
this.trigger('create_file', [file]) this.socket.emit('create_file', file)
}) })
}) })
@@ -108,7 +129,7 @@ class Watcher extends EventEmitter
this.watcher.on('change', (_path) => this.watcher.on('change', (_path) =>
{ {
// Set up // Set up
const relativePath = _path.replace(process.cwd(), '.') const relativePath = _path.replace(this.path, '.')
const mimeType = mime.lookup(relativePath) const mimeType = mime.lookup(relativePath)
const file = {} const file = {}
@@ -138,8 +159,8 @@ class Watcher extends EventEmitter
file.content = data.toString() file.content = data.toString()
} }
// Trigger // Emit
this.trigger('update_file', [file]) this.socket.emit('update_file', file)
}) })
}) })
@@ -154,10 +175,10 @@ class Watcher extends EventEmitter
this.watcher.on('unlink', (_path) => this.watcher.on('unlink', (_path) =>
{ {
// Set up // Set up
const relativePath = _path.replace(process.cwd(), '.') const relativePath = _path.replace(this.path, '.')
// Trigger // Emit
this.trigger('delete_file', [relativePath]) this.socket.emit('delete_file', relativePath)
// Debug // Debug
if(this.config.debug) if(this.config.debug)
@@ -170,10 +191,10 @@ class Watcher extends EventEmitter
this.watcher.on('addDir', (_path) => this.watcher.on('addDir', (_path) =>
{ {
// Set up // Set up
const relativePath = _path.replace(process.cwd(), '.') const relativePath = _path.replace(this.path, '.')
// Trigger // Emit
this.trigger('create_folder', [{ path: relativePath }]) this.socket.emit('create_folder', { path: relativePath })
// Debug // Debug
if(this.config.debug) if(this.config.debug)
@@ -186,10 +207,10 @@ class Watcher extends EventEmitter
this.watcher.on('unlinkDir', (_path) => this.watcher.on('unlinkDir', (_path) =>
{ {
// Set up // Set up
const relativePath = _path.replace(process.cwd(), '.') const relativePath = _path.replace(this.path, '.')
// Trigger // Emit
this.trigger('delete_folder', [{ path: relativePath }]) this.socket.emit('delete_folder', { path: relativePath })
// Debug // Debug
if(this.config.debug) if(this.config.debug)