Skip to content

Commit

Permalink
Merge pull request #57 from VincentRoth/master
Browse files Browse the repository at this point in the history
Add option noCache with test and doc
  • Loading branch information
contra committed Sep 28, 2018
2 parents 1edb135 + d43a5e2 commit da18b0f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -111,6 +111,12 @@ be the same by default, but specifying `duplicates: true` would yield:
}
```

`noCache`: Prevent file caching. Could be useful using gulp.watch or other watch requiring refreshed file content Default is false.

```js
requireDir('./dir', { noCache: true })
```

## Tips

Make an `index.js` in a directory with this code to clean things up:
Expand Down
5 changes: 5 additions & 0 deletions index.js
Expand Up @@ -113,6 +113,11 @@ module.exports = function requireDir(dir, opts) {
continue;
}

// delete cache
if (opts.noCache) {
delete require.cache[abs];
}

// if duplicates are wanted, key off the full name always, and
// also the base if it hasn't been taken yet (since this ext
// has higher priority than any that follow it). if duplicates
Expand Down
59 changes: 59 additions & 0 deletions test/noCache.js
@@ -0,0 +1,59 @@
var assert = require("assert");
var requireDir = require("..");
var fs = require("fs");

var cachedResult = {
a: "a",
b: "b"
};

var notCachedResult = {
a: "c",
b: "b"
};

// filter the results to a particular file:
assert.deepEqual(requireDir("./noCache", { noCache: false }), cachedResult);

var promiseFileModification = new Promise(function(resolve, reject) {
fs.writeFile("test/noCache/a.js", "module.exports = 'c';", "ascii", function(
error
) {
if (error) {
reject(error);
} else {
resolve();
}
});
});

promiseFileModification.then(
function() {
// Check if cache is active that it is the same result
assert.deepEqual(requireDir("./noCache", { noCache: false }), cachedResult);

// Check by removing cache that the result is the new content
assert.deepEqual(
requireDir("./noCache", { noCache: true }),
notCachedResult
);

console.log("noCache tests passed.");

fs.writeFile(
"test/noCache/a.js",
"module.exports = 'a';",
"ascii",
function(error) {
if (error) {
console.error("noCache tests, issue to reset test.");
console.error(error);
}
}
);
},
function(error) {
console.error("noCache tests failed.");
console.error(error);
}
);
1 change: 1 addition & 0 deletions test/noCache/a.js
@@ -0,0 +1 @@
module.exports = 'a';
1 change: 1 addition & 0 deletions test/noCache/b.json
@@ -0,0 +1 @@
"b"

0 comments on commit da18b0f

Please sign in to comment.