-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (157 loc) · 3.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* !
* github-finder
* GitHub finder
* Copyright (c) 2012 Enrico Marino and Federico Spini
* MIT License
*/
/**
* Module dependencies.
*/
var request = require('superagent');
var async = require('async');
var Emitter = require('events').EventEmitter;
var join = require('path').join;
var toString = {}.toString;
module.exports = finder;
/**
* finder
* create a finder
*
* @param {Object} options options.
* @param {String} [client_id] GitHub application Client ID.
* @param {String} [client_id] GitHub application Client Secret.
* @return {Finder} finder.
* @api public
*/
function finder(options) {
return new Finder(options);
}
finder.Finder = Finder;
/**
* Finder
* create a finder
*
* @param {Object} options options.
* @param {String} [client_id] GitHub application Client ID.
* @param {String} [client_id] GitHub application Client Secret.
* @return {Finder} finder.
* @api public
*/
function Finder(options) {
Emitter.call(this)
options = options || {};
var client_id = options.client_id;
var client_secret = options.client_secret;
if (client_id && client_secret) {
this.credentials = {
client_id: client_id,
client_secret: client_secret
};
}
};
/**
* Inherit from `EventEmitter.prototype`.
*/
Finder.prototype = new Emitter;
Finder.prototype.constructor = Finder;
/**
* open
* open a GitHub project.
*
* @param {Object} options options.
* @param {String} user user.
* @param {String} project project.
* @param {String} [path=''] path.
* @return {Finder} finder.
* @api public
*/
Finder.prototype.open = function(options, callback) {
callback = callback || function() {};
var self = this;
this.get(options, function() {
self.emit('end');
callback();
});
return this;
};
/**
* get
* Get a directory or a file
*
* @param {Object} options options.
* @param {String} user user.
* @param {String} project project.
* @param {String} [path=''] path.
* @param {Function} callback callback.
* @param {Object} err error.
* @param {Object} data directory info.
* @api public
*/
Finder.prototype.get = function(options, callback) {
var self = this;
var user = options.user;
var project = options.project;
var path = options.path;
var fullpath = join(user, project, 'contents', path);
var url = 'https://api.github.com/repos/' + fullpath;
var query = {};
if (this.credentials) {
query = this.credentials;
}
request
.get(url)
.query(query)
.end(function(err, res) {
if (err) {
throw err;
}
self.read(options, res.body, callback);
});
};
/**
* read
* Read the item
*
* @param {Object} options options.
* @param {String} user user.
* @param {String} project project.
* @param {String} [path=''] path.
* @param {Object} item file or directory.
* @param {Function} callback callback.
* @param {Object} err error.
* @api public
*/
Finder.prototype.read = function(options, item, callback) {
var is_dir = '[object Array]' === toString.call(item);
if (is_dir) {
this.emit('dir', item);
this.readdir(options, item, callback);
return;
}
this.emit('file', item);
callback();
};
/**
* readdir
* Read directory information
*
* @param {Object} options options.
* @param {String} user user.
* @param {String} project project.
* @param {String} [path=''] path.
* @param {Array} dir directory info.
* @param {Function} callback callback.
* @param {Object} err error object.
* @param {Object} obj the object of parsed files.
*/
Finder.prototype.readdir = function(options, dir, callback) {
var self = this;
async.forEach(dir, function(item, callback) {
var options_clone = {
user: options.user,
project: options.project,
path: item.path
};
self.get(options_clone, callback);
}, callback);
};