forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.js
75 lines (68 loc) · 1.61 KB
/
fs.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
'use strict';
var fs = require('fs');
var path = require('path');
var util = require('handlebars-utils');
var utils = require('./utils');
var number = require('./number');
var helpers = module.exports;
/**
* Helper `fileSize` is deprecated. Use `helper.prettyBytes` instead.
*/
helpers.fileSize = number.bytes;
/**
* Read a file from the file system. This is useful in composing
* "include"-style helpers using sub-expressions.
*
* ```handlebars
* {{read "a/b/c.js"}}
* {{someHelper (read "a/b/c.md")}}
* ```
* @param {String} `filepath`
* @return {String}
* @api public
*/
helpers.read = function(filepath, options) {
return fs.readFileSync(filepath, 'utf8');
};
/**
* Return an array of files from the given
* directory.
*
* @param {String} `directory`
* @return {Array}
* @api public
*/
helpers.readdir = function(dir, filter) {
var files = fs.readdirSync(dir);
files = files.map(function(fp) {
return path.join(dir, fp);
});
if (util.isOptions(filter)) {
return files;
}
if (typeof filter === 'function') {
return filter(files);
}
if (utils.typeOf(filter) === 'regexp') {
return files.filter(function(fp) {
return filter.test(fp);
});
}
if (utils.isGlob(filter)) {
const isMatch = utils.mm.matcher(filter);
const result = [];
for(let i = 0; i < files.length; i++) {
if (isMatch(files[i])) {
result.push(files[i]);
}
}
return result;
}
if (['isFile', 'isDirectory'].indexOf(filter) !== -1) {
return files.filter(function(fp) {
var stat = fs.statSync(fp);
return stat[filter]();
});
}
return files;
};