Handle same name projects
This commit is contained in:
+25
-8
@@ -29,18 +29,24 @@ class App
|
||||
this.set_server()
|
||||
this.set_socket()
|
||||
this.set_models()
|
||||
this.set_dummy()
|
||||
}
|
||||
|
||||
/**
|
||||
* Set models
|
||||
* Set Dummy
|
||||
*/
|
||||
set_models()
|
||||
set_dummy()
|
||||
{
|
||||
// Set up
|
||||
this.projects = new Projects( { socket: this.sockets.main } )
|
||||
// Default project
|
||||
let project = this.projects.create_project( 'dummy' )
|
||||
|
||||
// Tests
|
||||
var project = this.projects.create_project( 'dummy' )
|
||||
// Same name projects
|
||||
// let project_2 = this.projects.create_project( 'dummy' )
|
||||
// let project_3 = this.projects.create_project( 'dummy' )
|
||||
// let project_4 = this.projects.create_project( 'dummy' )
|
||||
// let project_5 = this.projects.create_project( 'dummy' )
|
||||
|
||||
// Some file
|
||||
project.files.create( './coucou/coco.txt', '1234' )
|
||||
project.files.create( './test-1.txt', 'content 1' )
|
||||
project.files.update( './test-1.txt', 'content 2' )
|
||||
@@ -50,13 +56,24 @@ class App
|
||||
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.files.delete( './toto/tata/ipsum.txt' )
|
||||
|
||||
var counting = 0
|
||||
// Incrementing
|
||||
let counting = 0
|
||||
setInterval( function()
|
||||
{
|
||||
project.files.update( './coucou/coco.txt', 'test: ' + counting++ )
|
||||
}, 2000 )
|
||||
|
||||
console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) )
|
||||
// Log
|
||||
// console.log( util.inspect( project.files.describe(), { depth: null, colors: true } ) )
|
||||
}
|
||||
|
||||
/**
|
||||
* Set models
|
||||
*/
|
||||
set_models()
|
||||
{
|
||||
// Set up
|
||||
this.projects = new Projects( { socket: this.sockets.main } )
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,14 +8,18 @@ class Project
|
||||
{
|
||||
constructor( options )
|
||||
{
|
||||
this.name = options.name
|
||||
this.slug = slug( this.name )
|
||||
|
||||
this.set_name( options.name )
|
||||
this.set_socket( options.socket )
|
||||
|
||||
this.files = new Files( { socket: this.socket } )
|
||||
}
|
||||
|
||||
set_name( _name )
|
||||
{
|
||||
this.name = _name
|
||||
this.slug = slug( this.name )
|
||||
}
|
||||
|
||||
set_socket( socket )
|
||||
{
|
||||
// Set up
|
||||
|
||||
@@ -23,13 +23,35 @@ class Projects
|
||||
console.log( 'socket projects'.green.bold + ' - ' + 'connect'.cyan + ' - ' + socket.id.cyan )
|
||||
|
||||
this.socket.emit( 'update_projects', this.describe() )
|
||||
} );
|
||||
} )
|
||||
}
|
||||
|
||||
create_project( _name )
|
||||
{
|
||||
// Create project
|
||||
let project = new Project( { name: _name, socket: this.original_socket } )
|
||||
let project = new Project( { name: _name, socket: this.original_socket } ),
|
||||
same_name_project = this.all[ project.slug ]
|
||||
|
||||
// Try to found same name project
|
||||
while( typeof same_name_project !== 'undefined' )
|
||||
{
|
||||
// Found new number
|
||||
let last_number = same_name_project.name.match(/\d+$/),
|
||||
new_number = 2
|
||||
|
||||
if( last_number && last_number.length )
|
||||
{
|
||||
last_number = ~~last_number[ 0 ]
|
||||
|
||||
new_number = last_number + 1
|
||||
}
|
||||
|
||||
// Update project name
|
||||
project.set_name( _name + ' ' + new_number )
|
||||
|
||||
// Try to found
|
||||
same_name_project = this.all[ project.slug ]
|
||||
}
|
||||
|
||||
// Save
|
||||
this.all[ project.slug ] = project
|
||||
|
||||
Reference in New Issue
Block a user