if (process.type === 'browser') { require('source-map-support').install(); }var log = require('common/utils/logger').debugLogger(__filename); var logError = require('common/utils/logger').errorLogger(__filename, false); var logFatal = require('common/utils/logger').errorLogger(__filename, true);'use strict';

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

var _stringify = require('babel-runtime/core-js/json/stringify');

var _stringify2 = _interopRequireDefault(_stringify);

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

/**
 * @return the css of the theme
 */
var getThemeCss = function () {
  var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(theme) {
    return _regenerator2.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return _fsExtraPromise2.default.readFileAsync(_filePaths2.default.getThemePath(theme), 'utf-8');

          case 2:
            return _context.abrupt('return', _context.sent);

          case 3:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function getThemeCss(_x) {
    return _ref.apply(this, arguments);
  };
}();

/**
 * @return the css of the file
 */


var getStyleCss = function () {
  var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(style) {
    return _regenerator2.default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return _fsExtraPromise2.default.readFileAsync(_filePaths2.default.getStylePath(style), 'utf-8');

          case 2:
            return _context2.abrupt('return', _context2.sent);

          case 3:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, this);
  }));

  return function getStyleCss(_x2) {
    return _ref2.apply(this, arguments);
  };
}();

/**
 * @return the list of Hunspell dictionaries available in the given dir
 */


/**
 * Verify it's not a directory and the app can access it.
 * If it's invalid, purge it and write it again.
 * If it already exists, it's left untouched.
 */
var replaceFile = function () {
  var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(filePath, writePromise) {
    var stats;
    return _regenerator2.default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            _context3.prev = 0;
            _context3.next = 3;
            return _fsExtraPromise2.default.accessAsync(filePath, _fsExtraPromise2.default.R_OK | _fsExtraPromise2.default.W_OK);

          case 3:
            _context3.next = 5;
            return _fsExtraPromise2.default.lstatAsync(filePath);

          case 5:
            stats = _context3.sent;

            if (stats.isFile()) {
              _context3.next = 8;
              break;
            }

            throw new Error();

          case 8:
            _context3.next = 21;
            break;

          case 10:
            _context3.prev = 10;
            _context3.t0 = _context3['catch'](0);
            _context3.prev = 12;
            _context3.next = 15;
            return _fsExtraPromise2.default.removeAsync(filePath);

          case 15:
            _context3.next = 19;
            break;

          case 17:
            _context3.prev = 17;
            _context3.t1 = _context3['catch'](12);

          case 19:
            _context3.next = 21;
            return writePromise();

          case 21:
          case 'end':
            return _context3.stop();
        }
      }
    }, _callee3, this, [[0, 10], [12, 17]]);
  }));

  return function replaceFile(_x3, _x4) {
    return _ref3.apply(this, arguments);
  };
}();

/**
 * Check if the path exists, can be accessed and is a file.
 */


var isFileExists = function () {
  var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(filePath) {
    var stats;
    return _regenerator2.default.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            _context4.prev = 0;
            _context4.next = 3;
            return _fsExtraPromise2.default.accessAsync(filePath, _fsExtraPromise2.default.R_OK | _fsExtraPromise2.default.W_OK);

          case 3:
            _context4.next = 5;
            return _fsExtraPromise2.default.lstatAsync(filePath);

          case 5:
            stats = _context4.sent;
            return _context4.abrupt('return', stats.isFile());

          case 9:
            _context4.prev = 9;
            _context4.t0 = _context4['catch'](0);
            return _context4.abrupt('return', false);

          case 12:
          case 'end':
            return _context4.stop();
        }
      }
    }, _callee4, this, [[0, 9]]);
  }));

  return function isFileExists(_x5) {
    return _ref4.apply(this, arguments);
  };
}();

var _fsExtraPromise = require('fs-extra-promise');

var _fsExtraPromise2 = _interopRequireDefault(_fsExtraPromise);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _filePaths = require('common/utils/file-paths');

var _filePaths2 = _interopRequireDefault(_filePaths);

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

function getDictionariesSync(dirPath) {
  if (!_fsExtraPromise2.default.statSyncNoException(dirPath)) {
    log('dictionaries path does not exist', dirPath);
    return [];
  }

  var dictionaries = _fsExtraPromise2.default.readdirSync(dirPath).filter(function (filename) {
    return _path2.default.extname(filename) === '.dic';
  }).filter(function (filename) {
    return _fsExtraPromise2.default.statSync(_path2.default.join(dirPath, filename)).isFile();
  }).map(function (filename) {
    return _path2.default.basename(filename, '.dic');
  });

  log('dictionaries in', dirPath, 'found:', (0, _stringify2.default)(dictionaries));
  return dictionaries;
}

/**
 * @return the list of Hunspell dictionaries available in all the given dirs
 */
function getAllDictionariesSync(dirPaths) {
  return dirPaths.reduce(function (acc, dirPath) {
    return acc.concat(getDictionariesSync(dirPath));
  }, []);
}exports.default = {
  getThemeCss: getThemeCss,
  getStyleCss: getStyleCss,
  getDictionariesSync: getDictionariesSync,
  getAllDictionariesSync: getAllDictionariesSync,
  replaceFile: replaceFile,
  isFileExists: isFileExists
};
//# sourceMappingURL=data:application/json;charset=utf8;base64, 
