Restrudturing app + ES6 + Init socket + Init models
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
'use strict'
|
||||
|
||||
// Depedencies
|
||||
let chokidar = require( 'chokidar' ),
|
||||
path = require( 'path' ),
|
||||
colors = require( 'colors' ),
|
||||
ip = require( 'ip' ),
|
||||
socket_io_client = require( 'socket.io-client' )
|
||||
|
||||
/**
|
||||
* App class
|
||||
*/
|
||||
class App
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
constructor( options )
|
||||
{
|
||||
this.set_options( options )
|
||||
this.set_args()
|
||||
this.set_watcher()
|
||||
this.set_socket()
|
||||
}
|
||||
|
||||
/**
|
||||
* Set options
|
||||
*/
|
||||
set_options( options )
|
||||
{
|
||||
// No option
|
||||
if( typeof options !== 'object' )
|
||||
options = {}
|
||||
|
||||
// Defaults
|
||||
if( typeof options.domain === 'undefined' )
|
||||
options.domain = ip.address()
|
||||
|
||||
if( typeof options.port === 'undefined' )
|
||||
options.port = 3000
|
||||
|
||||
// Save
|
||||
this.options = options
|
||||
}
|
||||
|
||||
/**
|
||||
* Set args
|
||||
* Test missing arg
|
||||
*/
|
||||
set_args()
|
||||
{
|
||||
// Set up
|
||||
this.arguments = process.argv.slice( 2 )
|
||||
|
||||
// Missing project slug
|
||||
if( this.arguments.length === 0 )
|
||||
{
|
||||
// Stop process
|
||||
console.log( 'Missing arguments: first argument should be the projet slug'.red )
|
||||
process.exit()
|
||||
}
|
||||
|
||||
// Wrong project slug
|
||||
if( !this.arguments[ 0 ].match( /^[a-z_-][a-z0-9_-]+$/ ) )
|
||||
{
|
||||
// Stop process
|
||||
console.log( 'Wrong arguments: projet slug'.red )
|
||||
process.exit()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set socket
|
||||
* Connect to site
|
||||
*/
|
||||
set_socket()
|
||||
{
|
||||
// Set up
|
||||
this.socket = socket_io_client( `http://${this.options.domain}:${this.options.port}` )
|
||||
|
||||
// Connect event
|
||||
this.socket.on( 'connect', () =>
|
||||
{
|
||||
console.log( 'connected' )
|
||||
this.socket.emit( 'start_project', { slug : this.arguments[ 0 ] } )
|
||||
} )
|
||||
}
|
||||
|
||||
/**
|
||||
* Set watcher
|
||||
* Listen to modifications on file and folders
|
||||
*/
|
||||
set_watcher()
|
||||
{
|
||||
// Set up
|
||||
this.watcher = chokidar.watch(
|
||||
process.cwd(),
|
||||
{
|
||||
ignored : /[\/\\]\./,
|
||||
ignoreInitial : true
|
||||
}
|
||||
)
|
||||
|
||||
// Add event
|
||||
this.watcher.on( 'add', ( path ) =>
|
||||
{
|
||||
console.log( 'add:'.green.bold, path )
|
||||
} )
|
||||
|
||||
// Change event
|
||||
this.watcher.on( 'change', ( path ) =>
|
||||
{
|
||||
console.log( 'change:'.green.bold, path )
|
||||
} )
|
||||
|
||||
// Unlink event
|
||||
this.watcher.on( 'unlink', ( path ) =>
|
||||
{
|
||||
console.log( 'unlink:'.green.bold, path )
|
||||
} )
|
||||
|
||||
// AddDir event
|
||||
this.watcher.on( 'addDir', ( path ) =>
|
||||
{
|
||||
console.log( 'addDir:'.green.bold, path )
|
||||
} )
|
||||
|
||||
// UnlinkDir event
|
||||
this.watcher.on( 'unlinkDir', ( path ) =>
|
||||
{
|
||||
console.log( 'unlinkDir:'.green.bold, path )
|
||||
} )
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = App
|
||||
+3
-40
@@ -1,41 +1,4 @@
|
||||
// Depedencies
|
||||
var chokidar = require( 'chokidar' ),
|
||||
path = require( 'path' ),
|
||||
colors = require( 'colors' );
|
||||
'use strict'
|
||||
|
||||
// Working directory
|
||||
var cwd = process.cwd();
|
||||
|
||||
// Watcher
|
||||
var watcher = chokidar.watch(
|
||||
cwd,
|
||||
{
|
||||
ignored : /[\/\\]\./,
|
||||
ignoreInitial : true
|
||||
}
|
||||
);
|
||||
|
||||
watcher.on( 'add', ( path ) =>
|
||||
{
|
||||
console.log( 'add:'.green.bold, path );
|
||||
} );
|
||||
|
||||
watcher.on( 'change', ( path ) =>
|
||||
{
|
||||
console.log( 'change:'.green.bold, path );
|
||||
} );
|
||||
|
||||
watcher.on( 'unlink', ( path ) =>
|
||||
{
|
||||
console.log( 'unlink:'.green.bold, path );
|
||||
} );
|
||||
|
||||
watcher.on( 'addDir', ( path ) =>
|
||||
{
|
||||
console.log( 'addDir:'.green.bold, path );
|
||||
} );
|
||||
|
||||
watcher.on( 'unlinkDir', ( path ) =>
|
||||
{
|
||||
console.log( 'unlinkDir:'.green.bold, path );
|
||||
} );
|
||||
let App = require( './app.class.js' ),
|
||||
app = new App()
|
||||
+3
-1
@@ -18,6 +18,8 @@
|
||||
"homepage": "https://github.com/brunosimon/code-spectactor",
|
||||
"dependencies": {
|
||||
"chokidar": "^1.5.1",
|
||||
"colors": "^1.1.2"
|
||||
"colors": "^1.1.2",
|
||||
"ip": "^1.1.3",
|
||||
"socket.io-client": "^1.4.6"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
'use strict'
|
||||
|
||||
// Dependencies
|
||||
let express = require( 'express' ),
|
||||
helmet = require( 'helmet' ),
|
||||
http = require( 'http' ),
|
||||
socket_io = require( 'socket.io' ),
|
||||
colors = require( 'colors' ),
|
||||
path = require( 'path' ),
|
||||
ip = require( 'ip' ),
|
||||
util = require( 'util' )
|
||||
|
||||
/**
|
||||
* App class
|
||||
*/
|
||||
class App
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
constructor( _options )
|
||||
{
|
||||
this.set_options( _options )
|
||||
this.set_models()
|
||||
this.set_express()
|
||||
this.set_server()
|
||||
this.set_socket()
|
||||
}
|
||||
|
||||
/**
|
||||
* Set models
|
||||
*/
|
||||
set_models()
|
||||
{
|
||||
let Projects = require( './models/projects.class.js' )
|
||||
this.projects = new Projects()
|
||||
|
||||
var project = this.projects.create_project( 'test' )
|
||||
|
||||
project.update_file( 'toto/tata/tete/tutu.txt', 'content 1' )
|
||||
project.update_file( 'toto/tata/lorem.txt', 'content 2' )
|
||||
project.update_file( 'toto/tata/ipsum.txt', 'content 3' )
|
||||
|
||||
project.delete_file( 'toto/tata/tete/tutu.txt' )
|
||||
project.delete_file( 'toto/tata/lorem.txt' )
|
||||
project.delete_file( 'toto/tata/ipsum.txt' )
|
||||
|
||||
console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
|
||||
}
|
||||
|
||||
/**
|
||||
* Set options
|
||||
*/
|
||||
set_options( _options )
|
||||
{
|
||||
// No option
|
||||
if( typeof _options !== 'object' )
|
||||
_options = {}
|
||||
|
||||
if( typeof _options.port === 'undefined' )
|
||||
_options.port = 3000
|
||||
|
||||
// Save
|
||||
this.options = _options
|
||||
}
|
||||
|
||||
/**
|
||||
* Set express
|
||||
* Start express and set controllers
|
||||
*/
|
||||
set_express()
|
||||
{
|
||||
// Set up
|
||||
this.express = express()
|
||||
|
||||
// Controllers
|
||||
this.express.use( '/codes', require( './controllers/index.js' ) )
|
||||
}
|
||||
|
||||
/**
|
||||
* Set server
|
||||
*/
|
||||
set_server()
|
||||
{
|
||||
// Set up
|
||||
this.server = http.createServer( this.express )
|
||||
|
||||
// Start
|
||||
this.server.listen( this.options.port, () =>
|
||||
{
|
||||
// URL
|
||||
let url = 'http://' + ip.address() + ':' + this.options.port
|
||||
|
||||
console.log( colors.green( '---------------------------' ) )
|
||||
console.log( 'server'.green.bold + ' - ' + 'started'.cyan )
|
||||
console.log( 'server'.green.bold + ' - ' + url.cyan )
|
||||
} )
|
||||
}
|
||||
|
||||
/**
|
||||
* Set socket
|
||||
*/
|
||||
set_socket()
|
||||
{
|
||||
// Set up
|
||||
this.io = socket_io.listen( this.server )
|
||||
|
||||
// Connection event
|
||||
this.io.on( 'connection', ( socket ) =>
|
||||
{
|
||||
console.log( 'socket'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan )
|
||||
|
||||
socket.on( 'start_project', ( data ) =>
|
||||
{
|
||||
|
||||
} )
|
||||
} )
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = App
|
||||
+3
-30
@@ -1,31 +1,4 @@
|
||||
// Dependencies
|
||||
var express = require( 'express' ),
|
||||
helmet = require( 'helmet' ),
|
||||
http = require( 'http' ),
|
||||
socket_io = require( 'socket.io' ),
|
||||
colors = require( 'colors' ),
|
||||
path = require( 'path' ),
|
||||
ip = require( 'ip' );
|
||||
'use strict'
|
||||
|
||||
// Express
|
||||
var express = express();
|
||||
|
||||
express.use( '/codes', require( './controllers/index.js' ) )
|
||||
|
||||
// Server
|
||||
var server = http.createServer( express ),
|
||||
server_port = 3000;
|
||||
|
||||
// Server
|
||||
server.listen( server_port, function()
|
||||
{
|
||||
// URL
|
||||
var url = 'http://' + ip.address() + ':' + server_port;
|
||||
|
||||
console.log( colors.green( '---------------------------' ) );
|
||||
console.log( 'server'.green.bold + ' - ' + 'started'.cyan );
|
||||
console.log( 'server'.green.bold + ' - ' + url.cyan );
|
||||
} );
|
||||
|
||||
// Socket
|
||||
var io = socket_io.listen( server );
|
||||
let App = require( './app.class.js' ),
|
||||
app = new App()
|
||||
@@ -0,0 +1,34 @@
|
||||
'use strict'
|
||||
|
||||
class File
|
||||
{
|
||||
constructor( _name, _content )
|
||||
{
|
||||
// Set up
|
||||
this.name = _name
|
||||
this.versions = []
|
||||
|
||||
// Create first version
|
||||
if( typeof _content !== 'undefined' )
|
||||
this.create_version( _content )
|
||||
}
|
||||
|
||||
create_version( content )
|
||||
{
|
||||
// Create version
|
||||
let version = {}
|
||||
|
||||
// Set up
|
||||
version.date = new Date()
|
||||
version.content = content
|
||||
|
||||
this.versions.push( version )
|
||||
}
|
||||
|
||||
destructor()
|
||||
{
|
||||
console.log( 'destruct' )
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = File
|
||||
@@ -0,0 +1,135 @@
|
||||
'use strict'
|
||||
|
||||
// Dependencies
|
||||
let path = require( 'path' ),
|
||||
util = require( 'util' ),
|
||||
File = require( './file.class.js' )
|
||||
|
||||
class Project
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
this.folders = {
|
||||
root:
|
||||
{
|
||||
name : 'root',
|
||||
files : {},
|
||||
folders: {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
update_file( _path, _content )
|
||||
{
|
||||
// Set up
|
||||
let normalize_path = path.normalize( _path ),
|
||||
parsed_path = path.parse( normalize_path )
|
||||
|
||||
// Retrieve folder
|
||||
let folder = this.get_folder( parsed_path.dir )
|
||||
|
||||
// Folder doesn't exist
|
||||
if( !folder )
|
||||
{
|
||||
// Create folders
|
||||
folder = this.create_folder( parsed_path.dir )
|
||||
}
|
||||
|
||||
// Retrieve file
|
||||
let file = folder.files[ parsed_path.base ]
|
||||
|
||||
// File doesn't exist
|
||||
if( !file )
|
||||
{
|
||||
file = new File( parsed_path.base )
|
||||
folder.files[ file.name ] = file
|
||||
}
|
||||
|
||||
file.create_version( _content )
|
||||
}
|
||||
|
||||
get_folder( _path )
|
||||
{
|
||||
let path_parts = _path.split( path.sep ),
|
||||
folder = this.folders.root
|
||||
|
||||
for( let _path_part of path_parts )
|
||||
{
|
||||
folder = folder.folders[ _path_part ]
|
||||
|
||||
// Folder not found
|
||||
if(typeof folder === 'undefined')
|
||||
return false
|
||||
}
|
||||
|
||||
return folder
|
||||
}
|
||||
|
||||
create_folder( _path )
|
||||
{
|
||||
// Set up
|
||||
let path_parts = _path.split( path.sep ),
|
||||
folder = this.folders.root
|
||||
|
||||
for( let _path_part of path_parts )
|
||||
{
|
||||
// Temporary folder
|
||||
let __folder = folder.folders[ _path_part ]
|
||||
|
||||
// Folder doesn't exist
|
||||
if( typeof __folder === 'undefined' )
|
||||
{
|
||||
__folder = {}
|
||||
__folder.name = _path_part
|
||||
__folder.files = {}
|
||||
__folder.folders = {}
|
||||
|
||||
folder.folders[ _path_part ] = __folder
|
||||
}
|
||||
|
||||
folder = __folder
|
||||
}
|
||||
|
||||
return folder;
|
||||
}
|
||||
|
||||
// clean_folders()
|
||||
// {
|
||||
// // Empty all
|
||||
// this.folders.all = {}
|
||||
|
||||
// let clean_folder = function( folder )
|
||||
// {
|
||||
// for( let _folder_name in folder.folders )
|
||||
// {
|
||||
// clean_folder( folder.folders[ _folder_name ] )
|
||||
// }
|
||||
// }
|
||||
|
||||
// clean_folder( this.folders.root )
|
||||
// }
|
||||
|
||||
delete_file( _path )
|
||||
{
|
||||
// Set up
|
||||
let normalize_path = path.normalize( _path ),
|
||||
parsed_path = path.parse( normalize_path )
|
||||
|
||||
// Retrieve folder
|
||||
let folder = this.get_folder( parsed_path.dir )
|
||||
|
||||
// Folder found
|
||||
if( folder )
|
||||
{
|
||||
let file = folder.files[ parsed_path.base ]
|
||||
|
||||
// File found
|
||||
if( file )
|
||||
{
|
||||
delete folder.files[ parsed_path.base ]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Project
|
||||
@@ -0,0 +1,38 @@
|
||||
'use strict'
|
||||
|
||||
let Project = require( './project.class.js' )
|
||||
|
||||
class Projects
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
this.all = {}
|
||||
}
|
||||
|
||||
create_project( _slug )
|
||||
{
|
||||
// Create project
|
||||
let project = new Project( _slug )
|
||||
|
||||
// Save
|
||||
this.all[ project.slug ] = project
|
||||
|
||||
// Return
|
||||
return project
|
||||
}
|
||||
|
||||
get_project_by_slug( _slug )
|
||||
{
|
||||
// Find project
|
||||
let project = this.all[ _slug ]
|
||||
|
||||
// Found
|
||||
if( project )
|
||||
return project
|
||||
|
||||
// Not found
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Projects
|
||||
@@ -0,0 +1 @@
|
||||
sdfsdfdsf
|
||||
Reference in New Issue
Block a user