diff --git a/app/app.class.js b/app/app.class.js index 68c989e..6db509a 100644 --- a/app/app.class.js +++ b/app/app.class.js @@ -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,8 +82,8 @@ class App // Connect event this.socket.on( 'connect', () => { - console.log( 'connected' ) - this.socket.emit( 'start_project', { slug : this.arguments[ 0 ] } ) + console.log( 'connected'.green ) + this.socket.emit( 'start_project', { slug: this.arguments[ 0 ] } ) } ) } @@ -96,39 +97,77 @@ class App this.watcher = chokidar.watch( process.cwd(), { - ignored : /[\/\\]\./, - ignoreInitial : true + ignored : /[\/\\]\./, + ignoreInitial: true } ) // 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 } ) } ) } } diff --git a/site/app.class.js b/site/app.class.js index 651e385..bbc85c5 100644 --- a/site/app.class.js +++ b/site/app.class.js @@ -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 } ) ) } ) } ) } diff --git a/site/models/file.class.js b/site/models/file.class.js index 08345f8..47e80e0 100644 --- a/site/models/file.class.js +++ b/site/models/file.class.js @@ -1,5 +1,8 @@ 'use strict' +// Depedencies +let diff = require( 'diff' ) + class File { constructor( _name, _content ) @@ -31,4 +34,4 @@ class File } } -module.exports = File \ No newline at end of file +module.exports = File diff --git a/site/models/project.class.js b/site/models/project.class.js index 7438554..e587db0 100644 --- a/site/models/project.class.js +++ b/site/models/project.class.js @@ -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 ) diff --git a/test-folder/folder-2/hey.txt b/test-folder/folder-2/hey.txt new file mode 100644 index 0000000..2d441ec --- /dev/null +++ b/test-folder/folder-2/hey.txt @@ -0,0 +1 @@ +qdsqsdsq diff --git a/test-folder/iusdhfiuhsdf b/test-folder/iusdhfiuhsdf deleted file mode 100644 index 36b2124..0000000 --- a/test-folder/iusdhfiuhsdf +++ /dev/null @@ -1 +0,0 @@ -dsfsdf diff --git a/test-folder/jsodfjidsf b/test-folder/jsodfjidsf deleted file mode 100644 index e69de29..0000000 diff --git a/test-folder/test-1.txt b/test-folder/test-1.txt new file mode 100644 index 0000000..c16eceb --- /dev/null +++ b/test-folder/test-1.txt @@ -0,0 +1,3 @@ +dsfsdf + +dsfsdf diff --git a/test-folder/test-2.txt b/test-folder/test-2.txt new file mode 100644 index 0000000..8740d3d --- /dev/null +++ b/test-folder/test-2.txt @@ -0,0 +1,4 @@ +kqsdqsdjn +iudfgdfg + +qsdiuhqsdiuqhsd diff --git a/test-folder/uhuh b/test-folder/uhuh deleted file mode 100644 index 18b6a3f..0000000 --- a/test-folder/uhuh +++ /dev/null @@ -1,3 +0,0 @@ -test -qsdqsd -sdfdsf \ No newline at end of file