-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcreate-examples.js
More file actions
34 lines (28 loc) · 877 Bytes
/
create-examples.js
File metadata and controls
34 lines (28 loc) · 877 Bytes
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
const fs = require('fs');
const R = require('ramda');
const vfs = require('vinyl-fs');
const through2 = require('through2');
const src = './examples/**/*.js';
const dest = './examples/example.json';
const stream = vfs.src(src);
const examplesList = [];
const options = {
objectMode: true
};
const groupByCategory = R.groupBy(R.prop('category'));
const transform = function transform(chunk, enc, callback) {
const arr = chunk.path.split('/');
const obj = {
category: arr[6],
title: arr[7].replace('.js', ''),
code: chunk.contents.toString()
};
examplesList.push(obj);
callback();
};
const flush = function flush(callback) {
const object = groupByCategory(examplesList);
this.push(JSON.stringify(object, null, 2));
callback();
};
stream.pipe(through2(options, transform, flush)).pipe(fs.createWriteStream(dest));