'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.create = create;
exports.remove = remove;
exports.deleteAll = deleteAll;

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _ = require('../');

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

function create(name) {
  var lockFile = _path2.default.join(_.config.root, 'abe-process-' + name + '.lock');
  try {
    var stats = _fs2.default.statSync(lockFile);
    if (stats.isFile()) {
      console.log('cannot lock process ' + name + ' already running');
      return false;
    }
  } catch (err) {
    _fs2.default.writeFileSync(lockFile, '', { encoding: 'utf8' });
  }

  return true;
}

function remove(name) {
  var lockFile = _path2.default.join(_.config.root, 'abe-process-' + name + '.lock');
  try {
    var stats = _fs2.default.statSync(lockFile);
    if (stats.isFile()) {
      _fs2.default.unlinkSync(lockFile);
      return true;
    }
  } catch (err) {}
  return false;
}

function deleteAll() {
  var files = _fs2.default.readdirSync(_.config.root);
  Array.prototype.forEach.call(files, function (file) {
    if (file.indexOf('abe-process') > -1 && file.indexOf('.lock') > -1) {
      _fs2.default.unlinkSync(_path2.default.join(_.config.root, file));
    }
  });
}