readFile.js 1.2 KB

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