-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
fs.js
68 lines (61 loc) · 1.46 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
'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)) {
return files.filter(utils.mm.matcher(filter));
}
if (['isFile', 'isDirectory'].indexOf(filter) !== -1) {
return files.filter(function(fp) {
var stat = fs.statSync(fp);
return stat[filter]();
});
}
return files;
};