UNPKG

1.26 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.readFile = readFile;
7exports.readFileSync = readFileSync;
8
9var _fs = _interopRequireDefault(require("fs"));
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13async function fsReadFileAsync(pathname, encoding) {
14 return new Promise((resolve, reject) => {
15 _fs.default.readFile(pathname, encoding, (error, contents) => {
16 if (error) {
17 reject(error);
18 return;
19 }
20
21 resolve(contents);
22 });
23 });
24}
25
26async function readFile(filepath, options = {}) {
27 const throwNotFound = options.throwNotFound === true;
28
29 try {
30 const content = await fsReadFileAsync(filepath, 'utf8');
31 return content;
32 } catch (error) {
33 if (throwNotFound === false && error.code === 'ENOENT') {
34 return null;
35 }
36
37 throw error;
38 }
39}
40
41function readFileSync(filepath, options = {}) {
42 const throwNotFound = options.throwNotFound === true;
43
44 try {
45 const content = _fs.default.readFileSync(filepath, 'utf8');
46
47 return content;
48 } catch (error) {
49 if (throwNotFound === false && error.code === 'ENOENT') {
50 return null;
51 }
52
53 throw error;
54 }
55}
56//# sourceMappingURL=readFile.js.map
\No newline at end of file