-
Notifications
You must be signed in to change notification settings - Fork 154
/
build
executable file
·82 lines (66 loc) · 2.32 KB
/
build
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
#!/usr/bin/env node
const async = require('async');
const path = require('path');
const yargs = require('yargs');
const ParseComments = require('parse-comments');
const showdown = require('showdown');
const fs = require('fs');
const glob = require('glob');
const _ = require('lodash');
const argv = yargs
.option('allRules', {
alias: 'a',
default: false
})
.option('outputPath', {
alias: 'o',
default: __dirname + '/rules.json'
})
.argv;
const files = glob.sync(__dirname + '/src/rules/*.js');
const parseComments = new ParseComments();
const converter = new showdown.Converter();
showdown.setFlavor('github');
async.reduce(files, [], (rules, file, callback) => {
fs.readFile(file, 'utf8', (err, data) => {
if (err) return callback(err);
const parsed = parseComments.parse(data)[0];
// strip the opening comment and remove extra newlines
const code = data.split('\n').slice(parsed.loc.end.line).join('\n').trim();
const ruleTags = {};
parsed.tags.forEach((tag) => ruleTags[tag.title] = tag.description);
if (argv.allRules || ruleTags.gallery === 'true') {
const ruleExample = parsed.examples.reduce((prev, curr) => "\n\n" + curr.description + "\n<pre><code>" + curr.value + "</code></pre>" + prev, "");
const ruleDescription = converter.makeHtml(parsed.description + ruleExample);
if (!ruleDescription) {
return callback(new Error(`Blank description for ${file}`));
}
const rule = {
id: path.basename(file).replace('.js', ''),
title: ruleTags.title,
overview: ruleTags.overview,
categories: ruleTags.category ? ruleTags.category.split(',') : ['default'],
description: ruleDescription,
code
};
rules.push(rule);
}
return callback(null, rules);
});
}, (err, rules) => {
if (err) {
console.log(err.stack);
process.exit(1);
}
const expandedRules = _.chain(rules)
.flatMap(rule => _.map(rule.categories, category => _.assign(_.clone(rule), { category })))
.groupBy(r => r.category)
.map((rules, category) => ({
name: category,
templates: _.map(rules, rule => _.omit(rule, 'category'))
}))
.value();
console.log(`rules written to ${argv.outputPath}`);
fs.writeFileSync(argv.outputPath, JSON.stringify(expandedRules, null, 2));
process.exit(0);
});