Skip to content

Commit 02f883a

Browse files
committed
docs on how to use onWriteEntry
1 parent 206fcf9 commit 02f883a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,54 @@ advanced use cases, such as re-using caches between runs.
351351
- `maxReadSize` The maximum buffer size for `fs.read()` operations.
352352
Defaults to 16 MB.
353353

354+
#### Using `onWriteMethod` to alter entries
355+
356+
The `onWriteMethod` function, if provided, will get a reference
357+
to each `entry` object on its way into the archive.
358+
359+
If any fields on this entry are changed, then these changes will
360+
be reflected in the entry that is written to the archive.
361+
362+
The return value of the method is ignored. All that matters is
363+
the final state of the entry object. This can also be used to
364+
track the files added to an archive, for example.
365+
366+
For example:
367+
368+
```js
369+
import * as tar from 'tar'
370+
const filesAdded = []
371+
tar.c({
372+
sync: true,
373+
file: 'lowercase-executable.tar',
374+
onWriteEntry(entry) {
375+
// initially, it's uppercase and 0o644
376+
console.log('adding', entry.path, entry.stat.mode.toString(8))
377+
// make all the paths lowercase
378+
entry.path = entry.path.toLowerCase()
379+
// make the entry executable
380+
entry.stat.mode = 0o755
381+
// in the archive, it's lowercase and 0o755
382+
filesAdded.push([entry.path, entry.stat.mode.toString(8)])
383+
},
384+
}, ['./bin'])
385+
console.log('added', filesAdded)
386+
```
387+
388+
Then, if the `./bin` directory contained `SOME-BIN`, it would
389+
show up in the archive as:
390+
391+
```
392+
$ node create-lowercase-executable.js
393+
adding ./bin/SOME-BIN 644
394+
added [[ './bin/some-bin', '755' ]]
395+
396+
$ tar cvf lowercase-executable.tar
397+
-rwxr-xr-x 0 isaacs 20 47731 Aug 14 08:56 ./bin/some-bin
398+
```
399+
400+
with a lowercase name and a mode of `0o755`.
401+
354402
### tar.x(options, fileList, callback) [alias: tar.extract]
355403

356404
Extract a tarball archive.

0 commit comments

Comments
 (0)