"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.getEnvVar = exports.getEnvExportCmd = exports.checkCrypto = void 0;var _path = _interopRequireDefault(require("path"));
var _logger = require("./logger");





var _utils = require("./utils");
var _fileutils = require("./fileutils");




var _constants = require("../constants");
var _taskManager = require("../taskManager");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { "default": obj };}function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {try {var info = gen[key](arg);var value = info.value;} catch (error) {reject(error);return;}if (info.done) {resolve(value);} else {Promise.resolve(value).then(_next, _throw);}}function _asyncToGenerator(fn) {return function () {var self = this,args = arguments;return new Promise(function (resolve, reject) {var gen = fn.apply(self, args);function _next(value) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);}function _throw(err) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);}_next(undefined);});};}

var getEnvExportCmd = function getEnvExportCmd(envVar, key) {
  if (_utils.isSystemWin) {
    return "".concat((0, _logger.chalk)().white("setx ".concat(envVar, " \"").concat(key, "\"")));
  }
  return "".concat((0, _logger.chalk)().white("export ".concat(envVar, "=\"").concat(key, "\"")));
};exports.getEnvExportCmd = getEnvExportCmd;

var getEnvVar = function getEnvVar(c) {
  var p1 = c.paths.workspace.dir.
  split('/').
  pop().
  replace('.', '');
  var p2 = c.files.project["package"].name.
  replace('@', '').
  replace('/', '_').
  replace(/-/g, '_');
  var envVar = "CRYPTO_".concat(p1, "_").concat(p2).toUpperCase();
  (0, _logger.logDebug)('encrypt looking for env var:', envVar);
  return envVar;
};exports.getEnvVar = getEnvVar;


var checkCrypto = /*#__PURE__*/function () {var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(c, parentTask, originTask) {var _c$files$project$conf, _c$files$project$conf2, _c$files$project$conf3, _c$files$project$conf4, _c$files$project$conf5, _c$files$project$conf6, _c$files$project$conf7, _c$files$project$conf8;var sourceRaw, destRaw, source, tsProjectPath, wsPath, tsWorkspacePath, tsWorkspace, tsProject;return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:
            (0, _logger.logTask)('checkCrypto');if (!(

            c.program.ci || (_c$files$project$conf = c.files.project.config) !== null && _c$files$project$conf !== void 0 && (_c$files$project$conf2 = _c$files$project$conf.crypto) !== null && _c$files$project$conf2 !== void 0 && _c$files$project$conf2.optional)) {_context.next = 3;break;}return _context.abrupt("return");case 3:

            sourceRaw = (_c$files$project$conf3 = c.files.project.config) === null || _c$files$project$conf3 === void 0 ? void 0 : (_c$files$project$conf4 = _c$files$project$conf3.crypto) === null || _c$files$project$conf4 === void 0 ? void 0 : (_c$files$project$conf5 = _c$files$project$conf4.decrypt) === null || _c$files$project$conf5 === void 0 ? void 0 : _c$files$project$conf5.source;
            destRaw = (_c$files$project$conf6 = c.files.project.config) === null || _c$files$project$conf6 === void 0 ? void 0 : (_c$files$project$conf7 = _c$files$project$conf6.crypto) === null || _c$files$project$conf7 === void 0 ? void 0 : (_c$files$project$conf8 = _c$files$project$conf7.encrypt) === null || _c$files$project$conf8 === void 0 ? void 0 : _c$files$project$conf8.dest;if (!

            destRaw) {_context.next = 25;break;}if (!(
            sourceRaw && destRaw)) {_context.next = 25;break;}
            source = "".concat((0, _fileutils.getRealPath)(c, sourceRaw, 'decrypt.source'));
            tsProjectPath = "".concat(source, ".timestamp");
            wsPath = _path["default"].join(
            c.paths.workspace.dir,
            c.files.project["package"].name);

            tsWorkspacePath = _path["default"].join(wsPath, 'timestamp');if (
            (0, _fileutils.fsExistsSync)(source)) {_context.next = 15;break;}
            (0, _logger.logWarning)(
            "This project uses encrypted files but you don't have them installed");_context.next = 25;break;case 15:


            tsWorkspace = 0;
            tsProject = 0;
            if ((0, _fileutils.fsExistsSync)(tsWorkspacePath)) {
              tsWorkspace = parseInt(
              (0, _fileutils.fsReadFileSync)(tsWorkspacePath).toString(),
              10);

            }

            if ((0, _fileutils.fsExistsSync)(tsProjectPath)) {
              tsProject = parseInt(
              (0, _fileutils.fsReadFileSync)(tsProjectPath).toString(),
              10);

            }if (!(

            tsProject > tsWorkspace)) {_context.next = 24;break;}
            (0, _logger.logWarning)("Your ".concat(tsWorkspacePath, " is out of date.\nproject timestamp: ").concat(
            (0, _logger.chalk)().grey("".concat(tsProject, " - ").concat(new Date(tsProject))), "\nworkspace timestamp: ").concat(
            (0, _logger.chalk)().grey("".concat(tsWorkspace, " - ").concat(new Date(tsWorkspace))), "\nyou should run decrypt"));_context.next = 23;return (

              (0, _taskManager.executeTask)(c, _constants.TASK_CRYPTO_DECRYPT, parentTask, originTask));case 23:return _context.abrupt("return");case 24:



            if (tsProject < tsWorkspace) {
              (0, _logger.logWarning)("Your ".concat(
              tsWorkspacePath, " is newer than your project one."));

            }case 25:case "end":return _context.stop();}}}, _callee);}));return function checkCrypto(_x, _x2, _x3) {return _ref.apply(this, arguments);};}();exports.checkCrypto = checkCrypto;
//# sourceMappingURL=crypto.js.map