-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
78 lines (59 loc) · 2.1 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
const PLUGIN = 'gulp-mson-to-json-schema';
var boutique = require('boutique');
var gutil = require('gulp-util');
var objectPath = require('object-path').get;
var Path = require('path');
var protagonist = require('protagonist');
var through = require('through2');
function getDataStructures (ast) {
var matches = [];
ast.content.forEach(function (section) {
if (section.element !== 'category') return;
section.content.forEach(function (element) {
if (element.element !== 'dataStructure') return;
matches.push(element);
});
});
return matches;
};
module.exports = function (config) {
var stream = through.obj(function (input, encoding, callback) {
if (input.stat.isDirectory()) return;
var self = this;
var filename = Path.basename(input.path);
var mson = input.contents.toString();
if (!mson.length) return;
protagonist.parse(mson, function (err, result) {
if (err) {
console.error(new gutil.PluginError(PLUGIN, err).toString());
callback();
};
result.warnings.forEach(function (warning) {
console.error(new gutil.PluginError(PLUGIN, warning).toString());
});
getDataStructures(result.ast).forEach(function (ast) {
var schema = boutique.represent({ ast: ast, contentType: 'application/schema+json' }, function (err, body) {
if (err) console.error(new gutil.PluginError(PLUGIN, err).toString());
self.push(new gutil.File({
base: '/',
cwd: '/',
path: '/' + ast.name.literal + '.json',
contents: new Buffer(body)
}));
});
if (!config || !config.samples) return;
var sample = boutique.represent({ ast: ast, contentType: 'application/json' }, function (err, body) {
if (err) console.error(new gutil.PluginError(PLUGIN, err).toString());
self.push(new gutil.File({
base: '/',
cwd: '/',
path: '/sample-' + ast.name.literal + '.json',
contents: new Buffer(body)
}));
});
});
callback();
});
});
return stream;
};