index.js
1.29 KB
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
'use strict';
var path = require('path');
var fs = require('graceful-fs');
var stripBom = require('strip-bom');
var stripBomStream = require('strip-bom-stream');
var File = require('vinyl');
var pify = require('pify');
var Promise = require('pinkie-promise');
var fsP = pify(fs, Promise);
exports.read = function (pth, opts) {
opts = opts || {};
var cwd = opts.cwd || process.cwd();
var base = opts.base || cwd;
pth = path.resolve(cwd, pth);
return fsP.stat(pth).then(function (stat) {
var file = new File({
cwd: cwd,
base: base,
path: pth,
stat: stat
});
if (opts.read === false) {
return file;
}
if (opts.buffer === false) {
file.contents = fs.createReadStream(pth).pipe(stripBomStream());
return file;
}
return fsP.readFile(pth).then(function (contents) {
file.contents = stripBom(contents);
return file;
});
});
};
exports.readSync = function (pth, opts) {
opts = opts || {};
var cwd = opts.cwd || process.cwd();
var base = opts.base || cwd;
pth = path.resolve(cwd, pth);
var contents;
if (opts.read !== false) {
contents = opts.buffer === false ?
fs.createReadStream(pth).pipe(stripBomStream()) :
stripBom(fs.readFileSync(pth));
}
return new File({
cwd: cwd,
base: base,
path: pth,
stat: fs.statSync(pth),
contents: contents
});
};