'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.cleanUpAccount = exports.deleteEachInstance = exports.deleteInstance = exports.createInstance = exports.shutdownLocation = exports.setupLocation = exports.createdSocketName = exports.randomKey = exports.cliLocation = exports.testsLocation = exports.connection = exports.nixt = undefined;

var _mkdirp = require('mkdirp');

var _mkdirp2 = _interopRequireDefault(_mkdirp);

var _fsExtra = require('fs-extra');

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _syncano = require('syncano');

var _syncano2 = _interopRequireDefault(_syncano);

var _nixt = require('nixt');

var _nixt2 = _interopRequireDefault(_nixt);

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _testUtils = require('../utils/test-utils');

var _testUtils2 = _interopRequireDefault(_testUtils);

var _printTools = require('../utils/print-tools');

var _uniqueInstance = require('../utils/unique-instance');

var _uniqueInstance2 = _interopRequireDefault(_uniqueInstance);

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

if (process.env.SYNCANO_E2E_DEBUG) {
  _nixt2.default.prototype.expect = function (fn) {
    function wrappedExpect(result) {
      console.log((0, _printTools.p)(6)('stdout:'));
      console.log((0, _printTools.p)(8)(result.stdout));
      console.log((0, _printTools.p)(6)('code:'));
      console.log((0, _printTools.p)(8)(result.code.toString()));
      return fn(result);
    }
    this.expectations.push(wrappedExpect);
  };
} /* eslint import/no-extraneous-dependencies: "warn" */
const nixt = exports.nixt = _nixt2.default;

process.env.SYNCANO_ACCOUNT_FILE = 'syncano-test';

// Variables used in tests
const { accountKey, syncanoYmlPath, instance } = _testUtils2.default.returnTestGlobals();

const connection = exports.connection = (0, _syncano2.default)({ baseUrl: `https://${process.env.SYNCANO_HOST}`, accountKey });
const testsLocation = exports.testsLocation = `${process.cwd()}/e2e-tests`;
const cliLocation = exports.cliLocation = 'node ../lib/cli.js';
const randomKey = exports.randomKey = _testUtils2.default.getRandomString();
const createdSocketName = exports.createdSocketName = _testUtils2.default.getRandomString();

const setupLocation = exports.setupLocation = name => {
  const location = [testsLocation, name].join('-');
  if (!_fsExtra2.default.existsSync(location)) {
    _mkdirp2.default.sync(location);
  }
  return location;
};

const shutdownLocation = exports.shutdownLocation = location => {
  if (_fsExtra2.default.existsSync(location)) {
    _fsExtra2.default.removeSync(location);
  }
  _fsExtra2.default.removeSync(syncanoYmlPath);
};

// Helper functions used in tests
const createInstance = exports.createInstance = () => connection.Instance.please().create({ name: (0, _uniqueInstance2.default)() }).then(response => response).catch(error => process.stderr.write(JSON.stringify(error.message, null, '')));

const deleteInstance = exports.deleteInstance = item => connection.Instance.please().delete({ name: item }).then(response => response).catch(error => process.stderr.write(JSON.stringify(error.message, null, '')));

const deleteEachInstance = exports.deleteEachInstance = instances => {
  const list = [];

  _lodash2.default.each(instances, item => list.push(deleteInstance(item)));
  return _bluebird2.default.all(list);
};

const cleanUpAccount = exports.cleanUpAccount = () => connection.Instance.please().list().then(res => {
  const instances = _lodash2.default.pull(_lodash2.default.map(res, 'name'), instance);
  return deleteEachInstance(instances);
}).catch(error => process.stderr.write(JSON.stringify(error.message)));
//# sourceMappingURL=utils.js.map