Connect app to site with socket

This commit is contained in:
brunosimon
2016-06-03 01:41:49 +02:00
parent 6e4bc28c90
commit 9347b79423
10 changed files with 101 additions and 24 deletions
+49 -10
View File
@@ -5,7 +5,8 @@ let chokidar = require( 'chokidar' ),
path = require( 'path' ), path = require( 'path' ),
colors = require( 'colors' ), colors = require( 'colors' ),
ip = require( 'ip' ), ip = require( 'ip' ),
socket_io_client = require( 'socket.io-client' ) socket_io_client = require( 'socket.io-client' ),
fs = require( 'fs' )
/** /**
* App class * App class
@@ -81,8 +82,8 @@ class App
// Connect event // Connect event
this.socket.on( 'connect', () => this.socket.on( 'connect', () =>
{ {
console.log( 'connected' ) console.log( 'connected'.green )
this.socket.emit( 'start_project', { slug : this.arguments[ 0 ] } ) this.socket.emit( 'start_project', { slug: this.arguments[ 0 ] } )
} ) } )
} }
@@ -96,39 +97,77 @@ class App
this.watcher = chokidar.watch( this.watcher = chokidar.watch(
process.cwd(), process.cwd(),
{ {
ignored : /[\/\\]\./, ignored : /[\/\\]\./,
ignoreInitial : true ignoreInitial: true
} }
) )
// Add event // Add event
this.watcher.on( 'add', ( _path ) => this.watcher.on( 'add', ( _path ) =>
{ {
console.log( 'add:'.green.bold, _path ) // Set up
let relative_path = _path.replace( process.cwd() + '/', '' )
console.log( 'add:'.green.bold, relative_path )
// Read
fs.readFile( _path, ( error, data ) =>
{
// Send
this.socket.emit( 'create_file', { path: relative_path, content: data.toString() } )
} )
} ) } )
// Change event // Change event
this.watcher.on( 'change', ( _path ) => this.watcher.on( 'change', ( _path ) =>
{ {
console.log( 'change:'.green.bold, _path ) // Set up
let relative_path = _path.replace( process.cwd() + '/', '' )
console.log( 'change:'.green.bold, relative_path )
// Read
fs.readFile( _path, ( error, data ) =>
{
// Send
this.socket.emit( 'update_file', { path: relative_path, content: data.toString() } )
} )
} ) } )
// Unlink event // Unlink event
this.watcher.on( 'unlink', ( _path ) => this.watcher.on( 'unlink', ( _path ) =>
{ {
console.log( 'unlink:'.green.bold, _path ) // Set up
let relative_path = _path.replace( process.cwd() + '/', '' )
console.log( 'unlink:'.green.bold, relative_path )
// Send
this.socket.emit( 'delete_file', { path: relative_path } )
} ) } )
// AddDir event // AddDir event
this.watcher.on( 'addDir', ( _path ) => this.watcher.on( 'addDir', ( _path ) =>
{ {
console.log( 'addDir:'.green.bold, _path ) // Set up
let relative_path = _path.replace( process.cwd() + '/', '' )
console.log( 'addDir:'.green.bold, relative_path )
// Send
this.socket.emit( 'create_folder', { path: relative_path } )
} ) } )
// UnlinkDir event // UnlinkDir event
this.watcher.on( 'unlinkDir', ( _path ) => this.watcher.on( 'unlinkDir', ( _path ) =>
{ {
console.log( 'unlinkDir:'.green.bold, _path ) // Set up
let relative_path = _path.replace( process.cwd() + '/', '' )
console.log( 'unlinkDir:'.green.bold, relative_path )
// Send
this.socket.emit( 'delete_folder', { path: relative_path } )
} ) } )
} }
} }
+36 -5
View File
@@ -35,9 +35,8 @@ class App
// Set up // Set up
this.projects = new Projects() this.projects = new Projects()
// Tests // // Tests
var project = this.projects.create_project( 'test' ) // var project = this.projects.create_project( 'test' )
// project.create_folder( 'toto' ) // project.create_folder( 'toto' )
// project.get_folder( 'toto//tutu/tete', true ) // project.get_folder( 'toto//tutu/tete', true )
// project.create_file( 'coucou/coco.txt', 'content 0' ) // project.create_file( 'coucou/coco.txt', 'content 0' )
@@ -46,8 +45,7 @@ class App
// project.update_file( 'toto/tata/ipsum.txt', 'content 3' ) // project.update_file( 'toto/tata/ipsum.txt', 'content 3' )
// project.delete_folder( 'toto/tutu', true ) // project.delete_folder( 'toto/tutu', true )
// project.delete_file( 'toto/tata/ipsum.txt' ) // project.delete_file( 'toto/tata/ipsum.txt' )
// console.log( util.inspect( project, { depth: null, colors: true } ) )
console.log( util.inspect( project, { depth: null, colors: true } ) )
} }
/** /**
@@ -113,9 +111,42 @@ class App
{ {
console.log( 'socket'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan ) console.log( 'socket'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan )
let project = null
socket.on( 'start_project', ( data ) => socket.on( 'start_project', ( data ) =>
{ {
project = this.projects.create_project( data.slug )
console.log( util.inspect( project, { depth: null, colors: true } ) )
} )
socket.on( 'update_file', ( data ) =>
{
project.update_file( data.path, data.content )
console.log( util.inspect( project, { depth: null, colors: true } ) )
} )
socket.on( 'create_file', ( data ) =>
{
project.create_file( data.path, data.content )
console.log( util.inspect( project, { depth: null, colors: true } ) )
} )
socket.on( 'delete_file', ( data ) =>
{
project.delete_file( data.path )
console.log( util.inspect( project, { depth: null, colors: true } ) )
} )
socket.on( 'create_folder', ( data ) =>
{
project.create_folder( data.path, data.content )
console.log( util.inspect( project, { depth: null, colors: true } ) )
} )
socket.on( 'delete_folder', ( data ) =>
{
project.delete_folder( data.path )
console.log( util.inspect( project, { depth: null, colors: true } ) )
} ) } )
} ) } )
} }
+4 -1
View File
@@ -1,5 +1,8 @@
'use strict' 'use strict'
// Depedencies
let diff = require( 'diff' )
class File class File
{ {
constructor( _name, _content ) constructor( _name, _content )
@@ -31,4 +34,4 @@ class File
} }
} }
module.exports = File module.exports = File
+4 -4
View File
@@ -11,9 +11,9 @@ class Project
{ {
this.name = name this.name = name
this.folders = { this.folders = {
root: '.':
{ {
name : 'root', name : '.',
files : {}, files : {},
folders: {} folders: {}
} }
@@ -124,7 +124,7 @@ class Project
// Set up // Set up
let normalize_path = path.normalize( _path ), let normalize_path = path.normalize( _path ),
path_parts = normalize_path.split( path.sep ), path_parts = normalize_path.split( path.sep ),
folder = this.folders.root folder = this
for( let _path_part of path_parts ) for( let _path_part of path_parts )
{ {
@@ -156,7 +156,7 @@ class Project
let normalize_path = path.normalize( _path ), let normalize_path = path.normalize( _path ),
path_parts = normalize_path.split( path.sep ), path_parts = normalize_path.split( path.sep ),
folder = this.folders.root, folder = this,
found = true found = true
for( let _path_part of path_parts ) for( let _path_part of path_parts )
+1
View File
@@ -0,0 +1 @@
qdsqsdsq
-1
View File
@@ -1 +0,0 @@
dsfsdf
View File
+3
View File
@@ -0,0 +1,3 @@
dsfsdf
dsfsdf
+4
View File
@@ -0,0 +1,4 @@
kqsdqsdjn
iudfgdfg
qsdiuhqsdiuqhsd
-3
View File
@@ -1,3 +0,0 @@
test
qsdqsd
sdfdsf