tree-sync
Advanced tools
Comparing version 1.4.0 to 2.0.0
164
index.js
'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; | ||
module.exports = class TreeSync { | ||
constructor(input, output, options = {}) { | ||
this._input = input; | ||
this._output = output; | ||
this._options = options; | ||
this._walkSyncOpts = {}; | ||
this._hasSynced = false; | ||
this._lastInput = FSTree.fromEntries([]); | ||
function TreeSync(input, output, options) { | ||
this._input = input; | ||
this._output = output; | ||
this._options = options || {}; | ||
this._walkSyncOpts = {}; | ||
this._hasSynced = false; | ||
this._lastInput = FSTree.fromEntries([]); | ||
// Pass through whitelisted options to walk-sync. | ||
if (this._options.globs) { | ||
this._walkSyncOpts.globs = options.globs; | ||
} | ||
if (this._options.ignore) { | ||
this._walkSyncOpts.ignore = options.ignore; | ||
} | ||
// Pass through whitelisted options to walk-sync. | ||
if (this._options.globs) { | ||
this._walkSyncOpts.globs = options.globs; | ||
debug('initializing TreeSync: %s -> %s', this._input, this._output); | ||
} | ||
if (this._options.ignore) { | ||
this._walkSyncOpts.ignore = options.ignore; | ||
} | ||
debug('initializing TreeSync: %s -> %s', this._input, this._output); | ||
} | ||
sync() { | ||
mkdirp.sync(this._output); | ||
mkdirp.sync(this._input); | ||
TreeSync.prototype.sync = function() { | ||
mkdirp.sync(this._output); | ||
mkdirp.sync(this._input); | ||
debug('syncing %s -> %s', this._input, this._output); | ||
debug('syncing %s -> %s', this._input, this._output); | ||
let input = FSTree.fromEntries(walkSync.entries(this._input, this._walkSyncOpts)); | ||
let output = FSTree.fromEntries(walkSync.entries(this._output, this._walkSyncOpts)); | ||
var input = FSTree.fromEntries(walkSync.entries(this._input, this._walkSyncOpts)); | ||
var output = FSTree.fromEntries(walkSync.entries(this._output, this._walkSyncOpts)); | ||
debug('walked %s %dms and %s %dms', this._input, input.size, this._output, output.size); | ||
debug('walked %s %dms and %s %dms', this._input, input.size, this._output, output.size); | ||
let isFirstSync = !this._hasSynced; | ||
let operations = output.calculatePatch(input).filter(operation => { | ||
if (operation[0] === 'change') { | ||
return isFirstSync; | ||
} else { | ||
return true; | ||
} | ||
}); | ||
var isFirstSync = !this._hasSynced; | ||
var operations = output.calculatePatch(input).filter(function(operation) { | ||
if (operation[0] === 'change') { | ||
return isFirstSync; | ||
} else { | ||
return true; | ||
} | ||
}); | ||
let inputOperations = this._lastInput.calculatePatch(input).filter(operation => { | ||
return operation[0] === 'change'; | ||
}); | ||
var inputOperations = this._lastInput.calculatePatch(input).filter(function(operation) { | ||
return operation[0] === 'change'; | ||
}); | ||
this._lastInput = input; | ||
this._lastInput = input; | ||
operations = operations.concat(inputOperations); | ||
operations = operations.concat(inputOperations); | ||
debug('calc operations %d', operations.length); | ||
debug('calc operations %d', operations.length); | ||
for (let patch of operations) { | ||
const operation = patch[0]; | ||
const pathname = patch[1]; | ||
const entry = patch[2]; | ||
operations.forEach(function(patch) { | ||
var operation = patch[0]; | ||
var pathname = patch[1]; | ||
var entry = patch[2]; | ||
const inputFullpath = this._input + '/' + pathname; | ||
const outputFullpath = this._output + '/' + pathname; | ||
var inputFullpath = this._input + '/' + pathname; | ||
var outputFullpath = this._output + '/' + pathname; | ||
switch(operation) { | ||
case 'create' : | ||
case 'change' : | ||
fs.writeFileSync(outputFullpath, fs.readFileSync(inputFullpath), { mode: entry.mode }); | ||
fs.utimesSync(outputFullpath, new Date(), entry.mtime / 1e3); | ||
break; | ||
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 }); | ||
case 'mkdir' : | ||
try { | ||
return fs.mkdirSync(outputFullpath); | ||
} catch(e) { | ||
if (e && e.code === 'EEXIST') { /* do nothing */ } | ||
else { throw e; } | ||
} | ||
break; | ||
case 'unlink': | ||
try { | ||
return fs.unlinkSync(outputFullpath); | ||
} catch(e) { | ||
if (e && e.code === 'ENOENT') { /* do nothing */ } | ||
else { throw e; } | ||
} | ||
break; | ||
case 'rmdir': | ||
return fs.rmdirSync(outputFullpath); | ||
default: | ||
throw TypeError('Unknown operation:' + operation + ' on path: ' + pathname); | ||
case 'mkdir' : | ||
try { | ||
fs.mkdirSync(outputFullpath); | ||
} catch(e) { | ||
if (e && e.code === 'EEXIST') { /* do nothing */ } | ||
else { throw e; } | ||
} | ||
break; | ||
case 'unlink': | ||
try { | ||
fs.unlinkSync(outputFullpath); | ||
} catch(e) { | ||
if (e && e.code === 'ENOENT') { /* do nothing */ } | ||
else { throw e; } | ||
} | ||
break; | ||
case 'rmdir': | ||
fs.rmdirSync(outputFullpath); | ||
break; | ||
default: | ||
throw TypeError('Unknown operation:' + operation + ' on path: ' + pathname); | ||
} | ||
} | ||
}, this); | ||
this._hasSynced = true; | ||
debug('applied patches: %d', operations.length); | ||
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 only type and name; don't want downstream relying on entries. | ||
return operations.map(op => op.slice(0,2)); | ||
} | ||
}; |
{ | ||
"name": "tree-sync", | ||
"version": "1.4.0", | ||
"version": "2.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"directories": { | ||
@@ -35,4 +38,4 @@ "test": "tests" | ||
"mocha": "^2.3.4", | ||
"mocha-eslint": "^1.0.0" | ||
"mocha-eslint": "^5.0.0" | ||
} | ||
} |
7764
133