From b1a01d34ec8cafc7c3c9044e5e3e68040bc9c52b Mon Sep 17 00:00:00 2001 From: Bruno SIMON Date: Fri, 16 Sep 2016 19:28:34 +0200 Subject: [PATCH] [update] Debug toggle --- app/app.class.js | 6 +- site/app.class.js | 106 +++++++++++++++++++++++++--------- site/models/project.class.js | 28 +++++++-- site/models/projects.class.js | 28 +++++++-- 4 files changed, 127 insertions(+), 41 deletions(-) diff --git a/app/app.class.js b/app/app.class.js index eea6a66..3693d8d 100644 --- a/app/app.class.js +++ b/app/app.class.js @@ -19,7 +19,7 @@ class App constructor( _options ) { this.set_options( _options ) - this.set_args() + this.set_arguments() this.set_watcher() this.set_socket() } @@ -45,10 +45,10 @@ class App } /** - * Set args + * Set arguments * Test missing arg */ - set_args() + set_arguments() { // Set up this.arguments = process.argv.slice( 2 ) diff --git a/site/app.class.js b/site/app.class.js index e193d5e..80344a5 100644 --- a/site/app.class.js +++ b/site/app.class.js @@ -22,10 +22,8 @@ class App */ constructor( _options ) { + this.set_arguments() this.set_options( _options ) - - this.domain = 'http://' + ip.address() + ':' + this.options.port - this.set_express() this.set_server() this.set_socket() @@ -33,11 +31,61 @@ class App this.set_dummy() } + /** + * Set options + */ + set_options( _options ) + { + // No option + if( typeof _options !== 'object' ) + { + _options = {} + } + + if( typeof _options.port === 'undefined' ) + { + _options.port = 1571 + } + + if( typeof _options.debug === 'undefined' ) + { + if( this.arguments.length ) + { + _options.debug = this.arguments[ 0 ] === 'true' ? true : false + } + else + { + _options.debug = false + } + } + + if( typeof _options.domain === 'undefined' ) + { + _options.domain = `http://${ip.address()}:${_options.port}` + } + + // Save + this.options = _options + } + + /** + * Set arguments + * Test missing arg + */ + set_arguments() + { + // Set up + this.arguments = process.argv.slice( 2 ) + } + /** * Set Dummy */ set_dummy() { + if( !this.options.debug ) + return; + // Default project let project = this.projects.create_project( 'dummy' ) @@ -150,23 +198,7 @@ class App set_models() { // Set up - this.projects = new Projects( { socket: this.sockets.main } ) - } - - /** - * Set options - */ - set_options( _options ) - { - // No option - if( typeof _options !== 'object' ) - _options = {} - - if( typeof _options.port === 'undefined' ) - _options.port = 1571 - - // Save - this.options = _options + this.projects = new Projects( { socket: this.sockets.main, debug: this.options.debug } ) } /** @@ -182,7 +214,7 @@ class App this.express.set( 'views', path.join( __dirname, 'views' ) ) this.express.use( express.static( path.join( __dirname, 'public' ) ) ) - this.express.locals.domain = this.domain + this.express.locals.domain = this.options.domain // Controllers this.express.use( '/', require( './controllers/index.js' ) ) @@ -202,7 +234,7 @@ class App // URL console.log( colors.green( '---------------------------' ) ) console.log( 'server'.green.bold + ' - ' + 'started'.cyan ) - console.log( 'server'.green.bold + ' - ' + this.domain.cyan ) + console.log( 'server'.green.bold + ' - ' + this.options.domain.cyan ) } ) } @@ -222,14 +254,20 @@ class App // Set up let project = null - console.log( 'socket app'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan ) + 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.create_project( data.name ) - // console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) ) + if( this.options.debug ) + { + console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) ) + } } ) // Update file @@ -237,7 +275,10 @@ class App { project.files.create_version( data.path, data.content ) - // console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) ) + if( this.options.debug ) + { + console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) ) + } } ) // Create file @@ -245,7 +286,10 @@ class App { project.files.create( data.path, data.content ) - // console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) ) + if( this.options.debug ) + { + console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) ) + } } ) // Delete file @@ -253,7 +297,10 @@ class App { project.files.delete( data.path ) - // console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) ) + if( this.options.debug ) + { + console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) ) + } } ) // Disconnect @@ -261,7 +308,10 @@ class App { this.projects.delete_project( project.slug ) - console.log( 'socket app'.green.bold + ' - ' + 'disconnect'.cyan + ' - ' + socket.id.cyan ) + if( this.options.debug ) + { + console.log( 'socket app'.green.bold + ' - ' + 'disconnect'.cyan + ' - ' + socket.id.cyan ) + } } ); } ) } diff --git a/site/models/project.class.js b/site/models/project.class.js index d145a9d..5ff1cbd 100644 --- a/site/models/project.class.js +++ b/site/models/project.class.js @@ -6,15 +6,30 @@ let slug = require( 'slug' ), class Project { - constructor( options ) + constructor( _options ) { - this.set_name( options.name ) - this.set_socket( options.socket ) + this.set_options( _options ) + this.set_name( _options.name ) + this.set_socket( _options.socket ) this.files = new Files( { socket: this.socket } ) this.date = new Date() } + /** + * Set options + */ + set_options( _options ) + { + if( typeof _options.debug === 'undefined' ) + { + _options.debug = false + } + + // Save + this.options = _options + } + set_name( _name ) { this.name = _name @@ -30,9 +45,12 @@ class Project // Connection event this.socket.on( 'connection', ( socket ) => { - console.log( 'socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan ) - this.socket.emit( 'update_project', this.describe() ) + + if( this.options.debug ) + { + console.log( 'socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan ) + } } ) } diff --git a/site/models/projects.class.js b/site/models/projects.class.js index 3b8da55..f2ffd24 100644 --- a/site/models/projects.class.js +++ b/site/models/projects.class.js @@ -4,11 +4,26 @@ let Project = require( './project.class.js' ) class Projects { - constructor( options ) + constructor( _options ) { this.all = {} - this.set_socket( options.socket ) + this.set_options( _options ) + this.set_socket( _options.socket ) + } + + /** + * Set options + */ + set_options( _options ) + { + if( typeof _options.debug === 'undefined' ) + { + _options.debug = false + } + + // Save + this.options = _options } set_socket( socket ) @@ -20,16 +35,19 @@ class Projects // Connection event this.socket.on( 'connection', ( socket ) => { - console.log( 'socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan ) - this.socket.emit( 'update_projects', this.describe() ) + + if( this.options.debug ) + { + console.log( 'socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan ) + } } ) } create_project( _name ) { // Create project - let project = new Project( { name: _name, socket: this.original_socket } ), + let project = new Project( { name: _name, socket: this.original_socket, debug: this.options.debug } ), same_name_project = this.all[ project.slug ] // Try to found same name project