Skip to content

Commit

Permalink
Support instantiation without options
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jun 26, 2016
1 parent d709709 commit bf3903c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.nyc_output
coverage
node_modules
tmp
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ node_js:
- "5"
- "6"
script:
- npm test
- npm test
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.08

- Support instantiation without options

## 0.07

- Fixed an issue where fileExits caused false positivs
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Hot File Cache
========================================
[![npm version](https://badge.fury.io/js/hot-file-cache.svg)](http://badge.fury.io/js/hot-file-cache) [![Dependency Status](https://david-dm.org/jantimon/hot-file-cache.svg)](https://david-dm.org/jantimon/hot-file-cache) [![Build status](https://travis-ci.org/jantimon/hot-file-cache.svg)](https://travis-ci.org/jantimon/hot-file-cache) [![Build status](https://ci.appveyor.com/api/projects/status/u0798wdxt4qho7xq/branch/master?svg=true)](https://ci.appveyor.com/project/jantimon/hot-file-cache/branch/master)
[![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square)](https://github.com/Flet/semistandard)
[![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square)](https://github.com/Flet/semistandard)
[![Coverage Status](https://coveralls.io/repos/github/jantimon/hot-file-cache/badge.svg?branch=master)](https://coveralls.io/github/jantimon/hot-file-cache?branch=master)

A file/glob cache which will invalidate automatically if the source changes (powered by [chokidar](https://github.com/paulmillr/chokidar))

Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ function HotFileCache (patterns, options) {
// Turn string patterns into array
this.patterns = [].concat(patterns);
// Turn functions into array
this.fileProcessor = [].concat(options.fileProcessor || []);
this.fileProcessor = [].concat(this.options.fileProcessor || []);
// Remove fileMap from chokidar options
delete (options.fileProcessor);
delete (this.options.fileProcessor);
// Initialize cache
this.cache = {};
// Watched files
Expand All @@ -27,11 +27,14 @@ function HotFileCache (patterns, options) {
var watcher = chokidar.watch(this.patterns, this.options);
watcher.on('ready', resolve.bind(null, watcher));
watcher.on('error', reject);
// Add files to the internal `getFiles` cache on creation
watcher.on('add', function (file) {
this.watched.push(path.join(this.options.cwd, file));
}.bind(this));
// Remove files from the internal `getFiles` cache on deletion
watcher.on('unlink', function (file) {
var index = this.watched.indexOf(path.join(this.options.cwd, file));
/* istanbul ignore else */
if (index !== -1) {
this.watched.splice(index, 1);
}
Expand Down
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
{
"name": "hot-file-cache",
"version": "0.0.7",
"version": "0.0.8",
"description": "A watched file cache which will invalidate automatically if the source changes",
"main": "index.js",
"scripts": {
"prepublish": "npm test",
"test": "ava",
"posttest": "semistandard index.js",
"test": "npm-run-all test:*",
"test:ava": "ava -v",
"test:nyc": "nyc ava",
"posttest": "npm-run-all posttest:*",
"posttest:lint": "semistandard index.js",
"posttest:coverage-report": "nyc report --reporter=html",
"posttest:coverage": "nyc check-coverage --lines 100 --functions 100 --branches 100",
"puml": "puml generate flow.puml -o flow.png"
},
"files": [
Expand Down Expand Up @@ -39,9 +44,12 @@
},
"devDependencies": {
"ava": "^0.15.2",
"coveralls": "^2.11.9",
"mkdirp": "^0.5.1",
"ncp": "^2.0.0",
"node-plantuml": "^0.4.4",
"npm-run-all": "^2.2.2",
"nyc": "^6.6.1",
"rimraf": "^2.5.2",
"semistandard": "^8.0.0"
}
Expand Down
16 changes: 15 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ async function createTestEnvironment() {
const targetDir = path.resolve(tmp, 'test-' + testDirId++);
await mkdirp(targetDir);
await copy(fixtures, targetDir);
await sleep(fileEventDelay);
return targetDir;
}

test('uses current cwd if no cwd is specified', async t => {
const cache = new HotFileCache('*.md');
t.is(cache.options.cwd, process.cwd());
t.pass();
});

test('reads file from disk', async t => {
const dir = await createTestEnvironment();
const cache = new HotFileCache('*.md', {cwd: dir});
Expand All @@ -33,6 +38,15 @@ test('reads file from disk', async t => {
t.pass();
});

test('reads file from disk with atomic', async t => {
const dir = await createTestEnvironment();
const cache = new HotFileCache('*.md', {cwd: dir, atomic: true});
const expectedContent = await readFile(path.join(fixtures, 'file1.md'));
const content = await cache.readFile(path.join(dir, 'file1.md'));
t.is(content.toString(), expectedContent.toString());
t.pass();
});

test('reads file from cache', async t => {
const dir = await createTestEnvironment();
const cache = new HotFileCache('*.md', {cwd: dir});
Expand Down

0 comments on commit bf3903c

Please sign in to comment.