├── index.js ├── package.json ├── readme.md └── test └── index.js /index.js: -------------------------------------------------------------------------------- 1 | const humanInterval = require('human-interval') 2 | const fs = require('fs') 3 | 4 | function pathIsFresh (filename, interval) { 5 | interval = interval || '1 hour' 6 | 7 | if (!fs.existsSync(filename)) return false 8 | 9 | const mtime = +(new Date(fs.statSync(filename).mtime)) 10 | const int = humanInterval(interval) 11 | const now = Date.now() 12 | return mtime + int > now 13 | } 14 | 15 | module.exports = pathIsFresh 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "path-is-fresh", 3 | "version": "1.0.0", 4 | "description": "A human-friendly tool for checking file freshness", 5 | "main": "index.js", 6 | "repository": "https://github.com/zeke/path-is-fresh", 7 | "author": "Zeke Sikelianos ", 8 | "license": "MIT", 9 | "devDependencies": { 10 | "chai": "^4.1.2", 11 | "mocha": "^4.0.1", 12 | "standard": "^10.0.3", 13 | "standard-markdown": "^4.0.2", 14 | "timekeeper": "^2.0.0", 15 | "tmp": "^0.0.33" 16 | }, 17 | "dependencies": { 18 | "human-interval": "^0.1.6" 19 | }, 20 | "scripts": { 21 | "test": "mocha && standard --fix && standard-markdown" 22 | } 23 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # path-is-fresh 2 | 3 | A human-friendly tool for checking file freshness 4 | 5 | ## Installation 6 | 7 | ```sh 8 | npm install path-is-fresh --save 9 | ``` 10 | 11 | ## Usage 12 | 13 | The module exports a single function: 14 | 15 | ```js 16 | const pathIsFresh = require('path-is-fresh') 17 | ``` 18 | 19 | The first argument should be a fully-qualified path to a file or directory: 20 | 21 | ```js 22 | pathIsFresh('/some/file-created-in-the-last-hour.txt') 23 | // true 24 | 25 | pathIsFresh('/some/file-created-ages-ago.txt') 26 | // false 27 | ``` 28 | 29 | To customize the freshness threshold, specify a 30 | [human interval](http://ghub.io/human-interval) 31 | string like `10 seconds` or `1 minute` or `3 days` as the second argument: 32 | 33 | ```js 34 | pathIsFresh('/some/file.txt', '3 weeks') 35 | // true 36 | ``` 37 | 38 | If the given path doesn't exist, the function will return false: 39 | 40 | ```js 41 | pathIsFresh('nonexistent-file') 42 | // false 43 | ``` 44 | 45 | ## Tests 46 | 47 | ```sh 48 | npm install 49 | npm test 50 | ``` 51 | 52 | ## Dependencies 53 | 54 | - [human-interval](https://github.com/rschmukler/human-interval): Human readable time measurements 55 | 56 | ## Dev Dependencies 57 | 58 | - [chai](https://github.com/chaijs/chai): BDD/TDD assertion library for node.js and the browser. Test framework agnostic. 59 | - [mocha](https://github.com/mochajs/mocha): simple, flexible, fun test framework 60 | - [standard](https://github.com/standard/standard): JavaScript Standard Style 61 | - [standard-markdown](): Test your Markdown files for Standard JavaScript Style™ 62 | - [timekeeper](https://github.com/vesln/timekeeper): Easy testing of time-dependent code. 63 | - [tmp](): Temporary file and directory creator 64 | 65 | 66 | ## License 67 | 68 | MIT 69 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | require('chai').should() 2 | const {describe, it} = require('mocha') 3 | const pathIsFresh = require('..') 4 | const {travel} = require('timekeeper') 5 | const humanInterval = require('human-interval') 6 | const tmp = require('tmp') 7 | const file = tmp.fileSync().name 8 | 9 | function travelForward (interval) { 10 | travel(new Date(Date.now() + humanInterval(interval))) 11 | } 12 | 13 | describe('pathIsFresh', () => { 14 | it('returns false for nonexistent files', () => { 15 | pathIsFresh('nonexistent/file').should.eq(false) 16 | }) 17 | 18 | it('defaults freshness threshold to one hour', () => { 19 | pathIsFresh(file).should.eq(true) 20 | 21 | travelForward('59') 22 | pathIsFresh(file).should.eq(true) 23 | 24 | travelForward('61 minutes') 25 | pathIsFresh(file).should.eq(false) 26 | }) 27 | 28 | it('accepts custom interval', () => { 29 | travelForward('5 years') 30 | pathIsFresh(file, '6 years').should.eq(true) 31 | pathIsFresh(file, '4 years').should.eq(false) 32 | }) 33 | }) 34 | --------------------------------------------------------------------------------