all files / lib/ file.js

92.59% Statements 75/81
79.17% Branches 19/24
91.67% Functions 33/36
93.75% Lines 60/64
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187    20×                     279×     62× 189×       279×   279×         448×         67×         14×                     134×                               348×         67× 67×           17× 13×                                       63×   63× 63× 62×             137×   133×         224×                 22×     22×         20×         20×         20×         40×           279×  
'use strict';
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (EprotoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _bluebird = require('bluebird');
 
var _bluebird2 = _interopRequireDefault(_bluebird);
 
var _fs = require('fs');
 
var _fs2 = _interopRequireDefault(_fs);
 
var _path = require('path');
 
var _path2 = _interopRequireDefault(_path);
 
var _minimatch = require('minimatch');
 
var _minimatch2 = _interopRequireDefault(_minimatch);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { if (I!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
var fsp = _bluebird2.default.promisifyAll(_fs2.default);
 
function joinWith(dir) {
  return function (file) {
    return _path2.default.join(dir, file);
  };
}
 
var File = function () {
  function File(pathname) {
    _classCallCheck(this, File);
 
    this._pathname = pathname;
  }
 
  _createClass(File, [{
    key: '_getStatsSync',
    value: function _getStatsSync() {
      return _fs2.default.statSync(this._pathname);
    }
  }, {
    key: '_getStats',
    value: function _getStats() {
      return fsp.statAsync(this._pathname);
    }
  }, {
    key: '_isFile',
    value: function _isFile() {
      return (/^\./.test(_path2.default.basename(this._pathname))
      );
    }
  }, {
    key: '_isDirectory',
    value: function _isDirectory() {
      return (/(^|\/)\.[^\/\.]/g.test(this._pathname)
      );
    }
  }, {
    key: '_depth',
    value: function _depth(pathname) {
      return pathname.split(_path2.default.sep).length - 1;
    }
  }, {
    key: '_access',
    value: function _access(permission) {
      var hasPermission = true;
 
      return fsp.accessAsync(this._pathname, permission).catch(function () {
        return hasPermission = false;
      }).then(function () {
        return hasPermission;
      });
    }
  }, {
    key: 'isDirectorySync',
    value: function isDirectorySync() {
      return this._getStatsSync().isDirectory();
    }
  }, {
    key: 'isDirectory',
    value: function isDirectory() {
      return this._getStats().then(function (stats) {
        return stats.isDirectory();
      });
    }
  }, {
    key: 'isHiddenSync',
    value: function isHiddenSync() {
      if (!this.isDirectorySync()) {
        return this._isFile();
      }
      return this._isDirectory();
    }
  }, {
    key: 'isHidden',
    value: function isHidden() {
      var _this = this;
 
      this.isDirectory().then(function (isDirectory) {
        if (!isDirectory) {
          return _this._isFile();
        }
        return _this._isDirectory();
      });
    }
  }, {
    key: 'getFilesSync',
    value: function getFilesSync() {
      var _this2 = this;
 
      if (this.isDirectorySync()) {
        return _fs2.default.readdirSync(this._pathname).map(function (file) {
          return _path2.default.join(_this2._pathname, file);
        });
      }
      return null;
    }
  }, {
    key: 'getFiles',
    value: function getFiles() {
      var _this3 = this;
 
      return this.isDirectory().then(function (isDirectory) {
        if (isDirectory) {
          return fsp.readdirAsync(_this3._pathname).map(joinWith(_this3._pathname));
        }
        return null;
      });
    }
  }, {
    key: 'getDepthSync',
    value: function getDepthSync() {
      if (!this.isDirectorySync()) {
        return this._depth(_path2.default.dirname(this._pathname));
      }
      return this._depth(this._pathname);
    }
  }, {
    key: 'getName',
    value: function getName() {
      return this._pathname;
    }
  }, {
    key: 'getPathExtension',
    value: function getPathExtension() {
      return _path2.default.extname(this._pathname).substring(1);
    }
  }, {
    key: 'isMatch',
    value: function isMatch(globPattern) {
      var glob = new _minimatch2.default.Minimatch(globPattern, {
        matchBase: true
      });
      return glob.match(this._pathname);
    }
  }, {
    key: 'lastModifiedSync',
    value: function lastModifiedSync() {
      return this._getStatsSync()['mtime'];
    }
  }, {
    key: 'lastAccessedSync',
    value: function lastAccessedSync() {
      return this._getStatsSync()['atime'];
    }
  }, {
    key: 'lastChangedSync',
    value: function lastChangedSync() {
      return this._getStatsSync()['ctime'];
    }
  }, {
    key: 'sizeSync',
    value: function sizeSync() {
      return this._getStatsSync().size;
    }
  }]);
 
  return File;
}();
 
module.exports.create = function (filename) {
  return new File(filename);
};