Skip to content

[Feat] Bump walk-sync; Add support for walk-sync opts #20

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

Merged
merged 3 commits into from
Nov 30, 2018
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
43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,54 @@ A module for repeated efficient synchronizing two directories.
// input/a/{a.js,b.js}
// output/

var tree = new TreeSync('input', 'output')
var tree = new TreeSync('input', 'output');
tree.sync();
// output is now contains copies of everything that is in input

fs.unlink('/input/a/b/js');
fs.unlink('/input/a/b.js');

// input / output have diverged

tree.sync();

// difference is calculated and efficient patch to update `output` is created and applied
```

Under the hood, this library uses [walk-sync](https://github.com/joliss/node-walk-sync) to traverse files and folders. You may optionally pass in options to selectively include or ignore files and directories. These whitelisted properties (`ignore` and `globs`) will be passed to walk-sync.

```js
// Assume the following folder structure...
// input/
// foo/
// foo.js
// bar.js
// bar/
// foo-bar.js

var tree = new TreeSync('input', 'output', {
ignore: ['**/b']
});
tree.sync();

// We now expect output to contain foo/, but not bar/.
// Any changes made to bar/ won't be synced to output, and the contents of bar/ won't be traversed.
```

```js
// Assume the following folder structure...
// input/
// foo/
// foo.js
// bar.js
// bar/
// foo-bar.js

var tree = new TreeSync('input', 'output', {
globs: ['foo', 'foo/bar.js']
});
tree.sync();

// We now expect output to contain foo/bar.js, but nothing else.
// Be careful when using this property! You'll need to make sure that if you're including a file, it's parent
// path is also included, or you'll get an error when tree-sync tries to copy the file over.
```
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ var debug = require('debug')('tree-sync');

module.exports = TreeSync;

function TreeSync(input, output) {
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;
}

debug('initializing TreeSync: %s -> %s', this._input, this._output);
}

Expand All @@ -23,8 +33,8 @@ TreeSync.prototype.sync = function() {

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

var input = FSTree.fromEntries(walkSync.entries(this._input));
var output = FSTree.fromEntries(walkSync.entries(this._output));
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);

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tree-sync",
"version": "1.2.2",
"version": "1.3.0",
"description": "",
"main": "index.js",
"directories": {
Expand All @@ -20,11 +20,11 @@
"author": "Stefan Penner",
"license": "ISC",
"dependencies": {
"fs-tree-diff": "^0.5.6",
"debug": "^2.2.0",
"fs-tree-diff": "^0.5.6",
"mkdirp": "^0.5.1",
"quick-temp": "^0.1.5",
"walk-sync": "^0.2.7"
"walk-sync": "^0.3.3"
},
"devDependencies": {
"chai": "^3.4.1",
Expand Down
34 changes: 34 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,39 @@ describe('TreeSync', function() {
expect(entries.length).to.eql(3);
});
});

describe('validate walk-sync options', function() {
it('should ignore files/folders it is told to ignore', function() {
// Start with an empty dir
expect(walkSync(tmp)).to.deep.equal([]);

// We need our own treeSync instance with options
treeSync = new TreeSync(__dirname + '/fixtures/', tmp, {
ignore: ['**/bar']
});

treeSync.sync();

expect(walkSync(tmp)).to.deep.equal([
'one/',
'one/foo.txt'
]);
});

it('should only include globs it is told to include', function() {
expect(walkSync(tmp)).to.deep.equal([]);

treeSync = new TreeSync(__dirname + '/fixtures/', tmp, {
globs: ['one', 'one/foo.txt']
});

treeSync.sync();

expect(walkSync(tmp)).to.deep.equal([
'one/',
'one/foo.txt'
]);
});
});
});
});
36 changes: 21 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ async@~0.2.6:
version "0.2.10"
resolved "https://registry.npmjs.org/async/-/async-0.2.10.tgz#b6bbe0b0674b9d719708ca38de8c237cb526c3d1"

balanced-match@^0.4.1:
version "0.4.2"
resolved "https://registry.npmjs.org/balanced-match/-/balanced-match-0.4.2.tgz#cb3f3e3c732dc0f01ee70b403f302e61d7709838"
balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"

brace-expansion@^1.0.0:
version "1.1.6"
resolved "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.6.tgz#7197d7eaa9b87e648390ea61fc66c84427420df9"
brace-expansion@^1.0.0, brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
dependencies:
balanced-match "^0.4.1"
balanced-match "^1.0.0"
concat-map "0.0.1"

camelcase@^1.0.2:
Expand Down Expand Up @@ -134,7 +134,7 @@ commander@2.3.0:

concat-map@0.0.1:
version "0.0.1"
resolved "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"

concat-stream@^1.4.6:
version "1.5.2"
Expand Down Expand Up @@ -205,7 +205,7 @@ doctrine@^0.7.1:

ensure-posix-path@^1.0.0:
version "1.0.2"
resolved "https://registry.npmjs.org/ensure-posix-path/-/ensure-posix-path-1.0.2.tgz#a65b3e42d0b71cfc585eb774f9943c8d9b91b0c2"
resolved "https://registry.yarnpkg.com/ensure-posix-path/-/ensure-posix-path-1.0.2.tgz#a65b3e42d0b71cfc585eb774f9943c8d9b91b0c2"

es5-ext@^0.10.7, es5-ext@^0.10.8, es5-ext@~0.10.11, es5-ext@~0.10.2, es5-ext@~0.10.7:
version "0.10.12"
Expand Down Expand Up @@ -810,8 +810,8 @@ lru-cache@2:
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-2.7.3.tgz#6d4524e8b955f95d4f5b58851ce21dd72fb4e952"

matcher-collection@^1.0.0:
version "1.0.4"
resolved "https://registry.npmjs.org/matcher-collection/-/matcher-collection-1.0.4.tgz#2f66ae0869996f29e43d0b62c83dd1d43e581755"
version "1.0.5"
resolved "https://registry.yarnpkg.com/matcher-collection/-/matcher-collection-1.0.5.tgz#2ee095438372cb8884f058234138c05c644ec339"
dependencies:
minimatch "^3.0.2"

Expand All @@ -822,12 +822,18 @@ minimatch@0.3:
lru-cache "2"
sigmund "~1.0.0"

"minimatch@2 || 3", minimatch@^3.0.0, minimatch@^3.0.2:
"minimatch@2 || 3", minimatch@^3.0.0:
version "3.0.3"
resolved "https://registry.npmjs.org/minimatch/-/minimatch-3.0.3.tgz#2a4e4090b96b2db06a9d7df01055a62a77c9b774"
dependencies:
brace-expansion "^1.0.0"

minimatch@^3.0.2:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
dependencies:
brace-expansion "^1.1.7"

minimist@0.0.8, minimist@~0.0.1:
version "0.0.8"
resolved "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
Expand Down Expand Up @@ -1134,9 +1140,9 @@ util-deprecate@~1.0.1:
version "1.0.2"
resolved "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"

walk-sync@^0.2.7:
version "0.2.7"
resolved "https://registry.npmjs.org/walk-sync/-/walk-sync-0.2.7.tgz#b49be4ee6867657aeb736978b56a29d10fa39969"
walk-sync@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/walk-sync/-/walk-sync-0.3.3.tgz#1e9f12cd4fe6e0e6d4a0715b5cc7e30711d43cd1"
dependencies:
ensure-posix-path "^1.0.0"
matcher-collection "^1.0.0"
Expand Down