all files / lib/ file.js

99.07% Statements 107/108
83.33% Branches 25/30
100% Functions 53/53
100% Lines 90/90
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518    33×                         68×                   68×   68× 68×         29×         10×                                                   10× 10×                                               11×                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               68×          
'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); Eif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _bluebird = require('bluebird');
 
var _bluebird2 = _interopRequireDefault(_bluebird);
 
var _path = require('path');
 
var _path2 = _interopRequireDefault(_path);
 
var _minimatch = require('minimatch');
 
var _minimatch2 = _interopRequireDefault(_minimatch);
 
var _fs = require('./fs');
 
var _fs2 = _interopRequireDefault(_fs);
 
var _lock = require('./lock');
 
var _lock2 = _interopRequireDefault(_lock);
 
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);
  };
}
 
/**
 * @class
 */
 
var File = function () {
  function File(pathname) {
    _classCallCheck(this, File);
 
    this._dir = process.cwd();
    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: '_isHiddenFile',
    value: function _isHiddenFile() {
      return (/^\./.test(_path2.default.basename(this._pathname))
      );
    }
  }, {
    key: '_isHiddenDirectory',
    value: function _isHiddenDirectory() {
      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: '_checkAsyncStats',
    value: function _checkAsyncStats(type) {
      return this._getStats().then(function (stats) {
        return stats[type]();
      });
    }
 
    /**
     * Synchronously determine if pathname is a directory
     *
     * @instance
     * @memberOf File
     * @method
     * isDirectorySync
     * @return boolean
     * @example
     * import File from 'file-js';
     *
     * const file = File.create('myDirectory');
     * if (file.isDirectorySync()) {
     *    console.log('processing directory');
     * }
     */
 
  }, {
    key: 'isDirectorySync',
    value: function isDirectorySync() {
      return this._getStatsSync().isDirectory();
    }
 
    /**
     * Synchronously determine if pathname is a file
     *
     * @instance
     * @memberOf File
     * @method
     * isFileSync
     * @return boolean
     * @example
     * import File from 'file-js';
     *
     * const file = File.create('myDirectory');
     * if (file.isFileSync()) {
     *    console.log('processing file');
     * }
     */
 
  }, {
    key: 'isFileSync',
    value: function isFileSync() {
      return this._getStatsSync().isFile();
    }
 
    /**
     * Determine if pathname is a directory
     *
     * @instance
     * @memberOf File
     * @method
     * isDirectory
     * @return If the Promise fulfils, the fulfilment value is
     * a boolean indicating if the pathname is a directory
     * @example
     * import File from 'file-js';
     *
     * const file = File.create('myDirectory');
     * file.isDirectory((isDirectory) => {
     *   console.log(isDirectory);
     * });
     *
     */
 
  }, {
    key: 'isDirectory',
    value: function isDirectory() {
      return this._checkAsyncStats('isDirectory');
    }
 
    /**
     * Determine if pathname is a file
     *
     * @instance
     * @memberOf File
     * @method
     * isDirectory
     * @return If the Promise fulfils, the fulfilment value is
     * a boolean indicating if the pathname is a file
     * @example
     * import File from 'file-js';
     *
     * const file = File.create('myDirectory');
     * file.isFile((isFile) => {
     *   console.log(isFile);
     * });
     */
 
  }, {
    key: 'isFile',
    value: function isFile() {
      return this._checkAsyncStats('isFile');
    }
 
    /**
     * Synchronously determine if pathname is a hidden file
     *
     * @instance
     * @memberOf File
     * @method
     * isHiddenSync
     * @return boolean
     * @example
     * import File from 'file-js';
     *
     * const file = File.create('./myHiddenFile');
     * if (file.isHiddenSync()) {
     *    console.log('processing hidden file');
     * }
     */
 
  }, {
    key: 'isHiddenSync',
    value: function isHiddenSync() {
      if (!this.isDirectorySync()) {
        return this._isHiddenFile();
      }
      return this._isHiddenDirectory();
    }
 
    /**
     * Determine if pathname is a file
     *
     * @instance
     * @memberOf File
     * @method
     * isDirectory
     * @return If the Promise fulfils, the fulfilment value is
     * a boolean indicating if the pathname is a file
     * @example
     * import File from 'file-js';
     *
     * const file = File.create('myDirectory');
     * file.isFile((isFile) => {
     *   console.log(isFile);
     * });
     */
 
  }, {
    key: 'isHidden',
    value: function isHidden() {
      var _this = this;
 
      this.isDirectory().then(function (isDirectory) {
        if (!isDirectory) {
          return _this._isHiddenFile();
        }
        return _this._isHiddenDirectory();
      });
    }
 
    /**
     *  Renames the abstract pathname
     *
     * @instance
     * @memberOf File
     * @param {string|File} pathname - pathname either as a string or File instance
     * @method
     * rename
     * @return If the Promise fulfils, the fulfilment value is undefined
     * @example
     * import File from 'file-js';
     *
     * const original = File.create('fileA');
     * const renameTo = File.create('fileB');
     * file
     *  .rename(renameTo)
     *  .then(() => {
     *     console.log(file.getName()) // prints fileA
     *  });
     */
 
  }, {
    key: 'rename',
    value: function rename(pathname) {
      var _this2 = this;
 
      var newname = pathname instanceof File ? pathname.getName() : pathname;
 
      return fsp.renameAsync(this._pathname, newname).then(function () {
        _this2._pathname = newname;
      });
    }
 
    /**
     * Synchronously get list of files, if pathname is a directory
     *
     * @instance
     * @memberOf File
     * @method
     * getListSync
     * @return array of files
     * @example
     * import File from 'file-js';
     *
     * const file = File.create('./myHiddenFile');
     * const files = file.getListSync();
     * console.log(files);
     */
 
  }, {
    key: 'getListSync',
    value: function getListSync() {
      var _this3 = this;
 
      if (this.isDirectorySync()) {
        return _fs2.default.readdirSync(this._pathname).map(function (file) {
          return _path2.default.join(_this3._pathname, file);
        });
      }
      return null;
    }
 
    /**
     * Get list of file objects, if pathname is a directory
     *
     * @instance
     * @memberOf File
     * @method
     * getList
     * @return a promise. If the Promise fulfils, the fulfilment value is
     * a list of pathnames
     * @example
     * import File from 'file-js';
     *
     * const file = File.create('./myDirectory');
     * file.getList()
     * .then((files) => {
     *    console.log(files);
     * });
     */
 
  }, {
    key: 'getList',
    value: function getList() {
      var _this4 = this;
 
      return this.isDirectory().then(function (isDirectory) {
        if (isDirectory) {
          return fsp.readdirAsync(_this4._pathname).map(joinWith(_this4._pathname));
        }
        return null;
      });
    }
 
    /**
     * Get list of file objects, if pathname is a directory
     *
     * @instance
     * @memberOf File
     * @method
     * getFiles
     * @return a promise. If the Promise fulfils, the fulfilment value is
     * a list of File objects
     * @example
     * import File from 'file-js';
     *
     * const file = File.create('./myDirectory');
     * file.getFiles()
     * .then((files) => {
     *    console.log(files.map(file => file.isFileSync()));
     * });
     */
 
  }, {
    key: 'getFiles',
    value: function getFiles() {
      return this.getList().then(function (list) {
        if (!list) return _bluebird2.default.resolve(null);
 
        return list.map(function (pathname) {
          return File.create(pathname);
        });
      });
    }
 
    /**
     * Synchronously caculate the depth of a directory
     *
     * @instance
     * @memberOf File
     * @method
     * getDepthSync
     * @return boolean
     * @example
     * import File from 'file-js';
     *
     * const file = File.create('myDirectory');
     * console.log(file.getDepthSync());
     */
 
  }, {
    key: 'getDepthSync',
    value: function getDepthSync() {
      if (!this.isDirectorySync()) {
        return this._depth(_path2.default.dirname(this._pathname));
      }
      return this._depth(this._pathname);
    }
 
    /**
     * Returns the pathname as a string
     *
     * @instance
     * @memberOf File
     * @method
     * getName
     * @return String
     * @example
     * import File from 'file-js';
     *
     * const file = File.create('myDirectory');
     * console.log(file.getName());
     */
 
  }, {
    key: 'getName',
    value: function getName() {
      return this._pathname;
    }
  }, {
    key: 'getAbsolutePath',
    value: function getAbsolutePath() {
      if (_path2.default.isAbsolute(this._pathname)) {
        return this._pathname;
      }
      return [this._dir, this._pathname].join(_path2.default.sep);
    }
  }, {
    key: 'getCanonicalPath',
    value: function getCanonicalPath() {
      return _path2.default.normalize(this.getAbsolutePath());
    }
  }, {
    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;
    }
  }, {
    key: 'isWritable',
    value: function isWritable() {
      return this._access(_fs2.default.W_OK);
    }
  }, {
    key: 'isReadable',
    value: function isReadable() {
      return this._access(_fs2.default.R_OK);
    }
  }, {
    key: 'isExecutable',
    value: function isExecutable() {
      return this._access(_fs2.default.X_OK);
    }
  }, {
    key: 'delete',
    value: function _delete() {
      return fsp.unlinkAsync(this._pathname);
    }
  }, {
    key: 'withLock',
    value: function withLock(fn) {
      var _this5 = this;
 
      return _lock2.default.lockAsync(this._pathname).then(function () {
        return fn();
      }).finally(function () {
        _lock2.default.unlockAsync(_this5._pathname);
      });
    }
 
    /**
     * Static factory method to create an instance of File
     *
     * @static
     * @memberOf File
     * @method
     * create
     * @return File instance
     * @example
     * import File from 'file-js';
     *
     * const file = File.create();
     */
 
  }], [{
    key: 'create',
    value: function create(filename) {
      return new File(filename);
    }
  }]);
 
  return File;
}();
 
module.exports.create = File.create;