-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
381 lines (326 loc) · 9.92 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* mdlint
* https://github.com/ChrisWren/mdlint
*
* Copyright (c) 2013 Chris Wren
* Licensed under the MIT license.
*/
'use strict';
var spawn = require('child_process').spawn;
var fs = require('fs');
require('colors');
var program = require('commander');
var esprima = require('esprima');
var glob = require('glob');
var _ = require('lodash');
var request = require('request');
var headers = {
// GitHub API requires the User-Agent Header to be set
'User-Agent': 'mdlint'
};
// Location of token file to generate when user authenticates
var tokenFile = __dirname + '/authtoken.txt';
// Keep track of the number of files to parse so that if all files pass a success message can be logged
var numFilesToParse = 0;
// Keep track of the number of failed files to change the file break color for readability
var numFailedFiles = 0;
module.exports = function () {
// Use Auth Token if present
if (fs.existsSync(tokenFile)) {
headers.Authorization = 'token ' + fs.readFileSync(tokenFile, 'utf8');
}
program
.version(require('./package.json').version)
.option('-v, --verbose', 'report linting for all files');
program
.command('repo <repo>')
.description('lints a README from a GitHub repo')
.action(function (repo) {
fetchRepoREADME(repo);
});
program
.command('user <username>')
.description('lints all READMEs from a user\'s GitHub repos')
.action(function (user) {
fetchUserREADMEs(user);
});
program
.command('glob <glob>')
.description('lints local markdown files that match a file glob')
.action(function (fileGlob) {
var files = glob.sync(fileGlob);
numFilesToParse = files.length;
files.forEach(function (file) {
lintMarkdown(fs.readFileSync(file, 'utf8'), file);
});
if (numFailedFiles > 0) {
process.exit(1);
} else if (!program.verbose) {
console.log('All files passed linting.');
}
});
program
.command('query <query>')
.option('--page', 'The page of results to return. Defaults to 0.')
.description('lints READMEs from repos returned by a GitHub query.')
.action(function (query) {
request({
uri: 'https://api.github.com/legacy/repos/search/' +
query +
'?language=JavaScript' +
'&start_page=' + program.page || '0',
headers: headers
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
JSON.parse(body)
.repositories
.forEach(function (repo) {
fetchRepoREADME(repo.owner + '/' + repo.name);
});
} else {
if (response.headers['x-ratelimit-remaining'] === '0') {
getAuthToken();
} else {
console.log(body);
console.log(response.headers);
console.log('Unable to reach the GitHub API :('.red);
}
return;
}
});
});
// Process implicit commands
program
.command('*')
.action(function (command) {
if (command.indexOf('*') !== -1 || command.indexOf('.') !== -1) {
var files = glob.sync(command);
numFilesToParse = files.length;
files.forEach(function (file) {
lintMarkdown(fs.readFileSync(file, 'utf8'), file);
});
if (numFailedFiles > 0) {
process.exit(1);
}
} else if (command.indexOf('/') !== -1) {
fetchRepoREADME(command);
} else {
fetchUserREADMEs(command);
}
});
program
.parse(process.argv);
if (!program.args.length) {
program.help();
}
};
/**
* Fetches READMEs from a user's GitHub repos
* @param {String} GitHub username
*/
function fetchUserREADMEs (user) {
request({
uri: 'https://api.github.com/users/' + user + '/repos',
headers: headers
}, function (error, response, body) {
var responseBody = JSON.parse(body);
if (responseBody.message) {
console.log('Error: the following user was not found: '.red + user.blue);
return;
}
JSON.parse(body)
.forEach(function (repo) {
fetchRepoREADME(repo.full_name);
});
});
}
/**
* Fetches a README from GitHub
* @param {String} repo URL of repo to fetch
*/
function fetchRepoREADME (repo) {
request({
uri: 'https://api.github.com/repos/' + repo + '/readme',
headers: _.extend(headers, {
// Get raw README content
'Accept': 'application/vnd.github.VERSION.raw'
})
},
function (error, response, body) {
if (!error && response.statusCode === 200) {
lintMarkdown(body, repo);
} else {
if (response.headers['x-ratelimit-remaining'] === '0') {
getAuthToken();
} else {
console.log('README for https://github.com/' + repo.blue + ' not found.'.red);
return;
}
}
});
}
/**
* [parseMarkdown description]
* @param {String} markdownContent Content of markdown file
* @return {Array} Array of objects with a language and code property
*/
function parseMarkdown (markdownContent) {
var codeBlocks = markdownContent
.split('```')
.filter(function (codeBlock, index) {
// Delete the text in between code sections
if (index % 2 === 0 ||
// Remove non JavaScript and JSON code blocks
(codeBlock.trim().toLowerCase().indexOf('js') !== 0 &&
codeBlock.trim().toLowerCase().indexOf('javascript') !== 0)) {
return false;
}
return true;
})
.map(function (codeBlock) {
return {
lang: codeBlock.split('\n')[0].trim().toLowerCase(),
code: codeBlock.slice(codeBlock.indexOf('\n') + 1)
};
});
return codeBlocks;
}
// Boolean to keep track if the file break has been logged when discovering multiple errors in a single file
var didLogFileBreak;
/**
* Parses the JavaScript code blocks from the markdown file
* @param {String} body Body of markdown file
* @param {String} file Filename
*/
function lintMarkdown (body, file) {
var codeBlocks = parseMarkdown(body);
didLogFileBreak = false;
var failedCodeBlocks = _.reject(_.compact(codeBlocks), function (codeBlock) {
return validateCodeBlock(codeBlock, file);
});
numFilesToParse--;
if (failedCodeBlocks.length === 0) {
if (program.verbose) {
console.log('Markdown passed linting for '.green + file.blue.bold + '\n');
} else if (numFilesToParse === 0) {
console.log('All markdown files passed linting'.green);
}
} else {
if (numFailedFiles % 2 === 0) {
console.log('Markdown failed linting for '.red + file.yellow);
} else {
console.log('Markdown failed linting for '.red + file.blue);
}
numFailedFiles++;
console.log('');
}
}
/**
* Logs a break between files for readability
* @param {String} text Text to log
*/
function logFileBreak (text) {
if (numFailedFiles % 2 === 0) {
console.log(text.yellow.inverse);
} else {
console.log(text.blue.inverse);
}
}
/**
* Validates that code blocks are valid JavaScript
* @param {Object} code A block of code from the markdown file containg the lang and code
* @param {String} file Name of file currently being validated
*/
function validateCodeBlock (codeBlock, file) {
var lang = codeBlock.lang;
var code = codeBlock.code;
if (lang === 'json') {
try {
JSON.parse(code);
} catch (e) {
console.log(e);
console.log(code);
return false;
}
return true;
} else if (lang === 'js' || lang === 'javascript') {
code = preprocessCode(code);
try {
esprima.parse(code, { tolerant: true });
} catch (e) {
// Get indeces from lineNumber and column
var line = e.lineNumber - 1;
var column = e.column - 1;
// Highlight error in code
code = code.split('\n');
code[line] = code[line].slice(0, column).magenta +
code[line][column].red +
code[line].slice(column + 1).magenta;
code = code.join('\n');
if (!didLogFileBreak) {
logFileBreak(file);
didLogFileBreak = true;
}
console.log(e);
console.log(code);
return false;
}
return true;
}
}
/**
* Retrieves an auth token so that the user can exceed the uauthenticated rate limit
*/
function getAuthToken () {
console.log('You have hit the rate limit for unauthenticated requests, please log in to raise your rate limit:\n'.red);
program.prompt('GitHub Username: ', function (user) {
console.log('\nAfter entering your password, hit return' + ' twice.\n'.green);
var authProcess = spawn('curl', [
'-u',
user,
'-d',
'{"scopes":["repo"],"note":"mdlint"}',
'-s',
'https://api.github.com/authorizations'
], {
stdio: [process.stdin, 'pipe', process.stderr]
});
authProcess.stdout.setEncoding('utf8');
authProcess.stdout.on('data', function (data) {
var response = JSON.parse(data);
if (response.message) {
console.log(response.message.red + '\n');
process.exit();
} else {
fs.writeFileSync(tokenFile, response.token);
console.log('Authenticated :). Now try your lint again. \n'.green);
process.exit();
}
});
});
}
/**
* Preprocesses the code block and re-formats it to allow for partial code
* @param {String} code A block of code from the markdown file
* @return {String} Processed code transformed from partial code
*/
function preprocessCode (code) {
// Remove starting comments
while (code.indexOf('//') === 0) {
code = code.slice(code.indexOf('\n'));
}
// Starts with an object literal
if (code.indexOf('{') === 0) {
code = 'var json = ' + code;
// Starts with an object property
} else if (code.indexOf(':') !== -1 &&
code.indexOf(':') < code.indexOf(' ')) {
code = 'var json = {' + code + '}';
}
// Starts with an anonymous function
if (code.indexOf('function') === 0) {
code = 'var func = ' + code;
}
// Contains ...
return code.replace('...', '');
}