From 2cc95d01605ea7313182a52eec762d79da13c9bb Mon Sep 17 00:00:00 2001 From: brunosimon Date: Sat, 17 Sep 2016 02:35:08 +0200 Subject: [PATCH] [update] Global installation --- app/index.js | 4 - app/package.json | 25 ----- bin/index.js | 6 ++ bin/keppler.class.js | 104 +++++++++++++++++++ package.json | 34 ++++++ readme.md | 3 +- site/index.js | 4 +- site/models/event_emitter.class.js | 2 +- site/models/project.class.js | 1 + site/models/projects.class.js | 2 +- site/{app.class.js => site.class.js} | 67 ++++++------ watcher/index.js | 4 + app/app.class.js => watcher/watcher.class.js | 91 ++++++++-------- 13 files changed, 234 insertions(+), 113 deletions(-) delete mode 100644 app/index.js delete mode 100644 app/package.json create mode 100644 bin/index.js create mode 100644 bin/keppler.class.js create mode 100644 package.json rename site/{app.class.js => site.class.js} (95%) create mode 100644 watcher/index.js rename app/app.class.js => watcher/watcher.class.js (68%) diff --git a/app/index.js b/app/index.js deleted file mode 100644 index 3f06c73..0000000 --- a/app/index.js +++ /dev/null @@ -1,4 +0,0 @@ -'use strict' - -let App = require( './app.class.js' ), - app = new App() \ No newline at end of file diff --git a/app/package.json b/app/package.json deleted file mode 100644 index e6b147f..0000000 --- a/app/package.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "name": "code-spectator-app", - "version": "0.0.1", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "repository": { - "type": "git", - "url": "https://github.com/brunosimon/code-spectactor" - }, - "author": "Bruno Simon", - "license": "MIT", - "bugs": { - "url": "https://github.com/brunosimon/code-spectactor/issues" - }, - "homepage": "https://github.com/brunosimon/code-spectactor", - "dependencies": { - "chokidar": "^1.5.1", - "colors": "^1.1.2", - "ip": "^1.1.3", - "socket.io-client": "^1.4.6" - } -} diff --git a/bin/index.js b/bin/index.js new file mode 100644 index 0000000..5cb6b47 --- /dev/null +++ b/bin/index.js @@ -0,0 +1,6 @@ +#!/usr/bin/env node + +'use strict' + +let Keppler = require( './keppler.class.js' ), + keppler = new Keppler(); diff --git a/bin/keppler.class.js b/bin/keppler.class.js new file mode 100644 index 0000000..c228007 --- /dev/null +++ b/bin/keppler.class.js @@ -0,0 +1,104 @@ +'use strict' + +// Depedencies +let path = require( 'path' ), + colors = require( 'colors' ), + ip = require( 'ip' ), + Site = require( '../site/site.class.js' ), + Watcher = require( '../watcher/watcher.class.js' ) + +/** + * Keppler class + */ +class Keppler +{ + /** + * Constructor + */ + constructor( _options ) + { + this.set_arguments() + this.set_options( _options ) + this.set_site() + this.set_watcher() + } + + /** + * Set options + */ + set_options( _options ) + { + // No option + if( typeof _options !== 'object' ) + _options = {} + + // Defaults + if( typeof _options.debug === 'undefined' ) + { + if( this.arguments.length ) + { + _options.debug = this.arguments[ this.arguments.length - 1 ] === 'true' ? true : false + } + else + { + _options.debug = false + } + } + + if( typeof _options.port === 'undefined' ) + _options.port = 1571 + + if( typeof _options.domain === 'undefined' ) + _options.domain = `http://${ip.address()}:${_options.port}` + + // Save + this.options = _options + } + + /** + * Set arguments + * Retrieve arguments and test if missing + */ + set_arguments() + { + // Set up + this.arguments = process.argv.slice( 2 ) + + // Missing project name + if( this.arguments.length === 0 ) + { + // Stop process + console.log( 'Missing arguments: first argument should be the projet name'.red ) + process.exit() + } + } + + /** + * Set site + * Instantiate site + */ + set_site() + { + this.site = new Site( { + port : this.options.port, + domain: this.options.domain, + debug : this.options.debug + } ) + } + + /** + * Set watcher + * Instantiate watcher + */ + set_watcher() + { + this.watcher = new Watcher( { + port : this.options.port, + domain: this.options.domain, + debug : this.options.debug, + name : this.arguments[ 0 ] + } ) + } +} + +module.exports = Keppler diff --git a/package.json b/package.json new file mode 100644 index 0000000..81cb9d3 --- /dev/null +++ b/package.json @@ -0,0 +1,34 @@ +{ + "name": "keppler", + "version": "0.0.1", + "description": "", + "bin": { + "keppler": "bin/index.js" + }, + "main": "bin/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://github.com/brunosimon/keppler" + }, + "author": "Bruno Simon", + "license": "MIT", + "bugs": { + "url": "https://github.com/brunosimon/keppler/issues" + }, + "homepage": "https://github.com/brunosimon/keppler", + "dependencies": { + "chokidar": "^1.5.1", + "socket.io-client": "^1.4.6", + "colors": "^1.1.2", + "diff": "^2.2.3", + "express": "^4.13.4", + "helmet": "^2.1.0", + "ip": "^1.1.3", + "pug": "^2.0.0-beta4", + "slug": "^0.9.1", + "socket.io": "^1.4.6" + } +} diff --git a/readme.md b/readme.md index f34482a..fd41ea6 100644 --- a/readme.md +++ b/readme.md @@ -42,6 +42,7 @@ The project URL appears. Share it with the viewers on the same wifi. ### Futur features +- Log project URL and open in default browser - 404 - Retractable sidebar - Open multiple files @@ -56,4 +57,4 @@ The project URL appears. Share it with the viewers on the same wifi. - Tooltips - Copy file to clipboard - Download file -- Angular 2 \ No newline at end of file +- Angular 2 diff --git a/site/index.js b/site/index.js index 3f06c73..4b380c8 100644 --- a/site/index.js +++ b/site/index.js @@ -1,4 +1,4 @@ 'use strict' -let App = require( './app.class.js' ), - app = new App() \ No newline at end of file +let Site = require( './site.class.js' ), + site = new Site() diff --git a/site/models/event_emitter.class.js b/site/models/event_emitter.class.js index eb386b1..0c4c726 100644 --- a/site/models/event_emitter.class.js +++ b/site/models/event_emitter.class.js @@ -16,7 +16,7 @@ class Event_Emitter */ on( names, callback ) { - let that = this; + let that = this // Errors if( typeof names === 'undefined' || names === '' ) diff --git a/site/models/project.class.js b/site/models/project.class.js index 5ff1cbd..1d87b39 100644 --- a/site/models/project.class.js +++ b/site/models/project.class.js @@ -47,6 +47,7 @@ class Project { this.socket.emit( 'update_project', this.describe() ) + // Debug 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 f2ffd24..8e5a88f 100644 --- a/site/models/projects.class.js +++ b/site/models/projects.class.js @@ -91,7 +91,7 @@ class Projects { // Delete delete this.all[ _slug ] - project.destructor(); + project.destructor() // Emit this.socket.emit( 'update_projects', this.describe() ) diff --git a/site/app.class.js b/site/site.class.js similarity index 95% rename from site/app.class.js rename to site/site.class.js index 80344a5..e39277e 100644 --- a/site/app.class.js +++ b/site/site.class.js @@ -13,16 +13,15 @@ let express = require( 'express' ), fs = require( 'fs' ) /** - * App class + * Site class */ -class App +class Site { /** * Constructor */ constructor( _options ) { - this.set_arguments() this.set_options( _options ) this.set_express() this.set_server() @@ -38,53 +37,29 @@ class App { // No option if( typeof _options !== 'object' ) - { _options = {} - } + + // Defaults + if( typeof _options.debug === 'undefined' ) + _options.debug = false 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; + return // Default project let project = this.projects.create_project( 'dummy' ) @@ -228,6 +203,24 @@ class App // Set up this.server = http.createServer( this.express ) + // Error event + this.server.on( 'error', ( error ) => + { + // Server already running + if( error.code === 'EADDRINUSE' ) + { + // Debug + if( this.options.debug ) + { + console.log( 'server already running'.green.bold ) + } + + return + } + + console.log( error.message ) + } ) + // Start this.server.listen( this.options.port, () => { @@ -254,6 +247,7 @@ class App // Set up let project = null + // Debug if( this.options.debug ) { console.log( 'socket app'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan ) @@ -264,6 +258,7 @@ class App { project = this.projects.create_project( data.name ) + // Debug if( this.options.debug ) { console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) ) @@ -275,6 +270,7 @@ class App { project.files.create_version( data.path, data.content ) + // Debug if( this.options.debug ) { console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) ) @@ -286,6 +282,7 @@ class App { project.files.create( data.path, data.content ) + // Debug if( this.options.debug ) { console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) ) @@ -297,6 +294,7 @@ class App { project.files.delete( data.path ) + // Debug if( this.options.debug ) { console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) ) @@ -308,13 +306,14 @@ class App { this.projects.delete_project( project.slug ) + // Debug if( this.options.debug ) { console.log( 'socket app'.green.bold + ' - ' + 'disconnect'.cyan + ' - ' + socket.id.cyan ) } - } ); + } ) } ) } } -module.exports = App +module.exports = Site diff --git a/watcher/index.js b/watcher/index.js new file mode 100644 index 0000000..cf89850 --- /dev/null +++ b/watcher/index.js @@ -0,0 +1,4 @@ +'use strict' + +let Watcher = require( './watcher.class.js' ), + watcher = new Watcher() diff --git a/app/app.class.js b/watcher/watcher.class.js similarity index 68% rename from app/app.class.js rename to watcher/watcher.class.js index 3693d8d..2d92d0e 100644 --- a/app/app.class.js +++ b/watcher/watcher.class.js @@ -9,9 +9,9 @@ let chokidar = require( 'chokidar' ), fs = require( 'fs' ) /** - * App class + * Watcher class */ -class App +class Watcher { /** * Constructor @@ -19,7 +19,6 @@ class App constructor( _options ) { this.set_options( _options ) - this.set_arguments() this.set_watcher() this.set_socket() } @@ -34,42 +33,19 @@ class App _options = {} // Defaults - if( typeof _options.domain === 'undefined' ) - _options.domain = ip.address() + if( typeof _options.debug === 'undefined' ) + _options.debug = false if( typeof _options.port === 'undefined' ) _options.port = 1571 + 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 ) - - // Missing project name - if( this.arguments.length === 0 ) - { - // Stop process - console.log( 'Missing arguments: first argument should be the projet name'.red ) - process.exit() - } - - // // Wrong project name - // if( !this.arguments[ 0 ].match( /^[a-z_-][a-z0-9_-]+$/ ) ) - // { - // // Stop process - // console.log( 'Wrong arguments: projet name'.red ) - // process.exit() - // } - } - /** * Set socket * Connect to site @@ -77,13 +53,18 @@ class App set_socket() { // Set up - this.socket = socket_io_client( `http://${this.options.domain}:${this.options.port}/app` ) + this.socket = socket_io_client( `${this.options.domain}/app` ) // Connect event this.socket.on( 'connect', () => { - console.log( 'connected'.green ) - this.socket.emit( 'start_project', { name: this.arguments[ 0 ] } ) + this.socket.emit( 'start_project', { name: this.options.name } ) + + // Debug + if( this.options.debug ) + { + console.log( 'connected'.green.bold ) + } } ) } @@ -108,14 +89,18 @@ class App // 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() } ) } ) + + // Debug + if( this.options.debug ) + { + console.log( 'add:'.green.bold, relative_path ) + } } ) // Change event @@ -124,14 +109,18 @@ class App // 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() } ) } ) + + // Debug + if( this.options.debug ) + { + console.log( 'change:'.green.bold, relative_path ) + } } ) // Unlink event @@ -140,10 +129,14 @@ class App // 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 } ) + + // Debug + if( this.options.debug ) + { + console.log( 'unlink:'.green.bold, relative_path ) + } } ) // AddDir event @@ -152,10 +145,14 @@ class App // 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 } ) + + // Debug + if( this.options.debug ) + { + console.log( 'addDir:'.green.bold, relative_path ) + } } ) // UnlinkDir event @@ -164,12 +161,16 @@ class App // 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 } ) + + // Debug + if( this.options.debug ) + { + console.log( 'unlinkDir:'.green.bold, relative_path ) + } } ) } } -module.exports = App +module.exports = Watcher