1 |
|
2 | 'use strict';
|
3 |
|
4 | const fs = require('fs');
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function readFile(filepath , options ) {
|
11 | options = options || {};
|
12 | const throwNotFound = options.throwNotFound || false;
|
13 |
|
14 | return new Promise((resolve, reject) => {
|
15 | fs.readFile(filepath, 'utf8', (err, content) => {
|
16 | if (err && err.code === 'ENOENT' && !throwNotFound) {
|
17 | return resolve(null);
|
18 | }
|
19 | if (err) return reject(err);
|
20 | resolve(content);
|
21 | });
|
22 | });
|
23 | }
|
24 |
|
25 | readFile.sync = function readFileSync(
|
26 | filepath ,
|
27 | options
|
28 | ) {
|
29 | options = options || {};
|
30 | const throwNotFound = options.throwNotFound || false;
|
31 |
|
32 | try {
|
33 | return fs.readFileSync(filepath, 'utf8');
|
34 | } catch (err) {
|
35 | if (err.code === 'ENOENT' && !throwNotFound) {
|
36 | return null;
|
37 | }
|
38 | throw err;
|
39 | }
|
40 | };
|
41 |
|
42 | module.exports = readFile;
|