-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (65 loc) · 2.57 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
79
80
var Writer = require('broccoli-caching-writer');
var walkSync = require('walk-sync');
var path = require('path');
var mkdirp = require('mkdirp');
var matter = require('gray-matter');
var objectMerge = require('object-merge');
var fs = require('fs');
var copyDereferenceSync = require('copy-dereference').sync;
module.exports = FrontMatterFilter;
FrontMatterFilter.prototype = Object.create(Writer.prototype);
FrontMatterFilter.prototype.constructor = FrontMatterFilter;
function FrontMatterFilter (inputTree, options) {
if (!(this instanceof FrontMatterFilter)) return new FrontMatterFilter(inputTree, options);
var defaultOptions = {
stripFrontMatter: false,
removeIfNoFrontMatter: false,
grayMatter: {}
};
this.options = objectMerge(defaultOptions, (options || {}));
this.inputTree = inputTree;
}
FrontMatterFilter.prototype.updateCache = function(srcDir, destDir) {
var includeCB = this.options.include;
var strip = this.options.stripFrontMatter;
var removeIfNoFrontMatter = this.options.removeIfNoFrontMatter;
var grayMatterOptions = this.options.grayMatter;
var hasIncludeCB = (typeof(includeCB) === 'function');
var filePaths = walkSync(srcDir);
filePaths.forEach(function(filePath) {
var srcFilePath = path.join(srcDir, filePath);
var destFilePath = path.join(destDir, filePath);
// handle directories
if (filePath.slice(-1) === '/') {
mkdirp.sync(destFilePath);
return;
}
var content = fs.readFileSync(srcFilePath, {encoding: 'utf8'});
var hasFrontMatter = matter.exists(content, grayMatterOptions);
if (!hasFrontMatter) {
if (!removeIfNoFrontMatter) {
// fs.writeFileSync(destFilePath, content);
_copyFile(srcFilePath, destFilePath)
}
} else {
var parsed = matter(content, grayMatterOptions);
if (hasIncludeCB && includeCB(parsed.data)) {
// fs.writeFileSync(destFilePath, (strip ? parsed.content : content));
_copyFile(srcFilePath, destFilePath, parsed.content, strip);
} else if (!hasIncludeCB) {
// pass through if include callback not provided
// fs.writeFileSync(destFilePath, (strip ? parsed.content : content));
_copyFile(srcFilePath, destFilePath, parsed.content, strip);
}
}
});
return destDir;
};
function _copyFile(srcFilePath, destFilePath, parsedContent, strip) {
if (strip) {
// Need to implement caching still
fs.writeFileSync(destFilePath, parsedContent);
} else {
copyDereferenceSync(srcFilePath, destFilePath);
}
}