Skip to content
This repository has been archived by the owner on Jan 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1 from cgatno/release/v0.1.8
Browse files Browse the repository at this point in the history
Release/v0.1.8
  • Loading branch information
Christian Gaetano committed May 4, 2017
2 parents 37ac5b3 + 88daefc commit b56cfc4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ Thumbs.db
# or `npm run build`
# or `gulp build`
build/

# The test Impostr library
# Use `node test/main.js` to run the test and generate the library
test/impostr.json
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ src/
.eslintrc
.gitignore
gulpfile.babel.js
test/impostr.json
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "impostr",
"version": "0.1.7",
"version": "0.1.8",
"description": "Impostr helps you keep track of who's who and what's what. Simple caching tool for checking for file changes over time.",
"main": "build/impostr.js",
"repository": "https://github.com/cgatno/impostr",
Expand Down
38 changes: 33 additions & 5 deletions src/lib/TrackingCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ export default class TrackingCache {
*/
isTrackingPath(testPath) {
// Force boolean coercion to avoid conditional gotchas
return !!Object.keys(this.library).find(fileKey =>
fileKey === TrackedFile.formatAnyPath(testPath), // always format paths to tracked format
);
return !!this.library[TrackedFile.formatAnyPath(testPath)];
}

/**
Expand All @@ -74,8 +72,8 @@ export default class TrackingCache {
* @param {Function} done A function to be called when all newly added files are tracked.
*/
trackFiles(globPattern, done) {
if (this.debug) console.log(`Tracking files matching pattern ${globPattern}`);
glob(globPattern, (err, files) => {
if (this.debug) console.log(`Tracking files matching pattern ${globPattern}...`);
glob(globPattern, { nodir: true }, (err, files) => {
if (err) {
if (this.debug) console.log('IMPOSTR ERROR: ', JSON.stringify(err));
throw err;
Expand Down Expand Up @@ -139,8 +137,23 @@ export default class TrackingCache {
*/
pruneLibrary() {
const deletedPaths = [];
if (this.debug) console.log('Pruning library...');

// Only show pruning progress if debugging is enabled
let numFiles;
let currentFile;
if (this.debug) {
numFiles = Object.keys(this.library).length;
currentFile = 0;
}

Object.keys(this.library).forEach((filePathKey) => {
if (this.debug) {
currentFile += 1;
console.log(`[${currentFile}/${numFiles}] Checking if ${filePathKey} exists...`);
}
if (!fs.existsSync(filePathKey)) {
console.log(`${filePathKey} doesn't exist. Deleting...`);
delete this.library[filePathKey];
deletedPaths.push(filePathKey);
}
Expand All @@ -158,14 +171,29 @@ export default class TrackingCache {
// First prune the library so we only compare existing files
this.pruneLibrary();

if (this.debug) console.log('Updating library...');

// Only show updating progress if debugging is enabled
let numFiles;
let currentFile;
if (this.debug) {
numFiles = Object.keys(this.library).length;
currentFile = 0;
}

// Generate an array containing paths to updated files
const updatedPaths = Object.keys(this.library).filter((filePath) => {
if (this.debug) {
currentFile += 1;
console.log(`[${currentFile}/${numFiles}] Checking if ${filePath} has changed...`);
}
// Compare the saved hash to the hash of a new file
const currentHash = revHash(fs.readFileSync(filePath));
const savedHash = this.library[filePath];
if (currentHash !== savedHash) {
// If the file has changed, update the saved hash AND add this path to the list of changed
// files
if (this.debug) console.log(`${filePath} has changed! Updating hash...`);
this.library[filePath] = currentHash;
return true;
}
Expand Down
4 changes: 0 additions & 4 deletions test/impostr.json

This file was deleted.

0 comments on commit b56cfc4

Please sign in to comment.