"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _fsExtra = _interopRequireDefault(require("fs-extra"));

var _path = require("path");

function checkErr(err) {
  // NOTE: there is a bug where copySync will throw if the inode
  // from the src and dst are equal. In the case of symlinks, that'll happen.
  // https://github.com/jprichardson/node-fs-extra/issues/657
  if (!err.toString().includes('Source and destination must not be the same')) {
    throw err;
  }
}

function backup(userData, original) {
  var bin = original.split('/').pop();
  var backup = (0, _path.join)(userData, bin + '.bak');

  try {
    _fsExtra["default"].copySync(original, backup);
  } catch (err) {
    checkErr(err);
  }

  console.log("".concat(original, " copied to ").concat(backup));
}

function revert(userData, bin, dst) {
  var backup = (0, _path.join)(userData, bin + '.bak');

  try {
    _fsExtra["default"].removeSync(dst);
  } catch (err) {
    if (err.code !== 'ENOENT') {
      throw err;
    }
  }

  try {
    _fsExtra["default"].copySync(backup, dst);

    console.log("".concat(backup, " restored to ").concat(dst));
  } catch (err) {
    if (err.code === 'ENOENT') {
      console.log("".concat(backup, " not found, skipping"));
      return;
    }

    checkErr(err);
  }

  try {
    _fsExtra["default"].unlinkSync(backup);
  } catch (_) {// Failed to remove the backup. Suprising, but not worth bothering the user about.
  }
}
//# sourceMappingURL=backup.js.map