-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (61 loc) · 1.7 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
var fs = require("fs");
var marked = require("marked");
var path = require("path");
var debug = require("./lib/debug");
var runners = require("./lib/runners");
/**
* Readme tester runner
* @param {String} filepath path to the markdown file to be tested
* @param {Object} opts run options
* @property {Boolean} opts.bash whether to include bash parsing
* @param {Function} done callback when test is complete. Node callback signature
* @return {Void} no return value
*/
module.exports = function(filepath, opts, done) {
if(done === undefined) {
done = opts;
}
opts = opts || {};
var code = {};
var availLangs = ["js"]
if(opts.bash) {
availLangs.push("bash");
}
var dirname = path.dirname(filepath);
var md = fs.readFile(filepath, function(err, raw) {
if(err) return done(err);
var md = raw.toString();
var tokens = marked.lexer(md, {gfm: true});
tokens
.forEach(function(token) {
var lang = token.lang;
if(lang && availLangs.indexOf(lang) > -1 && token.type === "code") {
code[lang] = code[lang] || "";
code[lang] = code[lang] + token.text + "\n";
return "";
}
});
var toRun = [];
var toRun = Object.keys(code)
.map(function(lang) {
var runner = runners[lang];
if(!runner) {
debug("no runner for lang: %s", lang);
}
return function(done) {
runner(dirname, code[lang], done);
};
});
toRun.shift()(function fn(err) {
if(err) {
done(err);
}
else if(toRun.length > 0) {
toRun.shift()(fn)
}
else {
done();
}
});
})
}