diff --git a/index.js b/index.js index 1f71d13..3be586c 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,7 @@ cli.parse({ type: ['t', 'can be one of: all, public, private, forks, sources, member', 'string', 'all'], exclude: ['e', 'Exclude passed repos, comma separated', 'string'], only: ['o', 'Only clone passed repos, comma separated', 'string'], + directories: ['d', 'Check for existing directories before cloning', 'boolean', false], regexp: ['r', 'If true, exclude or only option will be evaluated as a regexp', 'boolean', false], username: ['u', 'Username for basic authentication. Required to access github api', 'string'], token: ['token', 'Token authentication. Required to access github api', 'string'], diff --git a/lib/github.js b/lib/github.js index 103ba02..c777a25 100644 --- a/lib/github.js +++ b/lib/github.js @@ -1,6 +1,8 @@ var spawn = require('child_process').spawn , cli = require('cli').enable('help', 'status', 'version') - , request = require('request').defaults({ jar: true }); + , request = require('request').defaults({ jar: true }) + , fs = require('fs') + , path = require('path'); var github = exports; @@ -14,7 +16,6 @@ function Organization(options) { this.token = options.token; this.regexp = options.regexp; this.gitaccess = options.gitaccess; - this.gitsettings = options.gitsettings; if (options.only && options.regexp) { this.only = new RegExp(options.only); @@ -27,11 +28,20 @@ function Organization(options) { } else if (options.exclude) { this.exclude = options.exclude.split(','); } + if (options.directories) { + this.existingDirs = this.getDirectories(process.cwd()); + } this.type = options.type; this.nextPageUrl = this.getRequestUri(); } +Organization.prototype.getDirectories = function(dir) { + return fs.readdirSync(dir).filter(function(subpath) { + return fs.statSync(path.join(dir, subpath)).isDirectory(); + }); +} + Organization.prototype.getRequestUri = function() { return this.BASE_URI + this.organization + '/repos' + '?per_page=' + this.perpage + '&type=' + this.type; } @@ -105,15 +115,19 @@ Organization.prototype.parseLinks = function(response) { } Organization.prototype.clone = function(repo) { + if (this.exclude && this.regexp && repo.name.match(this.exclude)) { return cli.debug('Repository ' + repo.name + ' ignored, exclude option: ' + this.exclude); } else if (this.exclude && !this.regexp && this.exclude.indexOf(repo.name) != -1) { return cli.debug('Repository ' + repo.name + ' ignored, exclude option: ' + this.exclude); } else if (this.only && this.regexp && !repo.name.match(this.only)) { - cli.debug('Repository ' + repo.name + ' ignored, only option: ' + this.only); + return cli.debug('Repository ' + repo.name + ' ignored, only option: ' + this.only); } else if (this.only && !this.regexp && this.only.indexOf(repo.name) == -1) { return cli.debug('Repository ' + repo.name + ' ignored, only option: ' + this.only); - } else { + } else if (this.existingDirs && this.existingDirs.indexOf(repo.name) > -1) { + return cli.info('Repository ' + repo.name + ' ignored, directory already found'); + } + else { this.executeCloneCommand(repo); } } diff --git a/test/github.js b/test/github.js index 20b7675..91a7679 100644 --- a/test/github.js +++ b/test/github.js @@ -220,5 +220,34 @@ describe('Organization', function() { }); }); }); + + describe('directory check mode', function() { + before(function() { + organization = new github.Organization({}); + organization.existingDirs = ['foozin', 'barzin']; + + cloneSpy = sinon.stub(organization, 'executeCloneCommand').returns(undefined); + }); + + it('executes clone command', function() { + var repo = { name: 'barbaz' }; + organization.clone(repo); + + repo = { name: 'bar' }; + organization.clone(repo); + + cloneSpy.callCount.should.equals(2); + }); + + it('does not execute clone command', function() { + var repo = { name: 'foozin' }; + organization.clone(repo); + + repo = { name: 'barzin' }; + organization.clone(repo); + + cloneSpy.callCount.should.equals(0); + }); + }); }); });