From c6127936ef24afa6b6301eb62ea22925ac5bfd3b Mon Sep 17 00:00:00 2001 From: Tyler Fisher Date: Thu, 5 Jan 2017 16:09:09 -0500 Subject: [PATCH 1/3] add -d flag, which checks if directory already exists inside directory where command was run --- index.js | 1 + lib/github.js | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) 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..6cf051e 100644 --- a/lib/github.js +++ b/lib/github.js @@ -1,6 +1,7 @@ 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'); var github = exports; @@ -14,7 +15,7 @@ function Organization(options) { this.token = options.token; this.regexp = options.regexp; this.gitaccess = options.gitaccess; - this.gitsettings = options.gitsettings; + this.directories = options.directories; if (options.only && options.regexp) { this.only = new RegExp(options.only); @@ -27,6 +28,10 @@ function Organization(options) { } else if (options.exclude) { this.exclude = options.exclude.split(','); } + if (options.directories) { + var dirs = p => fs.readdirSync(p).filter(f => fs.statSync(p+"/"+f).isDirectory()); + this.directories = dirs(process.cwd()); + } this.type = options.type; this.nextPageUrl = this.getRequestUri(); @@ -110,10 +115,13 @@ Organization.prototype.clone = function(repo) { } 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.directories && this.directories.indexOf(repo.name) > -1) { + return cli.info('Repository ' + repo.name + ' ignored, directory already found'); + } + else { this.executeCloneCommand(repo); } } From 1dd72cf883d67aec5ae699cc45b948b40a2b552a Mon Sep 17 00:00:00 2001 From: Tyler Fisher Date: Thu, 5 Jan 2017 16:32:47 -0500 Subject: [PATCH 2/3] add tests --- lib/github.js | 6 +++--- test/github.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/github.js b/lib/github.js index 6cf051e..a9a8d52 100644 --- a/lib/github.js +++ b/lib/github.js @@ -15,7 +15,6 @@ function Organization(options) { this.token = options.token; this.regexp = options.regexp; this.gitaccess = options.gitaccess; - this.directories = options.directories; if (options.only && options.regexp) { this.only = new RegExp(options.only); @@ -30,7 +29,7 @@ function Organization(options) { } if (options.directories) { var dirs = p => fs.readdirSync(p).filter(f => fs.statSync(p+"/"+f).isDirectory()); - this.directories = dirs(process.cwd()); + this.existingDirs = dirs(process.cwd()); } this.type = options.type; @@ -110,6 +109,7 @@ 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) { @@ -118,7 +118,7 @@ Organization.prototype.clone = function(repo) { 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 if (this.directories && this.directories.indexOf(repo.name) > -1) { + } else if (this.existingDirs && this.existingDirs.indexOf(repo.name) > -1) { return cli.info('Repository ' + repo.name + ' ignored, directory already found'); } else { 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); + }); + }); }); }); From 29fe7bdde176d1273bbfdb254fb3de5d9c2d67d2 Mon Sep 17 00:00:00 2001 From: Tyler Fisher Date: Thu, 5 Jan 2017 16:43:53 -0500 Subject: [PATCH 3/3] refactor to not use es6 syntax --- lib/github.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/github.js b/lib/github.js index a9a8d52..c777a25 100644 --- a/lib/github.js +++ b/lib/github.js @@ -1,7 +1,8 @@ var spawn = require('child_process').spawn , cli = require('cli').enable('help', 'status', 'version') , request = require('request').defaults({ jar: true }) - , fs = require('fs'); + , fs = require('fs') + , path = require('path'); var github = exports; @@ -28,14 +29,19 @@ function Organization(options) { this.exclude = options.exclude.split(','); } if (options.directories) { - var dirs = p => fs.readdirSync(p).filter(f => fs.statSync(p+"/"+f).isDirectory()); - this.existingDirs = dirs(process.cwd()); + 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; }