🎨 Improve syntax and remove current site structure

This commit is contained in:
brunosimon
2017-07-18 02:02:51 +02:00
parent c909611c8c
commit b545869813
223 changed files with 3342 additions and 26501 deletions
+2 -2
View File
@@ -1,4 +1,4 @@
'use strict'
let Watcher = require( './watcher.class.js' ),
watcher = new Watcher()
const Watcher = require('./watcher.class.js')
const watcher = new Watcher()
+206 -194
View File
@@ -1,244 +1,256 @@
'use strict'
// Depedencies
let chokidar = require( 'chokidar' ),
path = require( 'path' ),
colors = require( 'colors' ),
ip = require( 'ip' ),
socket_io_client = require( 'socket.io-client' ),
fs = require( 'fs' ),
mime = require( 'mime' ),
minimatch = require( 'minimatch' )
const chokidar = require('chokidar')
const ip = require('ip')
const socketIoClient = require('socket.io-client')
const fs = require('fs')
const mime = require('mime')
/**
* Watcher class
*/
class Watcher
{
/**
* Constructor
*/
constructor( _options )
{
this.set_options( _options )
this.set_watcher()
this.set_socket()
}
/**
* Constructor
*/
constructor(_options)
{
this.setOptions(_options)
this.setWatcher()
this.setSocket()
}
/**
* Set options
*/
set_options( _options )
{
// No option
if( typeof _options !== 'object' )
_options = {}
/**
* Set options
*/
setOptions(_options)
{
const options = typeof _options !== 'object' ? {} : _options
// Defaults
if( typeof _options.debug === 'undefined' )
_options.debug = false
// Defaults
if(typeof options.debug === 'undefined')
{
options.debug = false
}
if( typeof _options.port === 'undefined' )
_options.port = 1571
if(typeof options.port === 'undefined')
{
options.port = 1571
}
if( typeof _options.domain === 'undefined' )
_options.domain = `http://${ip.address()}:${_options.port}`
if(typeof options.domain === 'undefined')
{
options.domain = `http://${ip.address()}:${options.port}`
}
if( typeof _options.max_file_size === 'undefined' )
_options.max_file_size = 2000
if(typeof options.maxFileSize === 'undefined')
{
options.maxFileSize = 2000
}
// Save
this.options = _options
}
// Save
this.options = options
}
/**
* Set socket
* Connect to site
*/
set_socket()
{
// Set up
this.socket = socket_io_client( `${this.options.domain}/app` )
/**
* Set socket
* Connect to site
*/
setSocket()
{
// Set up
this.socket = socketIoClient(`${this.options.domain}/app`)
// Connect event
this.socket.on( 'connect', () =>
{
this.socket.emit( 'start_project', { name: this.options.name } )
// Connect event
this.socket.on('connect', () =>
{
this.socket.emit('start_project', { name: this.options.name })
// Debug
if( this.options.debug )
{
console.log( 'connected'.green.bold )
}
} )
}
// Debug
if(this.options.debug)
{
console.log('connected'.green.bold)
}
})
}
/**
* Set watcher
* Listen to modifications on file and folders
*/
set_watcher()
{
// Create ignored regex
let regexs = [],
Minimatch = require( 'minimatch' ).Minimatch
/**
* Set watcher
* Listen to modifications on file and folders
*/
setWatcher()
{
// Create ignored regex
const regexs = []
const Minimatch = require('minimatch').Minimatch
for( let _exclude_key in this.options.exclude )
{
let _exclude = this.options.exclude[ _exclude_key ],
minimatch = new Minimatch( _exclude, { dot: true } ),
regex = '' + minimatch.makeRe()
for(const _excludeKey in this.options.exclude)
{
const _exclude = this.options.exclude[ _excludeKey ]
const minimatch = new Minimatch(_exclude, { dot: true })
let regex = '' + minimatch.makeRe()
regex = regex.replace( '/^', '' )
regex = regex.replace( '$/', '' )
regex = regex.replace('/^', '')
regex = regex.replace('$/', '')
regexs.push( regex )
}
regexs.push(regex)
}
let regex = new RegExp( regexs.join( '|' ) )
const regex = new RegExp(regexs.join('|'))
// Set up
this.watcher = chokidar.watch(
process.cwd(),
{
// ignored : /[\/\\]\./,
ignored : regex,
ignoreInitial: true
}
)
// Set up
this.watcher = chokidar.watch(
process.cwd(),
{
// ignored : /[\/\\]\./,
ignored : regex,
ignoreInitial: true
}
)
// Add event
this.watcher.on( 'add', ( _path ) =>
{
// Set up
let relative_path = _path.replace( process.cwd(), '.' ),
mime_type = mime.lookup( relative_path ),
file = {}
// Add event
this.watcher.on('add', (_path) =>
{
// Set up
const relativePath = _path.replace(process.cwd(), '.')
const mimeType = mime.lookup(relativePath)
const file = {}
file.path = relative_path
file.can_read = true
file.path = relativePath
file.canRead = true
// Test mime type
if( mime_type.match(/^(audio)|(video)|(image)/) )
{
file.can_read = false
}
// Test mime type
if(mimeType.match(/^(audio)|(video)|(image)/))
{
file.canRead = false
}
// Retrieve stats
fs.stat( _path, ( error, stats ) =>
{
// Max file size
if( stats.size > this.options.max_file_size )
file.can_read = false
// Retrieve stats
fs.stat(_path, (error, stats) =>
{
// Max file size
if(this.options.maxFileSize < stats.size)
{
file.canRead = false
}
// Read
fs.readFile( _path, ( error, data ) =>
{
if( file.can_read )
file.content = data.toString()
// Read
fs.readFile(_path, (error, data) =>
{
if(file.canRead)
{
file.content = data.toString()
}
// Send
this.socket.emit( 'create_file', file )
} )
} )
// Send
this.socket.emit('create_file', file)
})
})
// Debug
if( this.options.debug )
{
console.log( 'add:'.green.bold, relative_path )
}
} )
// Debug
if(this.options.debug)
{
console.log('add:'.green.bold, relativePath)
}
})
// Change event
this.watcher.on( 'change', ( _path ) =>
{
// Set up
let relative_path = _path.replace( process.cwd(), '.' ),
mime_type = mime.lookup( relative_path ),
file = {}
// Change event
this.watcher.on('change', (_path) =>
{
// Set up
const relativePath = _path.replace(process.cwd(), '.')
const mimeType = mime.lookup(relativePath)
const file = {}
file.path = relative_path
file.can_read = true
file.path = relativePath
file.canRead = true
// Test mime type
if( mime_type.match(/^(audio)|(video)|(image)/) )
{
file.can_read = false
}
// Test mime type
if(mimeType.match(/^(audio)|(video)|(image)/))
{
file.canRead = false
}
// Retrieve stats
fs.stat( _path, ( error, stats ) =>
{
// Test max file size
if( stats.size > this.options.max_file_size )
file.can_read = false
// Retrieve stats
fs.stat(_path, (error, stats) =>
{
// Test max file size
if(this.options.maxFileSize < stats.size)
{
file.canRead = false
}
// Read
fs.readFile( _path, ( error, data ) =>
{
if( file.can_read )
file.content = data.toString()
// Read
fs.readFile(_path, (error, data) =>
{
if(file.canRead)
{
file.content = data.toString()
}
// Send
this.socket.emit( 'update_file', file )
} )
} )
// Send
this.socket.emit('update_file', file)
})
})
// Debug
if( this.options.debug )
{
console.log( 'change:'.green.bold, relative_path )
}
} )
// Debug
if(this.options.debug)
{
console.log('change:'.green.bold, relativePath)
}
})
// Unlink event
this.watcher.on( 'unlink', ( _path ) =>
{
// Set up
let relative_path = _path.replace( process.cwd(), '.' )
// Unlink event
this.watcher.on('unlink', (_path) =>
{
// Set up
const relativePath = _path.replace(process.cwd(), '.')
// Send
this.socket.emit( 'delete_file', { path: relative_path } )
// Send
this.socket.emit('delete_file', { path: relativePath })
// Debug
if( this.options.debug )
{
console.log( 'unlink:'.green.bold, relative_path )
}
} )
// Debug
if(this.options.debug)
{
console.log('unlink:'.green.bold, relativePath)
}
})
// AddDir event
this.watcher.on( 'addDir', ( _path ) =>
{
// Set up
let relative_path = _path.replace( process.cwd(), '.' )
// AddDir event
this.watcher.on('addDir', (_path) =>
{
// Set up
const relativePath = _path.replace(process.cwd(), '.')
// Send
this.socket.emit( 'create_folder', { path: relative_path } )
// Send
this.socket.emit('create_folder', { path: relativePath })
// Debug
if( this.options.debug )
{
console.log( 'addDir:'.green.bold, relative_path )
}
} )
// Debug
if(this.options.debug)
{
console.log('addDir:'.green.bold, relativePath)
}
})
// UnlinkDir event
this.watcher.on( 'unlinkDir', ( _path ) =>
{
// Set up
let relative_path = _path.replace( process.cwd(), '.' )
// UnlinkDir event
this.watcher.on('unlinkDir', (_path) =>
{
// Set up
const relativePath = _path.replace(process.cwd(), '.')
// Send
this.socket.emit( 'delete_folder', { path: relative_path } )
// Send
this.socket.emit('delete_folder', { path: relativePath })
// Debug
if( this.options.debug )
{
console.log( 'unlinkDir:'.green.bold, relative_path )
}
} )
}
// Debug
if(this.options.debug)
{
console.log('unlinkDir:'.green.bold, relativePath)
}
})
}
}
module.exports = Watcher