Improve Project model
This commit is contained in:
+20
-20
@@ -15,9 +15,9 @@ class App
|
|||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*/
|
*/
|
||||||
constructor( options )
|
constructor( _options )
|
||||||
{
|
{
|
||||||
this.set_options( options )
|
this.set_options( _options )
|
||||||
this.set_args()
|
this.set_args()
|
||||||
this.set_watcher()
|
this.set_watcher()
|
||||||
this.set_socket()
|
this.set_socket()
|
||||||
@@ -26,21 +26,21 @@ class App
|
|||||||
/**
|
/**
|
||||||
* Set options
|
* Set options
|
||||||
*/
|
*/
|
||||||
set_options( options )
|
set_options( _options )
|
||||||
{
|
{
|
||||||
// No option
|
// No option
|
||||||
if( typeof options !== 'object' )
|
if( typeof _options !== 'object' )
|
||||||
options = {}
|
_options = {}
|
||||||
|
|
||||||
// Defaults
|
// Defaults
|
||||||
if( typeof options.domain === 'undefined' )
|
if( typeof _options.domain === 'undefined' )
|
||||||
options.domain = ip.address()
|
_options.domain = ip.address()
|
||||||
|
|
||||||
if( typeof options.port === 'undefined' )
|
if( typeof _options.port === 'undefined' )
|
||||||
options.port = 3000
|
_options.port = 3000
|
||||||
|
|
||||||
// Save
|
// Save
|
||||||
this.options = options
|
this.options = _options
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -102,33 +102,33 @@ class App
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Add event
|
// Add event
|
||||||
this.watcher.on( 'add', ( path ) =>
|
this.watcher.on( 'add', ( _path ) =>
|
||||||
{
|
{
|
||||||
console.log( 'add:'.green.bold, path )
|
console.log( 'add:'.green.bold, _path )
|
||||||
} )
|
} )
|
||||||
|
|
||||||
// Change event
|
// Change event
|
||||||
this.watcher.on( 'change', ( path ) =>
|
this.watcher.on( 'change', ( _path ) =>
|
||||||
{
|
{
|
||||||
console.log( 'change:'.green.bold, path )
|
console.log( 'change:'.green.bold, _path )
|
||||||
} )
|
} )
|
||||||
|
|
||||||
// Unlink event
|
// Unlink event
|
||||||
this.watcher.on( 'unlink', ( path ) =>
|
this.watcher.on( 'unlink', ( _path ) =>
|
||||||
{
|
{
|
||||||
console.log( 'unlink:'.green.bold, path )
|
console.log( 'unlink:'.green.bold, _path )
|
||||||
} )
|
} )
|
||||||
|
|
||||||
// AddDir event
|
// AddDir event
|
||||||
this.watcher.on( 'addDir', ( path ) =>
|
this.watcher.on( 'addDir', ( _path ) =>
|
||||||
{
|
{
|
||||||
console.log( 'addDir:'.green.bold, path )
|
console.log( 'addDir:'.green.bold, _path )
|
||||||
} )
|
} )
|
||||||
|
|
||||||
// UnlinkDir event
|
// UnlinkDir event
|
||||||
this.watcher.on( 'unlinkDir', ( path ) =>
|
this.watcher.on( 'unlinkDir', ( _path ) =>
|
||||||
{
|
{
|
||||||
console.log( 'unlinkDir:'.green.bold, path )
|
console.log( 'unlinkDir:'.green.bold, _path )
|
||||||
} )
|
} )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-11
@@ -6,9 +6,9 @@ let express = require( 'express' ),
|
|||||||
http = require( 'http' ),
|
http = require( 'http' ),
|
||||||
socket_io = require( 'socket.io' ),
|
socket_io = require( 'socket.io' ),
|
||||||
colors = require( 'colors' ),
|
colors = require( 'colors' ),
|
||||||
path = require( 'path' ),
|
|
||||||
ip = require( 'ip' ),
|
ip = require( 'ip' ),
|
||||||
util = require( 'util' )
|
util = require( 'util' ),
|
||||||
|
Projects = require( './models/projects.class.js' )
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* App class
|
* App class
|
||||||
@@ -32,20 +32,22 @@ class App
|
|||||||
*/
|
*/
|
||||||
set_models()
|
set_models()
|
||||||
{
|
{
|
||||||
let Projects = require( './models/projects.class.js' )
|
// Set up
|
||||||
this.projects = new Projects()
|
this.projects = new Projects()
|
||||||
|
|
||||||
|
// Tests
|
||||||
var project = this.projects.create_project( 'test' )
|
var project = this.projects.create_project( 'test' )
|
||||||
|
|
||||||
project.update_file( 'toto/tata/tete/tutu.txt', 'content 1' )
|
// project.create_folder( 'toto' )
|
||||||
project.update_file( 'toto/tata/lorem.txt', 'content 2' )
|
// project.get_folder( 'toto//tutu/tete', true )
|
||||||
project.update_file( 'toto/tata/ipsum.txt', 'content 3' )
|
// project.create_file( 'coucou/coco.txt', 'content 0' )
|
||||||
|
// project.update_file( 'toto/tutu/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_folder( 'toto/tutu', true )
|
||||||
|
// project.delete_file( 'toto/tata/ipsum.txt' )
|
||||||
|
|
||||||
project.delete_file( 'toto/tata/tete/tutu.txt' )
|
console.log( util.inspect( project, { depth: null, colors: true } ) )
|
||||||
project.delete_file( 'toto/tata/lorem.txt' )
|
|
||||||
project.delete_file( 'toto/tata/ipsum.txt' )
|
|
||||||
|
|
||||||
console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -72,6 +74,7 @@ class App
|
|||||||
{
|
{
|
||||||
// Set up
|
// Set up
|
||||||
this.express = express()
|
this.express = express()
|
||||||
|
this.express.use( helmet() )
|
||||||
|
|
||||||
// Controllers
|
// Controllers
|
||||||
this.express.use( '/codes', require( './controllers/index.js' ) )
|
this.express.use( '/codes', require( './controllers/index.js' ) )
|
||||||
|
|||||||
+153
-63
@@ -7,8 +7,9 @@ let path = require( 'path' ),
|
|||||||
|
|
||||||
class Project
|
class Project
|
||||||
{
|
{
|
||||||
constructor()
|
constructor( name )
|
||||||
{
|
{
|
||||||
|
this.name = name
|
||||||
this.folders = {
|
this.folders = {
|
||||||
root:
|
root:
|
||||||
{
|
{
|
||||||
@@ -19,7 +20,83 @@ class Project
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
create_file( _path, _content )
|
||||||
|
{
|
||||||
|
// Set up
|
||||||
|
let normalize_path = path.normalize( _path ),
|
||||||
|
parsed_path = path.parse( normalize_path )
|
||||||
|
|
||||||
|
// Retrieve file
|
||||||
|
let file = this.get_file( normalize_path )
|
||||||
|
|
||||||
|
// File already exist
|
||||||
|
if( file )
|
||||||
|
return false
|
||||||
|
|
||||||
|
// Retrieve folder (force creation)
|
||||||
|
let folder = this.get_folder( parsed_path.dir, true )
|
||||||
|
|
||||||
|
// Create
|
||||||
|
file = new File( parsed_path.base )
|
||||||
|
folder.files[ file.name ] = file
|
||||||
|
|
||||||
|
// Has content
|
||||||
|
if( _content )
|
||||||
|
file.create_version( _content )
|
||||||
|
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
get_file( _path, _force_creation )
|
||||||
|
{
|
||||||
|
// Params
|
||||||
|
if( typeof _force_creation === 'undefined' )
|
||||||
|
_force_creation = false
|
||||||
|
|
||||||
|
// Set up
|
||||||
|
let normalize_path = path.normalize( _path ),
|
||||||
|
parsed_path = path.parse( normalize_path )
|
||||||
|
|
||||||
|
// Retrieve folder
|
||||||
|
let folder = this.get_folder( parsed_path.dir, _force_creation )
|
||||||
|
|
||||||
|
// Folder not found
|
||||||
|
if( !folder )
|
||||||
|
return false
|
||||||
|
|
||||||
|
// Retrieve file
|
||||||
|
let file = folder.files[ parsed_path.base ]
|
||||||
|
|
||||||
|
// File found
|
||||||
|
if( file )
|
||||||
|
return file
|
||||||
|
|
||||||
|
// Force creation
|
||||||
|
if( _force_creation )
|
||||||
|
{
|
||||||
|
// Create folders
|
||||||
|
file = this.create_file( normalize_path )
|
||||||
|
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not found
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
update_file( _path, _content )
|
update_file( _path, _content )
|
||||||
|
{
|
||||||
|
// Set up
|
||||||
|
let normalize_path = path.normalize( _path )
|
||||||
|
|
||||||
|
// Retrieve file
|
||||||
|
let file = this.get_file( normalize_path, true )
|
||||||
|
|
||||||
|
// Create version
|
||||||
|
file.create_version( _content )
|
||||||
|
}
|
||||||
|
|
||||||
|
delete_file( _path )
|
||||||
{
|
{
|
||||||
// Set up
|
// Set up
|
||||||
let normalize_path = path.normalize( _path ),
|
let normalize_path = path.normalize( _path ),
|
||||||
@@ -28,48 +105,26 @@ class Project
|
|||||||
// Retrieve folder
|
// Retrieve folder
|
||||||
let folder = this.get_folder( parsed_path.dir )
|
let folder = this.get_folder( parsed_path.dir )
|
||||||
|
|
||||||
// Folder doesn't exist
|
// Folder found
|
||||||
if( !folder )
|
if( folder )
|
||||||
{
|
{
|
||||||
// Create folders
|
let file = folder.files[ parsed_path.base ]
|
||||||
folder = this.create_folder( parsed_path.dir )
|
|
||||||
|
// File found
|
||||||
|
if( file )
|
||||||
|
{
|
||||||
|
file.destructor()
|
||||||
|
delete folder.files[ parsed_path.base ]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 )
|
create_folder( _path )
|
||||||
{
|
{
|
||||||
// Set up
|
// Set up
|
||||||
let path_parts = _path.split( path.sep ),
|
let normalize_path = path.normalize( _path ),
|
||||||
folder = this.folders.root
|
path_parts = normalize_path.split( path.sep ),
|
||||||
|
folder = this.folders.root
|
||||||
|
|
||||||
for( let _path_part of path_parts )
|
for( let _path_part of path_parts )
|
||||||
{
|
{
|
||||||
@@ -90,45 +145,80 @@ class Project
|
|||||||
folder = __folder
|
folder = __folder
|
||||||
}
|
}
|
||||||
|
|
||||||
return folder;
|
return folder
|
||||||
}
|
}
|
||||||
|
|
||||||
// clean_folders()
|
get_folder( _path, _force_creation )
|
||||||
// {
|
{
|
||||||
// // Empty all
|
// Params
|
||||||
// this.folders.all = {}
|
if( typeof _force_creation === 'undefined' )
|
||||||
|
_force_creation = false
|
||||||
|
|
||||||
// let clean_folder = function( folder )
|
let normalize_path = path.normalize( _path ),
|
||||||
// {
|
path_parts = normalize_path.split( path.sep ),
|
||||||
// for( let _folder_name in folder.folders )
|
folder = this.folders.root,
|
||||||
// {
|
found = true
|
||||||
// clean_folder( folder.folders[ _folder_name ] )
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// clean_folder( this.folders.root )
|
for( let _path_part of path_parts )
|
||||||
// }
|
{
|
||||||
|
folder = folder.folders[ _path_part ]
|
||||||
|
|
||||||
delete_file( _path )
|
// Folder not found
|
||||||
|
if(typeof folder === 'undefined')
|
||||||
|
{
|
||||||
|
found = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Folder found
|
||||||
|
if( found )
|
||||||
|
return folder
|
||||||
|
|
||||||
|
// Force creation
|
||||||
|
if( _force_creation )
|
||||||
|
{
|
||||||
|
// Create folders
|
||||||
|
folder = this.create_folder( normalize_path )
|
||||||
|
|
||||||
|
return folder
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not found
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
delete_folder( _path )
|
||||||
{
|
{
|
||||||
// Set up
|
// Set up
|
||||||
let normalize_path = path.normalize( _path ),
|
let normalize_path = path.normalize( _path ),
|
||||||
parsed_path = path.parse( normalize_path )
|
path_parts = _path.split( path.sep ),
|
||||||
|
folder = this.get_folder( normalize_path )
|
||||||
|
|
||||||
// Retrieve folder
|
// Folder not found
|
||||||
let folder = this.get_folder( parsed_path.dir )
|
if( !folder )
|
||||||
|
return false
|
||||||
|
|
||||||
// Folder found
|
// Recursive callback
|
||||||
if( folder )
|
let delete_folder = function( folder )
|
||||||
{
|
|
||||||
let file = folder.files[ parsed_path.base ]
|
|
||||||
|
|
||||||
// File found
|
|
||||||
if( file )
|
|
||||||
{
|
{
|
||||||
delete folder.files[ parsed_path.base ]
|
for( let _folder_name in folder.folders )
|
||||||
|
{
|
||||||
|
delete_folder( folder.folders[ _folder_name ] )
|
||||||
|
}
|
||||||
|
|
||||||
|
for( let _file_name in folder.files )
|
||||||
|
{
|
||||||
|
let file = folder.files[ _file_name ]
|
||||||
|
|
||||||
|
file.destructor()
|
||||||
|
delete folder.files[ _file_name ]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
delete_folder( folder )
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
dsfsdf
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
sdfsdfdsf
|
|
||||||
Reference in New Issue
Block a user