"use strict";

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

var _fs = require("fs");

var _path = _interopRequireDefault(require("path"));

var _jsYaml = _interopRequireDefault(require("js-yaml"));

var _constants = require("../constants");

var _getStrictCoatLockfiles = require("./get-strict-coat-lockfiles");

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

const initialCoatGlobalLockfile = {
  version: _constants.COAT_GLOBAL_LOCKFILE_VERSION
};
const initialCoatLocalLockfile = {
  version: _constants.COAT_LOCAL_LOCKFILE_VERSION
};
/**
 * Retrieves the parsed version of the coat global lockfile
 * that is placed in project-dir/coat.lock
 *
 * If no lockfile exists (yet), an initial empty lockfile is returned
 *
 * @param cwd Working directory of the coat project
 */

async function getCoatGlobalLockfile(cwd) {
  let lockfile = initialCoatGlobalLockfile;

  try {
    const lockfileRaw = await _fs.promises.readFile(_path.default.join(cwd, _constants.COAT_GLOBAL_LOCKFILE_PATH), "utf-8");
    lockfile = _jsYaml.default.safeLoad(lockfileRaw);
  } catch (error) {
    // Throw if error is anything other than "not found"
    if (error.code !== "ENOENT") {
      throw error;
    }
  } // TODO: See #32
  // Lockfile validation


  return (0, _getStrictCoatLockfiles.getStrictCoatGlobalLockfile)(lockfile);
}
/**
 * Retrieves the parsed version of the coat local lockfile
 * that is placed in project-dir/.coat/coat.lock
 *
 * If no lockfile exists (yet), an initial empty lockfile is returned
 *
 * @param cwd Working directory of the coat project
 */


async function getCoatLocalLockfile(cwd) {
  let lockfile = initialCoatLocalLockfile;

  try {
    const lockfileRaw = await _fs.promises.readFile(_path.default.join(cwd, _constants.COAT_LOCAL_LOCKFILE_PATH), "utf-8");
    lockfile = _jsYaml.default.safeLoad(lockfileRaw);
  } catch (error) {
    // Throw if error is anything other than "not found"
    if (error.code !== "ENOENT") {
      throw error;
    }
  } // TODO: See #32
  // Lockfile validation


  return (0, _getStrictCoatLockfiles.getStrictCoatLocalLockfile)(lockfile);
}