'use strict';

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

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

var _fsExtra2 = _interopRequireDefault(_fsExtra);

var _mkdirp = require('mkdirp');

var _mkdirp2 = _interopRequireDefault(_mkdirp);

var _child_process = require('child_process');

var _child_process2 = _interopRequireDefault(_child_process);

var _jsYaml = require('js-yaml');

var _jsYaml2 = _interopRequireDefault(_jsYaml);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

var _debug = require('../debug');

var _debug2 = _interopRequireDefault(_debug);

var _session = require('../session');

var _session2 = _interopRequireDefault(_session);

var _Constants = require('../../constants/Constants');

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

const { debug } = (0, _debug2.default)('utils-sockets-utils');

const socketTemplates = () => {
  const installedTemplates = _session2.default.settings.project.getSocketTemplates();
  const installedTemplatesList = Object.keys(installedTemplates).map(templateName => {
    const temp = { name: templateName, description: '' };
    return temp;
  });
  return _Constants.builtInTemplates.concat(installedTemplatesList);
};

const getTemplatesChoices = () => socketTemplates().map(socketTemplate => `${socketTemplate.name} - ${socketTemplate.description}`);

const findLocalPath = socketName => {
  debug('findLocalPath');
  let socketPath = null;
  const projectPath = _session2.default.projectPath || process.cwd();

  if (!_fsExtra2.default.existsSync(projectPath)) {
    return socketPath;
  }

  const socketInCurrentPath = _path2.default.join(projectPath, 'socket.yml');
  if (_fsExtra2.default.existsSync(socketInCurrentPath)) {
    const socket = _jsYaml2.default.load(_fsExtra2.default.readFileSync(socketInCurrentPath, 'utf8')) || {};
    if (socket.name === socketName) {
      return _path2.default.dirname(socketInCurrentPath);
    }
  }

  _fsExtra2.default.walkSync(projectPath).forEach(file => {
    if (file.match(new RegExp('socket.yml'))) {
      const socket = _jsYaml2.default.load(_fsExtra2.default.readFileSync(file, 'utf8')) || {};
      if (socket.name === socketName) {
        socketPath = _path2.default.dirname(file);
      }
    }
  });
  return socketPath;
};

// Listing sockets
// list sockets based on configuration files
const listLocal = () => {
  debug('listLocal');
  const sockets = [];

  _fsExtra2.default.walkSync(_session2.default.projectPath).forEach(file => {
    if (file.match(new RegExp('socket.yml'))) {
      const socket = _jsYaml2.default.load(_fsExtra2.default.readFileSync(file, 'utf8')) || {};
      sockets.push(socket.name);
    }
  });

  return sockets;
};

const getOrigFilePath = origFileLine => {
  let origFilePath = origFileLine.source.match(/webpack:\/\/\/(.*\.js)(\?|$)/)[1];

  if (origFilePath.match(/~\//)) {
    origFilePath = origFilePath.replace('~', 'node_modules');
  }
  return origFilePath;
};

const updateSocketNPMDeps = folder => {
  debug('updateSocketDeps');
  return new _bluebird2.default((resolve, reject) => {
    const prodFolder = _path2.default.join(folder, '.dist');
    debug('prodFolder', prodFolder);
    if (!_fsExtra2.default.existsSync(prodFolder)) {
      _mkdirp2.default.sync(prodFolder);
    }

    _fsExtra2.default.copySync(_path2.default.join(folder, 'package.json'), _path2.default.join(prodFolder, 'package.json'));

    _child_process2.default.exec('yarn --production', { cwd: prodFolder }, (yarnErr, stdout, stderr) => {
      // TODO: work a little bit more with results
      if (yarnErr) {
        return reject(yarnErr);
      }
      return resolve({ stdout, stderr });
    });
  });
};

exports.default = {
  getTemplatesChoices,
  findLocalPath,
  listLocal,
  getOrigFilePath,
  updateSocketNPMDeps
};
module.exports = exports['default'];
//# sourceMappingURL=utils.js.map