Connect app to site with socket
This commit is contained in:
+46
-7
@@ -5,7 +5,8 @@ let chokidar = require( 'chokidar' ),
|
||||
path = require( 'path' ),
|
||||
colors = require( 'colors' ),
|
||||
ip = require( 'ip' ),
|
||||
socket_io_client = require( 'socket.io-client' )
|
||||
socket_io_client = require( 'socket.io-client' ),
|
||||
fs = require( 'fs' )
|
||||
|
||||
/**
|
||||
* App class
|
||||
@@ -81,7 +82,7 @@ class App
|
||||
// Connect event
|
||||
this.socket.on( 'connect', () =>
|
||||
{
|
||||
console.log( 'connected' )
|
||||
console.log( 'connected'.green )
|
||||
this.socket.emit( 'start_project', { slug: this.arguments[ 0 ] } )
|
||||
} )
|
||||
}
|
||||
@@ -104,31 +105,69 @@ class App
|
||||
// Add event
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
@@ -35,9 +35,8 @@ class App
|
||||
// Set up
|
||||
this.projects = new Projects()
|
||||
|
||||
// Tests
|
||||
var project = this.projects.create_project( 'test' )
|
||||
|
||||
// // Tests
|
||||
// var project = this.projects.create_project( 'test' )
|
||||
// project.create_folder( 'toto' )
|
||||
// project.get_folder( 'toto//tutu/tete', true )
|
||||
// project.create_file( 'coucou/coco.txt', 'content 0' )
|
||||
@@ -46,8 +45,7 @@ class App
|
||||
// project.update_file( 'toto/tata/ipsum.txt', 'content 3' )
|
||||
// project.delete_folder( 'toto/tutu', true )
|
||||
// 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 )
|
||||
|
||||
let project = null
|
||||
|
||||
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 } ) )
|
||||
} )
|
||||
} )
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
'use strict'
|
||||
|
||||
// Depedencies
|
||||
let diff = require( 'diff' )
|
||||
|
||||
class File
|
||||
{
|
||||
constructor( _name, _content )
|
||||
|
||||
@@ -11,9 +11,9 @@ class Project
|
||||
{
|
||||
this.name = name
|
||||
this.folders = {
|
||||
root:
|
||||
'.':
|
||||
{
|
||||
name : 'root',
|
||||
name : '.',
|
||||
files : {},
|
||||
folders: {}
|
||||
}
|
||||
@@ -124,7 +124,7 @@ class Project
|
||||
// Set up
|
||||
let normalize_path = path.normalize( _path ),
|
||||
path_parts = normalize_path.split( path.sep ),
|
||||
folder = this.folders.root
|
||||
folder = this
|
||||
|
||||
for( let _path_part of path_parts )
|
||||
{
|
||||
@@ -156,7 +156,7 @@ class Project
|
||||
|
||||
let normalize_path = path.normalize( _path ),
|
||||
path_parts = normalize_path.split( path.sep ),
|
||||
folder = this.folders.root,
|
||||
folder = this,
|
||||
found = true
|
||||
|
||||
for( let _path_part of path_parts )
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
qdsqsdsq
|
||||
@@ -1 +0,0 @@
|
||||
dsfsdf
|
||||
@@ -0,0 +1,3 @@
|
||||
dsfsdf
|
||||
|
||||
dsfsdf
|
||||
@@ -0,0 +1,4 @@
|
||||
kqsdqsdjn
|
||||
iudfgdfg
|
||||
|
||||
qsdiuhqsdiuqhsd
|
||||
@@ -1,3 +0,0 @@
|
||||
test
|
||||
qsdqsd
|
||||
sdfdsf
|
||||
Reference in New Issue
Block a user