Skip to content

Commit 6688585

Browse files
committedJun 5, 2015
support using a function to adjust data properties for methods that deal with multiple entries such as bulk and directory.
1 parent 4d25ea6 commit 6688585

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed
 

‎README.md

+12
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,16 @@ The `data` property can be set (per src-dest mapping) to define data for matched
6262
```js
6363
archive.bulk([
6464
{ src: ['mydir/**'], data: { date: new Date() } },
65+
{ src: ['mydir/**'], data: function(data) {
66+
data.date = new Date();
67+
return data;
68+
}},
6569
{ expand: true, cwd: 'mydir', src: ['**'], dest: 'newdir' }
6670
]);
6771
```
6872

73+
As of v0.15, the `data` property can also be a function that receives data for each matched entry and is expected to return it after making any desired adjustments.
74+
6975
For more detail on this feature, please see [BULK.md](https://github.com/archiverjs/node-archiver/blob/master/BULK.md).
7076

7177
#### directory(dirpath[, destpath, data])
@@ -81,8 +87,14 @@ archive.directory('mydir', 'abc');
8187

8288
// mydir/ -> archive.ext/
8389
archive.directory('mydir', false, { date: new Date() });
90+
archive.directory('mydir', false, function(data) {
91+
data.date = new Date();
92+
return data;
93+
});
8494
```
8595

96+
As of v0.15, the `data` property can also be a function that receives data for each entry and is expected to return it after making any desired adjustments.
97+
8698
#### file(filepath, data)
8799

88100
Appends a file given its filepath using a [lazystream](https://github.com/jpommerening/node-lazystream) wrapper to prevent issues with open file limits. When the instance has received, processed, and emitted the file, the `entry` event is fired.

‎lib/core.js

+27-6
Original file line numberDiff line numberDiff line change
@@ -378,17 +378,28 @@ Archiver.prototype.bulk = function(mappings) {
378378

379379
files.forEach(function(file){
380380
var isExpandedPair = file.orig.expand || false;
381-
var fileData = file.data || {};
381+
var data = {};
382+
var dataFunction = false;
383+
384+
if (typeof file.data === 'function') {
385+
dataFunction = file.data;
386+
} else if (typeof file.data === 'object') {
387+
data = file.data;
388+
}
382389

383390
file.src.forEach(function(filepath) {
384-
var data = util._.extend({}, fileData);
385-
data.name = isExpandedPair ? util.unixifyPath(file.dest) : util.unixifyPath(file.dest || '', filepath);
391+
var entryData = util._.extend({}, data);
392+
entryData.name = isExpandedPair ? util.unixifyPath(file.dest) : util.unixifyPath(file.dest || '', filepath);
386393

387-
if (data.name === '.') {
394+
if (entryData.name === '.') {
388395
return;
389396
}
390397

391-
self._append(filepath, data);
398+
try {
399+
entryData = dataFunction(entryData) || entryData;
400+
} catch(e) {}
401+
402+
self._append(filepath, entryData);
392403
});
393404
});
394405

@@ -414,7 +425,13 @@ Archiver.prototype.directory = function(dirpath, destpath, data) {
414425
destpath = dirpath;
415426
}
416427

417-
if (typeof data !== 'object') {
428+
var dataFunction = false;
429+
if (typeof data === 'function') {
430+
dataFunction = data;
431+
data = {};
432+
} else if (typeof data === 'object') {
433+
data = data;
434+
} else {
418435
data = {};
419436
}
420437

@@ -429,6 +446,10 @@ Archiver.prototype.directory = function(dirpath, destpath, data) {
429446
entryData.name = util.sanitizePath(destpath, file.relative);
430447
entryData.stats = file.stats;
431448

449+
try {
450+
entryData = dataFunction(entryData) || entryData;
451+
} catch(e) {}
Has conversations. Original line has conversations.
452+
432453
self._append(file.path, entryData);
433454
});
434455
}

‎test/archiver.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ describe('archiver', function() {
145145

146146
archive
147147
.directory('test/fixtures/directory', null, { date: testDate })
148-
.directory('test/fixtures/directory', 'directory')
148+
.directory('test/fixtures/directory', 'directory', function(data) {
149+
data.funcProp = true;
150+
return data;
151+
})
149152
.finalize();
150153
});
151154

@@ -166,6 +169,10 @@ describe('archiver', function() {
166169
assert.property(entries, 'directory/subdir/subsub/');
167170
assert.property(entries, 'directory/subdir/subsub/level2.txt');
168171
});
172+
173+
it('should support setting data properties via function', function() {
174+
assert.propertyVal(entries['directory/level0.txt'], 'funcProp', true);
175+
});
169176
});
170177

171178
describe('#file', function() {
@@ -245,27 +252,41 @@ describe('archiver', function() {
245252

246253
archive
247254
.bulk([
248-
{ expand: true, cwd: 'test/fixtures/directory/', src: '**', data: { prop: 'value' } }
255+
{ expand: true, cwd: 'test/fixtures/directory/', src: '**', data: { prop: 'value' } },
256+
{ expand: true, cwd: 'test/fixtures/directory/', src: '**', dest: 'directory/', data: function(data) {
257+
data.funcProp = true;
258+
return data;
259+
}},
249260
])
250261
.finalize();
251262
});
252263

253264
it('should append multiple entries', function() {
254265
assert.isArray(actual);
255-
assert.lengthOf(actual, 5);
256266

257267
assert.property(entries, 'level0.txt');
258268
assert.property(entries, 'subdir/');
259269
assert.property(entries, 'subdir/level1.txt');
260270
assert.property(entries, 'subdir/subsub/');
261271
assert.property(entries, 'subdir/subsub/level2.txt');
272+
273+
assert.property(entries, 'directory/level0.txt');
274+
assert.property(entries, 'directory/subdir/');
275+
assert.property(entries, 'directory/subdir/level1.txt');
276+
assert.property(entries, 'directory/subdir/subsub/');
277+
assert.property(entries, 'directory/subdir/subsub/level2.txt');
262278
});
263279

264-
it('should support passing data properties', function() {
280+
it('should support setting data properties', function() {
265281
assert.property(entries, 'level0.txt');
266282
assert.propertyVal(entries['level0.txt'], 'prop', 'value');
267283
});
268284

285+
it('should support setting data properties via function', function() {
286+
assert.property(entries, 'directory/level0.txt');
287+
assert.propertyVal(entries['directory/level0.txt'], 'funcProp', true);
288+
});
289+
269290
it('should retain directory permissions', function() {
270291
assert.propertyVal(entries['subdir/'], 'mode', win32 ? 438 : 493);
271292
});

0 commit comments

Comments
 (0)
Please sign in to comment.