Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
22 changes: 18 additions & 4 deletions lib/github.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,7 +16,6 @@ function Organization(options) {
this.token = options.token;
this.regexp = options.regexp;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops. That was an accident, will fix.

this.gitaccess = options.gitaccess;
this.gitsettings = options.gitsettings;

if (options.only && options.regexp) {
this.only = new RegExp(options.only);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

} 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 {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you leave this else in the previous line? Just a coding style preference 😄

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Will fix.

this.executeCloneCommand(repo);
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
});