Update server side structure
This commit is contained in:
+5
-4
@@ -41,8 +41,8 @@ class App
|
|||||||
|
|
||||||
// Tests
|
// Tests
|
||||||
var project = this.projects.create_project( 'dummy' )
|
var project = this.projects.create_project( 'dummy' )
|
||||||
project.create_folder( './toto' )
|
// project.create_folder( './toto' )
|
||||||
project.get_folder( './toto//tutu/tete', true )
|
// project.get_folder( './toto//tutu/tete', true )
|
||||||
project.create_file( './coucou/coco.txt', '1234' )
|
project.create_file( './coucou/coco.txt', '1234' )
|
||||||
project.create_file( './test-1.txt', 'content 1' )
|
project.create_file( './test-1.txt', 'content 1' )
|
||||||
project.update_file( './test-1.txt', 'content 2' )
|
project.update_file( './test-1.txt', 'content 2' )
|
||||||
@@ -50,9 +50,10 @@ class App
|
|||||||
project.update_file( './toto/tata/lorem.txt', '123456789' )
|
project.update_file( './toto/tata/lorem.txt', '123456789' )
|
||||||
project.update_file( './toto/tata/lorem.txt', '1aze' )
|
project.update_file( './toto/tata/lorem.txt', '1aze' )
|
||||||
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.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/tutu', true )
|
project.delete_folder( './toto/tata', true )
|
||||||
// project.delete_file( './toto/tata/ipsum.txt' )
|
// project.delete_file( './toto/tata/ipsum.txt' )
|
||||||
// console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
|
console.log( util.inspect( project.folders, { depth: null, colors: true } ) )
|
||||||
|
console.log( util.inspect( project.files, { depth: null, colors: true } ) )
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,19 +1,26 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
// Depedencies
|
// Depedencies
|
||||||
let diff = require( 'diff' )
|
let diff = require( 'diff' ),
|
||||||
|
paths = require( '../utils/paths.class.js' ),
|
||||||
|
ids = require( '../utils/ids.class.js' )
|
||||||
|
|
||||||
class File
|
class File
|
||||||
{
|
{
|
||||||
constructor( _name, _content )
|
constructor( _options )
|
||||||
{
|
{
|
||||||
// Set up
|
// Set up
|
||||||
this.name = _name
|
this.id = ids.get_id()
|
||||||
this.versions = []
|
this.name = _options.name
|
||||||
|
this.path = {}
|
||||||
|
this.path.directory = _options.path
|
||||||
|
this.path.full = this.path.directory + '/' + this.name
|
||||||
|
this.path.parts = this.path.full.split( paths.separator )
|
||||||
|
this.versions = []
|
||||||
|
|
||||||
// Create first version
|
// Create first version
|
||||||
if( typeof _content !== 'undefined' )
|
if( typeof _options.content !== 'undefined' )
|
||||||
this.create_version( _content )
|
this.create_version( _options.content )
|
||||||
}
|
}
|
||||||
|
|
||||||
create_version( content )
|
create_version( content )
|
||||||
@@ -38,9 +45,21 @@ class File
|
|||||||
return this.versions[ this.versions.length - 1 ]
|
return this.versions[ this.versions.length - 1 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
describe()
|
||||||
|
{
|
||||||
|
// Set up
|
||||||
|
let result = {}
|
||||||
|
result.id = this.id
|
||||||
|
result.name = this.name
|
||||||
|
result.path = this.path
|
||||||
|
result.versions = this.versions
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
destructor()
|
destructor()
|
||||||
{
|
{
|
||||||
console.log( 'destruct' )
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
// Dependencies
|
// Dependencies
|
||||||
let paths = require( '../utils/paths.class.js' ),
|
let paths = require( '../utils/paths.class.js' ),
|
||||||
|
ids = require( '../utils/ids.class.js' ),
|
||||||
util = require( 'util' ),
|
util = require( 'util' ),
|
||||||
slug = require( 'slug' ),
|
slug = require( 'slug' ),
|
||||||
File = require( './file.class.js' )
|
File = require( './file.class.js' )
|
||||||
@@ -12,6 +13,7 @@ class Project
|
|||||||
{
|
{
|
||||||
this.name = options.name
|
this.name = options.name
|
||||||
this.slug = slug( this.name )
|
this.slug = slug( this.name )
|
||||||
|
this.files = {}
|
||||||
this.folders = {
|
this.folders = {
|
||||||
'.':
|
'.':
|
||||||
{
|
{
|
||||||
@@ -36,7 +38,7 @@ class Project
|
|||||||
console.log( 'socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan )
|
console.log( 'socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan )
|
||||||
|
|
||||||
this.socket.emit( 'update_project', this.describe() )
|
this.socket.emit( 'update_project', this.describe() )
|
||||||
} );
|
} )
|
||||||
}
|
}
|
||||||
|
|
||||||
create_file( _path, _content )
|
create_file( _path, _content )
|
||||||
@@ -56,15 +58,18 @@ class Project
|
|||||||
let folder = this.get_folder( parsed_path.dir, true )
|
let folder = this.get_folder( parsed_path.dir, true )
|
||||||
|
|
||||||
// Create
|
// Create
|
||||||
file = new File( parsed_path.base )
|
file = new File( {
|
||||||
folder.files[ file.name ] = file
|
name : parsed_path.base,
|
||||||
|
path : parsed_path.dir,
|
||||||
|
content: _content
|
||||||
|
} )
|
||||||
|
|
||||||
// Has content
|
// Save
|
||||||
if( _content )
|
folder.files[ file.name ] = file.id
|
||||||
file.create_version( _content )
|
this.files[ file.id ] = file
|
||||||
|
|
||||||
// Emit
|
// Emit
|
||||||
this.socket.emit( 'update_project', this.describe() )
|
this.socket.emit( 'update_file', file.describe() )
|
||||||
|
|
||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
@@ -87,17 +92,20 @@ class Project
|
|||||||
return false
|
return false
|
||||||
|
|
||||||
// Retrieve file
|
// Retrieve file
|
||||||
let file = folder.files[ parsed_path.base ]
|
let file_id = folder.files[ parsed_path.base ]
|
||||||
|
|
||||||
// File found
|
// File found
|
||||||
if( file )
|
if( file_id )
|
||||||
|
{
|
||||||
|
let file = this.files[ file_id ]
|
||||||
return file
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
// Force creation
|
// Force creation
|
||||||
if( _force_creation )
|
if( _force_creation )
|
||||||
{
|
{
|
||||||
// Create folders
|
// Create folders
|
||||||
file = this.create_file( normalized_path )
|
let file = this.create_file( normalized_path )
|
||||||
|
|
||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
@@ -118,7 +126,9 @@ class Project
|
|||||||
file.create_version( _content )
|
file.create_version( _content )
|
||||||
|
|
||||||
// Emit
|
// Emit
|
||||||
this.socket.emit( 'update_project', this.describe() )
|
this.socket.emit( 'update_file', file.describe() )
|
||||||
|
|
||||||
|
return file
|
||||||
}
|
}
|
||||||
|
|
||||||
delete_file( _path )
|
delete_file( _path )
|
||||||
@@ -132,17 +142,21 @@ class Project
|
|||||||
// Folder found
|
// Folder found
|
||||||
if( folder )
|
if( folder )
|
||||||
{
|
{
|
||||||
let file = folder.files[ parsed_path.base ]
|
let file_id = folder.files[ parsed_path.base ]
|
||||||
|
|
||||||
// File found
|
// File found
|
||||||
if( file )
|
if( file_id )
|
||||||
{
|
{
|
||||||
|
let file = this.files[ file_id ]
|
||||||
|
|
||||||
// Delete file
|
// Delete file
|
||||||
file.destructor()
|
file.destructor()
|
||||||
delete folder.files[ parsed_path.base ]
|
delete folder.files[ file.name ]
|
||||||
|
delete this.files[ file.id ]
|
||||||
|
|
||||||
// Emit
|
// Emit
|
||||||
this.socket.emit( 'update_project', this.describe() )
|
this.socket.emit( 'delete_file', file_id )
|
||||||
|
this.socket.emit( 'update_folders', this.describe_folders )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -174,7 +188,7 @@ class Project
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Emit
|
// Emit
|
||||||
this.socket.emit( 'update_project', this.describe() )
|
this.socket.emit( 'update_folders', this.describe_folders() )
|
||||||
|
|
||||||
return folder
|
return folder
|
||||||
}
|
}
|
||||||
@@ -230,38 +244,79 @@ class Project
|
|||||||
return false
|
return false
|
||||||
|
|
||||||
// Recursive callback
|
// Recursive callback
|
||||||
let delete_folder = function( folder )
|
let empty_folder = ( folder ) =>
|
||||||
{
|
{
|
||||||
|
// Each folder
|
||||||
for( let _folder_name in folder.folders )
|
for( let _folder_name in folder.folders )
|
||||||
{
|
{
|
||||||
delete_folder( folder.folders[ _folder_name ] )
|
empty_folder( folder.folders[ _folder_name ] )
|
||||||
|
|
||||||
|
// Delete folder
|
||||||
|
delete folder.folders[ _folder_name ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Each file
|
||||||
for( let _file_name in folder.files )
|
for( let _file_name in folder.files )
|
||||||
{
|
{
|
||||||
let file = folder.files[ _file_name ]
|
// Set up
|
||||||
|
let file_id = folder.files[ _file_name ],
|
||||||
|
file = this.files[ file_id ]
|
||||||
|
|
||||||
file.destructor()
|
// File found
|
||||||
delete folder.files[ _file_name ]
|
if( file )
|
||||||
|
{
|
||||||
|
// Delete file
|
||||||
|
this.delete_file( file.path.full )
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
delete_folder( folder )
|
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
|
// Emit
|
||||||
this.socket.emit( 'update_project', this.describe() )
|
this.socket.emit( 'update_folders', this.describe_folders() )
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
describe()
|
describe()
|
||||||
{
|
{
|
||||||
|
// Set up
|
||||||
let result = {}
|
let result = {}
|
||||||
result.name = this.name
|
result.name = this.name
|
||||||
result.folders = this.folders
|
result.folders = this.describe_folders()
|
||||||
|
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
|
||||||
|
|||||||
@@ -94,9 +94,30 @@ application.factory(
|
|||||||
console.log('connected');
|
console.log('connected');
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
socket.on( 'update_file', function( data )
|
||||||
|
{
|
||||||
|
console.log('update_file');
|
||||||
|
console.log(data);
|
||||||
|
} );
|
||||||
|
|
||||||
|
socket.on( 'delete_file', function( data )
|
||||||
|
{
|
||||||
|
console.log('delete_file');
|
||||||
|
console.log(data);
|
||||||
|
} );
|
||||||
|
|
||||||
|
socket.on( 'update_folders', function( data )
|
||||||
|
{
|
||||||
|
console.log('update_folders');
|
||||||
|
console.log(data);
|
||||||
|
} );
|
||||||
|
|
||||||
// Update project event
|
// Update project event
|
||||||
socket.on( 'update_project', function( data )
|
socket.on( 'update_project', function( data )
|
||||||
{
|
{
|
||||||
|
console.log('update_folders');
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
// Reformat versions
|
// Reformat versions
|
||||||
result.each_version( data, function( version )
|
result.each_version( data, function( version )
|
||||||
{
|
{
|
||||||
@@ -140,7 +161,7 @@ application.factory(
|
|||||||
} );
|
} );
|
||||||
};
|
};
|
||||||
|
|
||||||
result.data = {"name":"","folders":{}};
|
result.data = {'name':'',folders:{},files:{}};
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ section.page-project {position:fixed;top:60px;right:0;bottom:0;left:0;}
|
|||||||
section.page-project .aside {position:absolute;top:0;bottom:0;left:0;width:240px;}
|
section.page-project .aside {position:absolute;top:0;bottom:0;left:0;width:240px;}
|
||||||
section.page-project section.file {position:absolute;top:0;right:0;bottom:0;left:240px;}
|
section.page-project section.file {position:absolute;top:0;right:0;bottom:0;left:240px;}
|
||||||
section.page-project section.file .file-header {position:absolute;top:0;right:0;left:0;height:30px;line-height:30px;padding:0 0 0 10px;background:#00f;}
|
section.page-project section.file .file-header {position:absolute;top:0;right:0;left:0;height:30px;line-height:30px;padding:0 0 0 10px;background:#00f;}
|
||||||
section.page-project section.file .versions-nav {position:absolute;top:30px;bottom:0;left:0;width:140px;background:#f0f;}
|
section.page-project section.file .versions-nav {position:absolute;top:30px;bottom:0;left:0;width:140px;background:#555;}
|
||||||
section.page-project section.file .code {position:absolute;top:30px;right:0;bottom:0;left:140px;background:#ff0;}
|
section.page-project section.file .code {position:absolute;top:30px;right:0;bottom:0;left:140px;background:#ff0;}
|
||||||
section.page-project section.file .code pre code {position:absolute;top:0;right:0;bottom:0;left:0;}
|
section.page-project section.file .code pre code {position:absolute;top:0;right:0;bottom:0;left:0;}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
class IDs
|
||||||
|
{
|
||||||
|
constructor()
|
||||||
|
{
|
||||||
|
this.last_id = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
get_id()
|
||||||
|
{
|
||||||
|
return ++this.last_id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let ids = new IDs()
|
||||||
|
|
||||||
|
module.exports = ids
|
||||||
Reference in New Issue
Block a user