-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
338 lines (291 loc) · 11 KB
/
app.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env node
var flatiron = require('flatiron'),
XRegExp = require('xregexp').XRegExp,
fs = require('fs'),
path = require('path'),
redis = require('redis'),
async = require('async'),
//broadway = require('broadway'),
app = flatiron.app,
botOptions = {};
app.config.file({ file: path.join(__dirname, 'config', 'config.json') });
var username = botOptions.username = app.config.get('username'),
password = botOptions.password = app.config.get('password'),
botname = botOptions.botname = app.config.get('bot-name'),
port = app.config.get('database:port'),
host = app.config.get('database:host'),
pass = app.config.get('database:password'),
npm_hash = app.config.get('database:npm_hash'),
totalNPMPackages = null,
totalRepositories = 0,
totalRepositoryMatches = 0,
repositoryMatchesList = [];
var redisClient = redis.createClient(port, host);
redisClient.auth(pass, function (err) {
if (err) {
throw err;
}
app.log.info("REDIS Authed!");
});
redisClient.on("error", function (err) {
app.log.error("REDIS " + err.message);
//return process.exit(1);
});
// ========================================= Settings =========================
var gitPullRequestMessageTitle = botOptions.gitPullRequestMessageTitle = "Hi! I fixed some code for you!",
gitPullRequestMessage = botOptions.gitPullRequestMessage = [
'Hi!',
'',
'',
'I am ' + botname,
'',
'Did you know that `path.{exists,existsSync}` was moved to `fs.{exists,existsSync}`, '
+ ' and that `tty.setRawMode(mode)` was moved to `tty.ReadStream#setRawMode()` '
+ '(i.e. `process.stdin.setRawMode()`) '
+ ' in node v0.8.0? Read more @[API changes between v0.6 and v0.8](https://github.com/joyent/node/wiki/API-changes-between-v0.6-and-v0.8) ',
'',
'I automatically made some changes I think will help you migrate your codebase to '
+ 'node v0.8.0, please review these changes and merge them if you feel they are '
+ 'useful, If they are not you can ignore this Pull Request.',
'',
'Have a Nice Day!',
'',
'--'
+ '[' + botname + '](https://github.com/blakmatrix/node-migrator-bot)'
].join('\n');
botOptions.changesList = [
{name: "path.exists",
message: '[fix] path.exists was moved to fs.exists',
func: function (fileList, settings, cb) {
async.map(fileList, function (file, callback) {
var re = /([^[0-9a-zA-Z_-]])path(\s*.\s*exists\s*[^[0-9a-zA-Z_-]])/g;
fileReplace(file, re, "$1fs$2", callback);
}, function (err, results) {
// results is now an array of stats for each file
if (err) {
app.log.error("path.exists Error: " + err);
return cb(null, 'DONE');
}
if (results.indexOf('OK') === -1) {
return cb(null, 'DONE');
} else {
return cb(null, 'OK');
}
});
}},
{name: "path.existsSync",
message: '[fix] path.existsSync was moved to fs.existsSync',
func: function (fileList, settings, cb) {
async.map(fileList, function (file, callback) {
var re = /([^[0-9a-zA-Z_-]])path(\s*.\s*existsSync\s*[^[0-9a-zA-Z_-]])/g;
fileReplace(file, re, "$1fs$2", callback);
}, function (err, results) {
// results is now an array of stats for each file
if (err) {
app.log.error("path.exists Error: " + err);
return cb(null, 'DONE');
}
if (results.indexOf('OK') === -1) {
return cb(null, 'DONE');
} else {
return cb(null, 'OK');
}
});
}},
{name: "tty.setRawMode",
message: '[fix] tty.setRawMode(mode) was moved to tty.ReadStream#setRawMode() (i.e. process.stdin.setRawMode())',
func: function (fileList, settings, cb) {
async.map(fileList, function (file, callback) {
var re = /([^[0-9a-zA-Z_-]])tty(\s*.\s*setRawMode\s*[^[0-9a-zA-Z_-]])/g;
fileReplace(file, re, "$1process.stdin$2", callback);
}, function (err, results) {
// results is now an array of stats for each file
if (err) {
app.log.error("path.exists Error: " + err);
return cb(null, 'DONE');
}
if (results.indexOf('OK') === -1) {
return cb(null, 'DONE');
} else {
return cb(null, 'OK');
}
});
}}
];
botOptions.filterList = function filterList(list, dir) {
var reInclude = /^(\w*((\.js)|(\.txt)|(\.md)|(\.markdown))?|readme.*)$/gi,
reExclude = /^(node_modules|\.git|)$/gi,
modList = list.filter(function (str) {return XRegExp.test(str, reInclude); })
.filter(function (str) {return !XRegExp.test(str, reExclude); });
return modList;
};
botOptions.dbAdd = function dbAdd(link, cb) {//no changes to make
redisClient.hset(npm_hash, link, 'processed');
return null;
};
var dbAddComplete = botOptions.dbAddComplete = function (link, cb) {//successful PR
totalRepositoryMatches++;
redisClient.hset(npm_hash, link, 'completed');
repositoryMatchesList.push(link);
return null;
};
botOptions.dbGetInfo = function dbGetInfo(cb) {
redisClient.hgetall(npm_hash, function (err, data) {
app.log.info('status repository');
app.log.info('--------- -------------------------------------------------------------');
for (var i in data) {
app.log.info(data[i] + ' ' + i);
++totalRepositories;
if (data[i] === 'completed') {
++totalRepositoryMatches;
}
}
return cb(null);
});
};
var dbGetCompleted = function (cb) {
redisClient.hgetall(npm_hash, function (err, data) {
app.log.info('');
app.log.info('List of matched repositories');
app.log.info('----------------------------------------------------------------------');
for (var i in data) {
if (data[i] === 'completed') {
app.log.info(i);
++totalRepositoryMatches;
}
++totalRepositories;
}
return cb(null);
});
};
botOptions.dbCheck = function dbCheck(link, cb) {
redisClient.hget(npm_hash, link, function (err, hashk_value) {
if (err) {
cb(err);
}
cb(null, hashk_value);
});
};
botOptions.makePullRequest = true;
botOptions.forkRepo = true;
botOptions.deleteRepo = true;
botOptions.addOnFailedCommit = true;
botOptions.addOnSuccessfulPR = true;
botOptions.setTotalNPMPackages = function setTotalNPMPackages(result) {
totalNPMPackages = result.length;
return null;
};
botOptions.setTotalRepositories = function setTotalRepositories(result) {
totalRepositories = result.length;
return null;
};
function setTotalRepositoryMatches(link) {
totalRepositoryMatches++;
dbAddComplete(link);
repositoryMatchesList.push(link);
return null;
}
function displayStats() {
app.log.info('================================================================================');
app.log.info('================================================================================');
app.log.info('Stats:');
if (totalNPMPackages) {
app.log.info('NPM package totals: ' + totalNPMPackages);
}
app.log.info('Github Repositories processed: ' + totalRepositories);
app.log.info('Github Repositories Matched: ' + totalRepositoryMatches);
app.log.info('Github Repositories Affected: ');
console.dir(repositoryMatchesList);
app.log.info('================================================================================');
}
function fileReplace(filename, re, replacement, cb) {
fs.readFile(filename, function (err, data) {
if (err) {
//return cb(err);
return cb(null, 'DONE');
}
var dataStr = data.toString(),
fixedDoc = '';
if (re.test(dataStr)) {
fixedDoc = dataStr.replace(re, replacement);
// write changes out to file
fs.writeFile(filename, fixedDoc, function (err) {
if (err) {
app.log.error('The file was not saved');
//return cb(err);
return cb(null, 'DONE');
} else {
app.log.info(filename.yellow.bold + ' was modified and changed!'.inverse.green);
return cb(null, 'OK');
}
});
} else {
app.log.debug('No ' + 'text to replace'.magenta.bold + ' found in ' + filename.yellow.bold);
return cb(null, 'DONE');
}
});
}
// ============================================================================
app.use(flatiron.plugins.cli, {
source: path.join(__dirname, 'lib', 'commands'),
usage: [
'',
'node-migrator-bot - Migrate your old Node.js Repos',
'',
'Usage:',
'',
' node-migrator-bot repo <myrepo> - Takes the URL link to your repository on',
' git hub, forks it, does its thing, then',
' initates a pull request. If a folder',
' path is given, runs file op for every file',
' node-migrator-bot user <user> - Takes a github username, forks all node.js',
' repositories, does its thing, then',
' initates a pull request on each repository',
' node-migrator-bot file <file> - runs the bot on the file provided',
' node-migrator-bot npm - runs the bot on npm packages with repos on ',
' github',
' node-migrator-bot db - displays all the processed repos in the db',
' node-migrator-bot use - You\'re looking at it!'
]
});
app.use(require("./lib/node-migrator-bot"), botOptions);
app.commands.repo = function repo(link, cb) {
this.log.info('Attempting to open path"' + link + '"');
app.doRepoUpdate(link, cb);
};
app.commands.delrepo = function delRepo(repo, cb) {
this.log.info('Attempting to open delete "' + username + '/' + repo + '"');
app.deleteRepo(repo, 'DONE', cb);
};
app.commands.db = function db(cb) {
this.log.info('Getting processed items in DB...');
app.getDBinfo(cb);
};
app.commands.dbcompleted = function dbcompleted(cb) {
this.log.info('Getting completed items in DB...');
dbGetCompleted(cb);
};
app.commands.npm = function npm(link, cb) {
this.log.warn('Running on all available npm repositories that are hosted on github!!!'.red.bold);
app.doNPMUpdate(cb);
};
app.commands.user = function user(user, cb) {
this.log.info('Attempting get information on "' + user + '"');
app.doUserRepoUpdateStart(user, cb);
};
app.commands.file = function file(filename, cb) {
this.log.info('Attempting to open "' + filename + '"');
app.makeFileChanges(filename, cb);
};
app.start(function (err) {
if (err) {
app.log.error(err.message || 'You didn\'t call any commands! Type <app> use to see use cases');
app.log.warn(botname.grey + ' NOT OK.');
redisClient.quit();
return process.exit(1);
}
redisClient.quit();
//Stats
displayStats();
app.log.info(botname.grey + ' ok'.green.bold);
});