UNPKG

fs-finder-updated

Version:
164 lines (138 loc) 4.76 kB
// Generated by CoffeeScript 2.7.0 (function() { var Base, Finder, Helpers, compare, isFunction, moment; Base = require('./Base'); Helpers = require('./Helpers'); moment = require('moment'); compare = require('operator-compare'); isFunction = function(obj) { return Object.prototype.toString.call(obj) === '[object Function]'; }; Finder = (function() { class Finder extends Base { //******************************************************************************************************************* // CREATING INSTANCE //******************************************************************************************************************* static in(path) { return new Finder(path); } static from(path) { return (new Finder(path)).recursively(); } static find(path, fn = null, type = 'all') { path = Helpers.parseDirectory(path); return (new Finder(path.directory)).recursively().find(path.mask, fn, type); } static findFiles(path = null, fn = null) { if (isFunction(path)) { fn = path; path = null; } return Finder.find(path, fn, 'files'); } static findDirectories(path = null, fn = null) { if (isFunction(path)) { fn = path; path = null; } return Finder.find(path, fn, 'directories'); } static findFile(path = null, fn = null) { if (isFunction(path)) { fn = path; path = null; } return Finder.findFirst().find(path, fn, 'files'); } static findDirectory(path = null, fn = null) { if (isFunction(path)) { fn = path; path = null; } return Finder.findFirst().find(path, fn, 'directories'); } //******************************************************************************************************************* // FIND METHODS //******************************************************************************************************************* find(mask = null, fn = null, type = 'all') { var ref; if (isFunction(mask)) { type = fn; fn = mask; mask = null; } mask = Helpers.normalizePattern(mask); if (this.up === true || ((ref = typeof this.up) === 'number' || ref === 'string')) { if (fn === null) { return this.getPathsFromParentsSync(mask, type); } else { return this.getPathsFromParentsAsync(fn, mask, type); } } else { if (fn === null) { return this.getPathsSync(type, mask); } else { return this.getPathsAsync(fn, type, mask); } } } findFiles(mask = null, fn = null) { if (isFunction(mask)) { fn = mask; mask = null; } return this.find(mask, fn, 'files'); } findDirectories(mask = null, fn = null) { if (isFunction(mask)) { fn = mask; mask = null; } return this.find(mask, fn, 'directories'); } findFile(mask = null, fn = null) { if (isFunction(mask)) { fn = mask; mask = null; } return this.findFirst().find(mask, fn, 'files'); } findDirectory(mask = null, fn = null) { if (isFunction(mask)) { fn = mask; mask = null; } return this.findFirst().find(mask, fn, 'directories'); } //******************************************************************************************************************* // FILTERS //******************************************************************************************************************* size(operation, value) { this.filter(function(stat) { return compare(stat.size, operation, value); }); return this; } date(operation, value) { this.filter(function(stat) { var date; switch (Object.prototype.toString.call(value)) { case '[object String]': date = moment(value, Finder.TIME_FORMAT); break; case '[object Object]': date = moment().subtract(value); break; default: throw new Error('Date format is not valid.'); } return compare((new Date(stat.mtime)).getTime(), operation, date.valueOf()); }); return this; } }; Finder.TIME_FORMAT = 'YYYY-MM-DD HH:mm'; return Finder; }).call(this); module.exports = Finder; }).call(this);