"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getCurrentFiles = getCurrentFiles;

var _fs = require("fs");

var _fromPairs = _interopRequireDefault(require("lodash/fromPairs"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Returns the disk content for the given file paths if they exist.
 * If a file does not exist, the value for the path will be undefined.
 *
 * @param filePaths The files that shall be retrieved
 */
async function getCurrentFiles(filePaths) {
  const currentFiles = await Promise.all(filePaths.map(async filePath => {
    // Try to retrieve the file contents
    try {
      const content = await _fs.promises.readFile(filePath, "utf-8"); // Return the path and the content as a tuple
      // to match them back together later in an object

      return [filePath, content];
    } catch (error) {
      if (error.code === "ENOENT") {
        // File doesn't exist, return the key with an undefined content
        return [filePath];
      } // A different error occurred, e.g. missing read permissions
      // This error should be thrown back to the user


      throw error;
    }
  })); // Node.js 10 compatibility
  // Use Object.fromEntries once Node 10 is no longer supported

  return (0, _fromPairs.default)(currentFiles);
}