forked from auth0/rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·70 lines (58 loc) · 1.84 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
#!/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 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(data)[0];
// strip the opening comment and remove extra newlines
const code = data.split('\n').slice(parsed.comment.end).join('\n').trim();
if (argv.allRules || parsed.gallery === 'true') {
const rule = {
id: path.basename(file).replace('.js', ''),
title: parsed.title,
overview: parsed.overview,
categories: parsed.category ? parsed.category.split(',') : ['default'],
description: converter.makeHtml(parsed.example),
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);
});