Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update file timestamps to avoid redundant re-syncs (Fixes #22) #23

Merged
merged 7 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module.exports = {
"env": {
"node": true
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2017
},
"extends": "eslint:recommended",
"rules": {
Expand All @@ -22,6 +26,7 @@ module.exports = {
"semi": [
2,
"always"
]
],
"no-var": "warn"
}
};
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ sudo: false

language: node_js
node_js:
danielgindi marked this conversation as resolved.
Show resolved Hide resolved
- "0.10"
- "0.12"
- "4"
- "5"
- "8"
- "10"
- node

cache:
Expand Down
7 changes: 5 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
# Test against these versions of Node.js.
environment:
matrix:
- nodejs_version: "4.2"
- nodejs_version: "5.0"
- nodejs_version: "8"
- nodejs_version: "10"

platform:
- x64

# Install scripts. (runs after repo cloning)
install:
Expand Down
54 changes: 29 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

var walkSync = require('walk-sync');
var FSTree = require('fs-tree-diff');
var mkdirp = require('mkdirp');
var fs = require('fs');
var debug = require('debug')('tree-sync');
const walkSync = require('walk-sync');
const FSTree = require('fs-tree-diff');
const mkdirp = require('mkdirp');
const fs = require('fs');
const debug = require('debug')('tree-sync');

module.exports = TreeSync;

Expand Down Expand Up @@ -33,21 +33,21 @@ TreeSync.prototype.sync = function() {

debug('syncing %s -> %s', this._input, this._output);

var input = FSTree.fromEntries(walkSync.entries(this._input, this._walkSyncOpts));
var output = FSTree.fromEntries(walkSync.entries(this._output, this._walkSyncOpts));
let input = FSTree.fromEntries(walkSync.entries(this._input, this._walkSyncOpts));
let output = FSTree.fromEntries(walkSync.entries(this._output, this._walkSyncOpts));

debug('walked %s %dms and %s %dms', this._input, input.size, this._output, output.size);

var isFirstSync = !this._hasSynced;
var operations = output.calculatePatch(input).filter(function(operation) {
let isFirstSync = !this._hasSynced;
let operations = output.calculatePatch(input).filter(function(operation) {
if (operation[0] === 'change') {
return isFirstSync;
} else {
return true;
}
});

var inputOperations = this._lastInput.calculatePatch(input).filter(function(operation) {
let inputOperations = this._lastInput.calculatePatch(input).filter(function(operation) {
return operation[0] === 'change';
});

Expand All @@ -57,47 +57,51 @@ TreeSync.prototype.sync = function() {

debug('calc operations %d', operations.length);

operations.forEach(function(patch) {
var operation = patch[0];
var pathname = patch[1];
var entry = patch[2];
for (let patch of operations) {
const operation = patch[0];
const pathname = patch[1];
const entry = patch[2];

var inputFullpath = this._input + '/' + pathname;
var outputFullpath = this._output + '/' + pathname;
const inputFullpath = this._input + '/' + pathname;
const outputFullpath = this._output + '/' + pathname;

switch(operation) {
case 'create' :
return fs.writeFileSync(outputFullpath, fs.readFileSync(inputFullpath), { mode: entry.mode });
case 'change' :
return fs.writeFileSync(outputFullpath, fs.readFileSync(inputFullpath), { mode: entry.mode });
fs.writeFileSync(outputFullpath, fs.readFileSync(inputFullpath), { mode: entry.mode });
fs.utimesSync(outputFullpath, new Date(), entry.mtime / 1e3);
break;

case 'mkdir' :
try {
return fs.mkdirSync(outputFullpath);
fs.mkdirSync(outputFullpath);
} catch(e) {
if (e && e.code === 'EEXIST') { /* do nothing */ }
else { throw e; }
}
break;

case 'unlink':
try {
return fs.unlinkSync(outputFullpath);
fs.unlinkSync(outputFullpath);
} catch(e) {
if (e && e.code === 'ENOENT') { /* do nothing */ }
else { throw e; }
}
break;

case 'rmdir':
return fs.rmdirSync(outputFullpath);
fs.rmdirSync(outputFullpath);
break;

default:
throw TypeError('Unknown operation:' + operation + ' on path: ' + pathname);
}
}, this);
}

this._hasSynced = true;
debug('applied patches: %d', operations.length);

// Return only type and name; don't want downstream relying on entries.
return operations.map(function (op) {
return op.slice(0,2);
});
return operations.map(op => op.slice(0,2));
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"description": "",
"main": "index.js",
"types": "index.d.ts",
"engines": {
"node": ">=8"
},
"directories": {
"test": "tests"
},
Expand Down Expand Up @@ -32,6 +35,6 @@
"chai": "^3.4.1",
"glob": "^7.0.0",
"mocha": "^2.3.4",
"mocha-eslint": "^1.0.0"
"mocha-eslint": "^5.0.0"
}
}
9 changes: 8 additions & 1 deletion tests/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
module.exports = {
env: {
mocha: true,
node: true,
es6: true
},
parserOptions: {
ecmaVersion: 2017
},
rules: {
// JSHint "expr", disabled due to chai expect assertions
'no-unused-expressions': 0,

// JSHint "unused"
'no-unused-vars': 0
'no-unused-vars': 0,

'no-var': 'warn'
}
};
Loading