Update project structure
This commit is contained in:
+15
-33
@@ -41,18 +41,14 @@ class App
|
|||||||
|
|
||||||
// Tests
|
// Tests
|
||||||
var project = this.projects.create_project( 'dummy' )
|
var project = this.projects.create_project( 'dummy' )
|
||||||
project.create_folder( './toto' )
|
project.files.create( './coucou/coco.txt', '1234' )
|
||||||
project.get_folder( './toto//tutu/tete', true )
|
project.files.create( './test-1.txt', 'content 1' )
|
||||||
project.create_file( './coucou/coco.txt', '1234' )
|
project.files.update( './test-1.txt', 'content 2' )
|
||||||
project.create_file( './test-1.txt', 'content 1' )
|
project.files.update( './test-1.txt', 'content 31298' )
|
||||||
project.update_file( './test-1.txt', 'content 2' )
|
project.files.update( './toto/tata/lorem.txt', '123456789' )
|
||||||
project.update_file( './test-1.txt', 'content 31298' )
|
project.files.update( './toto/tata/lorem.txt', '1aze' )
|
||||||
project.update_file( './toto/tata/lorem.txt', '123456789' )
|
project.files.update( './toto/tata/ipsum.txt', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia asperiores iure, animi voluptatibus ut officiis. Molestias, quod perferendis hic totam doloremque, porro aperiam enim tenetur, maxime inventore consequuntur nisi in?' )
|
||||||
project.update_file( './toto/tata/lorem.txt', '1aze' )
|
project.files.delete( './toto/tata/ipsum.txt' )
|
||||||
project.update_file( './toto/tata/ipsum.txt', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia asperiores iure, animi voluptatibus ut officiis. Molestias, quod perferendis hic totam doloremque, porro aperiam enim tenetur, maxime inventore consequuntur nisi in?' )
|
|
||||||
project.delete_folder( './toto/tata', true )
|
|
||||||
project.delete_file( './toto/tata/ipsum.txt' )
|
|
||||||
console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
|
|
||||||
console.log( util.inspect( project.files, { depth: null, colors: true } ) )
|
console.log( util.inspect( project.files, { depth: null, colors: true } ) )
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,42 +129,28 @@ class App
|
|||||||
// Create project
|
// Create project
|
||||||
project = this.projects.create_project( data.name )
|
project = this.projects.create_project( data.name )
|
||||||
|
|
||||||
console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
|
console.log( util.inspect( project.files, { depth: null, colors: true } ) )
|
||||||
} )
|
} )
|
||||||
|
|
||||||
socket.on( 'update_file', ( data ) =>
|
socket.on( 'update_file', ( data ) =>
|
||||||
{
|
{
|
||||||
project.update_file( data.path, data.content )
|
project.files.file( data.path, data.content )
|
||||||
|
|
||||||
console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
|
console.log( util.inspect( project.files, { depth: null, colors: true } ) )
|
||||||
} )
|
} )
|
||||||
|
|
||||||
socket.on( 'create_file', ( data ) =>
|
socket.on( 'create_file', ( data ) =>
|
||||||
{
|
{
|
||||||
project.create_file( data.path, data.content )
|
project.files.create( data.path, data.content )
|
||||||
|
|
||||||
console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
|
console.log( util.inspect( project.files, { depth: null, colors: true } ) )
|
||||||
} )
|
} )
|
||||||
|
|
||||||
socket.on( 'delete_file', ( data ) =>
|
socket.on( 'delete_file', ( data ) =>
|
||||||
{
|
{
|
||||||
project.delete_file( data.path )
|
project.files.delete( data.path )
|
||||||
|
|
||||||
console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
|
console.log( util.inspect( project.files, { depth: null, colors: true } ) )
|
||||||
} )
|
|
||||||
|
|
||||||
socket.on( 'create_folder', ( data ) =>
|
|
||||||
{
|
|
||||||
project.create_folder( data.path, data.content )
|
|
||||||
|
|
||||||
console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
|
|
||||||
} )
|
|
||||||
|
|
||||||
socket.on( 'delete_folder', ( data ) =>
|
|
||||||
{
|
|
||||||
project.delete_folder( data.path )
|
|
||||||
|
|
||||||
console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
|
|
||||||
} )
|
} )
|
||||||
} )
|
} )
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,121 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
// Dependencies
|
||||||
|
let paths = require( '../utils/paths.class.js' ),
|
||||||
|
File = require( './file.class.js' )
|
||||||
|
|
||||||
|
class Files
|
||||||
|
{
|
||||||
|
constructor( options )
|
||||||
|
{
|
||||||
|
this.items = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
create( _path, _content )
|
||||||
|
{
|
||||||
|
// Set up
|
||||||
|
let normalized_path = paths.normalize( _path ),
|
||||||
|
parsed_path = paths.parse( normalized_path )
|
||||||
|
|
||||||
|
// Retrieve file
|
||||||
|
let file = this.get( normalized_path )
|
||||||
|
|
||||||
|
// File already exist
|
||||||
|
if( file )
|
||||||
|
return false
|
||||||
|
|
||||||
|
// Create
|
||||||
|
file = new File( {
|
||||||
|
name : parsed_path.base,
|
||||||
|
path : parsed_path.dir,
|
||||||
|
content: _content
|
||||||
|
} )
|
||||||
|
|
||||||
|
// Save
|
||||||
|
this.items[ normalized_path ] = file
|
||||||
|
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
get( _path, _force_creation )
|
||||||
|
{
|
||||||
|
// Params
|
||||||
|
if( typeof _force_creation === 'undefined' )
|
||||||
|
_force_creation = false
|
||||||
|
|
||||||
|
// Set up
|
||||||
|
let normalized_path = paths.normalize( _path )
|
||||||
|
|
||||||
|
// Retrieve file
|
||||||
|
let file = this.items[ normalized_path ]
|
||||||
|
|
||||||
|
// File found
|
||||||
|
if( typeof file !== 'undefined' )
|
||||||
|
{
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
// Force creation
|
||||||
|
if( _force_creation )
|
||||||
|
{
|
||||||
|
file = this.create( normalized_path )
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not found
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
update( _path, _content )
|
||||||
|
{
|
||||||
|
// Set up
|
||||||
|
let normalized_path = paths.normalize( _path )
|
||||||
|
|
||||||
|
// Retrieve file
|
||||||
|
let file = this.get( normalized_path, true )
|
||||||
|
|
||||||
|
// Create version
|
||||||
|
file.create_version( _content )
|
||||||
|
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
delete( _path )
|
||||||
|
{
|
||||||
|
// Set up
|
||||||
|
let normalized_path = paths.normalize( _path )
|
||||||
|
|
||||||
|
// Retrieve file
|
||||||
|
let file = this.get( normalized_path )
|
||||||
|
|
||||||
|
// File found
|
||||||
|
if( file )
|
||||||
|
{
|
||||||
|
// Delete file
|
||||||
|
file.destructor()
|
||||||
|
delete this.items[ normalized_path ]
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
describe()
|
||||||
|
{
|
||||||
|
// Set up
|
||||||
|
let result = {}
|
||||||
|
|
||||||
|
// Each file
|
||||||
|
for( let _file_path in this.items )
|
||||||
|
{
|
||||||
|
let _file = this.items[ _file_path ]
|
||||||
|
|
||||||
|
result[ _file_path ] = _file.describe()
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Files
|
||||||
@@ -1,11 +1,8 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
// Dependencies
|
// Dependencies
|
||||||
let paths = require( '../utils/paths.class.js' ),
|
let slug = require( 'slug' ),
|
||||||
ids = require( '../utils/ids.class.js' ),
|
Files = require( './files.class.js' )
|
||||||
util = require( 'util' ),
|
|
||||||
slug = require( 'slug' ),
|
|
||||||
File = require( './file.class.js' )
|
|
||||||
|
|
||||||
class Project
|
class Project
|
||||||
{
|
{
|
||||||
@@ -13,15 +10,7 @@ class Project
|
|||||||
{
|
{
|
||||||
this.name = options.name
|
this.name = options.name
|
||||||
this.slug = slug( this.name )
|
this.slug = slug( this.name )
|
||||||
this.files = {}
|
this.files = new Files()
|
||||||
this.folders = {
|
|
||||||
'.':
|
|
||||||
{
|
|
||||||
name : '.',
|
|
||||||
files : {},
|
|
||||||
folders: {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.set_socket( options.socket )
|
this.set_socket( options.socket )
|
||||||
}
|
}
|
||||||
@@ -42,283 +31,15 @@ class Project
|
|||||||
} )
|
} )
|
||||||
}
|
}
|
||||||
|
|
||||||
create_file( _path, _content )
|
|
||||||
{
|
|
||||||
// Set up
|
|
||||||
let normalized_path = paths.normalize( _path ),
|
|
||||||
parsed_path = paths.parse( normalized_path )
|
|
||||||
|
|
||||||
// Retrieve file
|
|
||||||
let file = this.get_file( normalized_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( {
|
|
||||||
name : parsed_path.base,
|
|
||||||
path : parsed_path.dir,
|
|
||||||
content: _content
|
|
||||||
} )
|
|
||||||
|
|
||||||
// Save
|
|
||||||
folder.files[ file.name ] = file.id
|
|
||||||
this.files[ file.id ] = file
|
|
||||||
|
|
||||||
// Emit
|
|
||||||
this.socket.emit( 'update_file', file.describe() )
|
|
||||||
this.socket.emit( 'update_folders', this.describe_folders() )
|
|
||||||
|
|
||||||
return file
|
|
||||||
}
|
|
||||||
|
|
||||||
get_file( _path, _force_creation )
|
|
||||||
{
|
|
||||||
// Params
|
|
||||||
if( typeof _force_creation === 'undefined' )
|
|
||||||
_force_creation = false
|
|
||||||
|
|
||||||
// Set up
|
|
||||||
let normalized_path = paths.normalize( _path ),
|
|
||||||
parsed_path = paths.parse( normalized_path )
|
|
||||||
|
|
||||||
// Retrieve folder
|
|
||||||
let folder = this.get_folder( parsed_path.dir, _force_creation )
|
|
||||||
|
|
||||||
// Folder not found
|
|
||||||
if( !folder )
|
|
||||||
return false
|
|
||||||
|
|
||||||
// Retrieve file
|
|
||||||
let file_id = folder.files[ parsed_path.base ]
|
|
||||||
|
|
||||||
// File found
|
|
||||||
if( file_id )
|
|
||||||
{
|
|
||||||
let file = this.files[ file_id ]
|
|
||||||
return file
|
|
||||||
}
|
|
||||||
|
|
||||||
// Force creation
|
|
||||||
if( _force_creation )
|
|
||||||
{
|
|
||||||
// Create folders
|
|
||||||
let file = this.create_file( normalized_path )
|
|
||||||
|
|
||||||
return file
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not found
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
update_file( _path, _content )
|
|
||||||
{
|
|
||||||
// Set up
|
|
||||||
let normalized_path = paths.normalize( _path )
|
|
||||||
|
|
||||||
// Retrieve file
|
|
||||||
let file = this.get_file( normalized_path, true )
|
|
||||||
|
|
||||||
// Create version
|
|
||||||
file.create_version( _content )
|
|
||||||
|
|
||||||
// Emit
|
|
||||||
this.socket.emit( 'update_file', file.describe() )
|
|
||||||
|
|
||||||
return file
|
|
||||||
}
|
|
||||||
|
|
||||||
delete_file( _path )
|
|
||||||
{
|
|
||||||
// Set up
|
|
||||||
let parsed_path = paths.parse( _path )
|
|
||||||
|
|
||||||
// Retrieve folder
|
|
||||||
let folder = this.get_folder( parsed_path.dir )
|
|
||||||
|
|
||||||
// Folder found
|
|
||||||
if( folder )
|
|
||||||
{
|
|
||||||
let file_id = folder.files[ parsed_path.base ]
|
|
||||||
|
|
||||||
// File found
|
|
||||||
if( file_id )
|
|
||||||
{
|
|
||||||
let file = this.files[ file_id ]
|
|
||||||
|
|
||||||
// Delete file
|
|
||||||
file.destructor()
|
|
||||||
delete folder.files[ file.name ]
|
|
||||||
delete this.files[ file.id ]
|
|
||||||
|
|
||||||
// Emit
|
|
||||||
this.socket.emit( 'delete_file', file_id )
|
|
||||||
this.socket.emit( 'update_folders', this.describe_folders() )
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
create_folder( _path )
|
|
||||||
{
|
|
||||||
// Set up
|
|
||||||
let normalized_path = paths.normalize( _path ),
|
|
||||||
path_folders = normalized_path.split( paths.separator ),
|
|
||||||
folder = this
|
|
||||||
|
|
||||||
for( let _path_folder of path_folders )
|
|
||||||
{
|
|
||||||
// Temporary folder
|
|
||||||
let _folder = folder.folders[ _path_folder ]
|
|
||||||
|
|
||||||
// Folder doesn't exist
|
|
||||||
if( typeof _folder === 'undefined' )
|
|
||||||
{
|
|
||||||
_folder = {}
|
|
||||||
_folder.name = _path_folder
|
|
||||||
_folder.files = {}
|
|
||||||
_folder.folders = {}
|
|
||||||
|
|
||||||
folder.folders[ _path_folder ] = _folder
|
|
||||||
}
|
|
||||||
|
|
||||||
folder = _folder
|
|
||||||
}
|
|
||||||
|
|
||||||
// Emit
|
|
||||||
this.socket.emit( 'update_folders', this.describe_folders() )
|
|
||||||
|
|
||||||
return folder
|
|
||||||
}
|
|
||||||
|
|
||||||
get_folder( _path, _force_creation )
|
|
||||||
{
|
|
||||||
// Params
|
|
||||||
if( typeof _force_creation === 'undefined' )
|
|
||||||
_force_creation = false
|
|
||||||
|
|
||||||
let normalized_path = paths.normalize( _path ),
|
|
||||||
path_folders = normalized_path.split( paths.separator ),
|
|
||||||
folder = this,
|
|
||||||
found = true
|
|
||||||
|
|
||||||
for( let _path_folder of path_folders )
|
|
||||||
{
|
|
||||||
folder = folder.folders[ _path_folder ]
|
|
||||||
|
|
||||||
// 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( normalized_path )
|
|
||||||
|
|
||||||
return folder
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not found
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
delete_folder( _path )
|
|
||||||
{
|
|
||||||
// Set up
|
|
||||||
let normalized_path = paths.normalize( _path ),
|
|
||||||
folder = this.get_folder( normalized_path )
|
|
||||||
|
|
||||||
// Folder not found
|
|
||||||
if( !folder )
|
|
||||||
return false
|
|
||||||
|
|
||||||
// Recursive callback
|
|
||||||
let empty_folder = ( folder ) =>
|
|
||||||
{
|
|
||||||
// Each folder
|
|
||||||
for( let _folder_name in folder.folders )
|
|
||||||
{
|
|
||||||
empty_folder( folder.folders[ _folder_name ] )
|
|
||||||
|
|
||||||
// Delete folder
|
|
||||||
delete folder.folders[ _folder_name ]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Each file
|
|
||||||
for( let _file_name in folder.files )
|
|
||||||
{
|
|
||||||
// Set up
|
|
||||||
let file_id = folder.files[ _file_name ],
|
|
||||||
file = this.files[ file_id ]
|
|
||||||
|
|
||||||
// File found
|
|
||||||
if( file )
|
|
||||||
{
|
|
||||||
// Delete file
|
|
||||||
this.delete_file( file.path.full )
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
empty_folder( folder )
|
|
||||||
|
|
||||||
// Delete folder
|
|
||||||
let parsed_path = paths.parse( normalized_path ),
|
|
||||||
parent_folder = this.get_folder( parsed_path.dir )
|
|
||||||
|
|
||||||
if( parent_folder )
|
|
||||||
delete parent_folder.folders[ folder.name ]
|
|
||||||
|
|
||||||
// Emit
|
|
||||||
this.socket.emit( 'update_folders', this.describe_folders() )
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
describe()
|
describe()
|
||||||
{
|
{
|
||||||
// Set up
|
// Set up
|
||||||
let result = {}
|
let result = {}
|
||||||
result.name = this.name
|
result.name = this.name
|
||||||
result.folders = this.describe_folders()
|
result.files = this.files.describe()
|
||||||
result.files = this.describe_files()
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
describe_files()
|
|
||||||
{
|
|
||||||
// Set up
|
|
||||||
let result = {}
|
|
||||||
|
|
||||||
// Each file
|
|
||||||
for( let _file_id in this.files )
|
|
||||||
{
|
|
||||||
let _file = this.files[ _file_id ]
|
|
||||||
|
|
||||||
result[ _file_id ] = _file.describe()
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
describe_folders()
|
|
||||||
{
|
|
||||||
return this.folders
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Project
|
module.exports = Project
|
||||||
|
|||||||
Reference in New Issue
Block a user