Skip to content

Commit 53b4298

Browse files
authored
Merge pull request #20 from zimmi88/master
[Feat] Bump walk-sync; Add support for walk-sync opts
2 parents f383c2b + cf79b8f commit 53b4298

File tree

5 files changed

+112
-23
lines changed

5 files changed

+112
-23
lines changed

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,54 @@ A module for repeated efficient synchronizing two directories.
66
// input/a/{a.js,b.js}
77
// output/
88

9-
var tree = new TreeSync('input', 'output')
9+
var tree = new TreeSync('input', 'output');
1010
tree.sync();
1111
// output is now contains copies of everything that is in input
1212

13-
fs.unlink('/input/a/b/js');
13+
fs.unlink('/input/a/b.js');
1414

1515
// input / output have diverged
1616

1717
tree.sync();
1818

1919
// difference is calculated and efficient patch to update `output` is created and applied
2020
```
21+
22+
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.
23+
24+
```js
25+
// Assume the following folder structure...
26+
// input/
27+
// foo/
28+
// foo.js
29+
// bar.js
30+
// bar/
31+
// foo-bar.js
32+
33+
var tree = new TreeSync('input', 'output', {
34+
ignore: ['**/b']
35+
});
36+
tree.sync();
37+
38+
// We now expect output to contain foo/, but not bar/.
39+
// Any changes made to bar/ won't be synced to output, and the contents of bar/ won't be traversed.
40+
```
41+
42+
```js
43+
// Assume the following folder structure...
44+
// input/
45+
// foo/
46+
// foo.js
47+
// bar.js
48+
// bar/
49+
// foo-bar.js
50+
51+
var tree = new TreeSync('input', 'output', {
52+
globs: ['foo', 'foo/bar.js']
53+
});
54+
tree.sync();
55+
56+
// We now expect output to contain foo/bar.js, but nothing else.
57+
// Be careful when using this property! You'll need to make sure that if you're including a file, it's parent
58+
// path is also included, or you'll get an error when tree-sync tries to copy the file over.
59+
```

index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,22 @@ var debug = require('debug')('tree-sync');
88

99
module.exports = TreeSync;
1010

11-
function TreeSync(input, output) {
11+
function TreeSync(input, output, options) {
1212
this._input = input;
1313
this._output = output;
14+
this._options = options || {};
15+
this._walkSyncOpts = {};
1416
this._hasSynced = false;
1517
this._lastInput = FSTree.fromEntries([]);
1618

19+
// Pass through whitelisted options to walk-sync.
20+
if (this._options.globs) {
21+
this._walkSyncOpts.globs = options.globs;
22+
}
23+
if (this._options.ignore) {
24+
this._walkSyncOpts.ignore = options.ignore;
25+
}
26+
1727
debug('initializing TreeSync: %s -> %s', this._input, this._output);
1828
}
1929

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

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

26-
var input = FSTree.fromEntries(walkSync.entries(this._input));
27-
var output = FSTree.fromEntries(walkSync.entries(this._output));
36+
var input = FSTree.fromEntries(walkSync.entries(this._input, this._walkSyncOpts));
37+
var output = FSTree.fromEntries(walkSync.entries(this._output, this._walkSyncOpts));
2838

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tree-sync",
3-
"version": "1.2.2",
3+
"version": "1.3.0",
44
"description": "",
55
"main": "index.js",
66
"directories": {
@@ -20,11 +20,11 @@
2020
"author": "Stefan Penner",
2121
"license": "ISC",
2222
"dependencies": {
23-
"fs-tree-diff": "^0.5.6",
2423
"debug": "^2.2.0",
24+
"fs-tree-diff": "^0.5.6",
2525
"mkdirp": "^0.5.1",
2626
"quick-temp": "^0.1.5",
27-
"walk-sync": "^0.2.7"
27+
"walk-sync": "^0.3.3"
2828
},
2929
"devDependencies": {
3030
"chai": "^3.4.1",

tests/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,5 +239,39 @@ describe('TreeSync', function() {
239239
expect(entries.length).to.eql(3);
240240
});
241241
});
242+
243+
describe('validate walk-sync options', function() {
244+
it('should ignore files/folders it is told to ignore', function() {
245+
// Start with an empty dir
246+
expect(walkSync(tmp)).to.deep.equal([]);
247+
248+
// We need our own treeSync instance with options
249+
treeSync = new TreeSync(__dirname + '/fixtures/', tmp, {
250+
ignore: ['**/bar']
251+
});
252+
253+
treeSync.sync();
254+
255+
expect(walkSync(tmp)).to.deep.equal([
256+
'one/',
257+
'one/foo.txt'
258+
]);
259+
});
260+
261+
it('should only include globs it is told to include', function() {
262+
expect(walkSync(tmp)).to.deep.equal([]);
263+
264+
treeSync = new TreeSync(__dirname + '/fixtures/', tmp, {
265+
globs: ['one', 'one/foo.txt']
266+
});
267+
268+
treeSync.sync();
269+
270+
expect(walkSync(tmp)).to.deep.equal([
271+
'one/',
272+
'one/foo.txt'
273+
]);
274+
});
275+
});
242276
});
243277
});

yarn.lock

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ async@~0.2.6:
5858
version "0.2.10"
5959
resolved "https://registry.npmjs.org/async/-/async-0.2.10.tgz#b6bbe0b0674b9d719708ca38de8c237cb526c3d1"
6060

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

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

7272
camelcase@^1.0.2:
@@ -134,7 +134,7 @@ commander@2.3.0:
134134

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

139139
concat-stream@^1.4.6:
140140
version "1.5.2"
@@ -205,7 +205,7 @@ doctrine@^0.7.1:
205205

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

210210
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:
211211
version "0.10.12"
@@ -810,8 +810,8 @@ lru-cache@2:
810810
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-2.7.3.tgz#6d4524e8b955f95d4f5b58851ce21dd72fb4e952"
811811

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

@@ -822,12 +822,18 @@ minimatch@0.3:
822822
lru-cache "2"
823823
sigmund "~1.0.0"
824824

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

831+
minimatch@^3.0.2:
832+
version "3.0.4"
833+
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
834+
dependencies:
835+
brace-expansion "^1.1.7"
836+
831837
minimist@0.0.8, minimist@~0.0.1:
832838
version "0.0.8"
833839
resolved "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
@@ -1134,9 +1140,9 @@ util-deprecate@~1.0.1:
11341140
version "1.0.2"
11351141
resolved "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
11361142

1137-
walk-sync@^0.2.7:
1138-
version "0.2.7"
1139-
resolved "https://registry.npmjs.org/walk-sync/-/walk-sync-0.2.7.tgz#b49be4ee6867657aeb736978b56a29d10fa39969"
1143+
walk-sync@^0.3.3:
1144+
version "0.3.3"
1145+
resolved "https://registry.yarnpkg.com/walk-sync/-/walk-sync-0.3.3.tgz#1e9f12cd4fe6e0e6d4a0715b5cc7e30711d43cd1"
11401146
dependencies:
11411147
ensure-posix-path "^1.0.0"
11421148
matcher-collection "^1.0.0"

0 commit comments

Comments
 (0)