Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. The goal is to mimic the command line.
  2.  
  3. For create helper methods, pass in a list of files to add to the
  4. tarball.
  5.  
  6. If a file is specified, then write out to the file. Otherwise, return
  7. the stream, which can be piped wherever.
  8.  
  9. If no cb is provided, return a Promise. Actually, just return a Promise
  10. from creation helper methods no matter what, maybe?
  11.  
  12. So, to replicate `tar czf my-tarball.tgz files and folders`, you'd do:
  13.  
  14. tar.c(
  15. {
  16. gzip: <true|gzip options>,
  17. file: 'my-tarball.tgz',
  18. glob: <true|glob options>
  19. },
  20. ['some', 'files', 'and', 'folders']
  21. ).then(_ => { .. tarball has been created .. })
  22.  
  23. To replicate `tar cz files and folders > my-tarball.tgz`, you'd do:
  24.  
  25. tar.c(
  26. {
  27. gzip: <true|gzip options>,
  28. glob: <true|glob options>
  29. },
  30. ['some', 'files', 'and', 'folders']
  31. ).pipe(fs.createWriteStream('my-tarball.tgz')
  32.  
  33. To replicate `tar xf my-tarball.tgz` you'd do:
  34.  
  35. tar.x(
  36. {
  37. file: 'my-tarball.tgz'
  38. }
  39. ).then(_=> { .. tarball has been dumped in cwd .. })
  40.  
  41. To replicate `cat my-tarball.tgz | tar x -C some-dir --strip=1`:
  42.  
  43. fs.createReadStream('my-tarball.tgz').pipe(
  44. tar.x({
  45. strip: 1,
  46. C: 'some-dir' // alias for cwd:'some-dir', also ok
  47. })
  48. )
  49.  
  50. To replicate `tar tf my-tarball.tgz`, do this:
  51.  
  52. tar.t({ file: 'my-tarball.tgz' })
  53. .on('entry', entry => { .. do whatever with it .. })
  54.  
  55. To replicate `cat my-tarball.tgz | tar t` do:
  56.  
  57. fs.createReadStream('my-tarball.tgz')
  58. .pipe(tar.t())
  59. .on('entry', entry => { .. do whatever with it .. })
  60.  
  61. The arguments list to `tar t` and `tar x` specify a list of filenames
  62. to extract or list, so they're equivalent to:
  63.  
  64. {
  65. filter: (path, entry) => new Set(itemList).has(path)
  66. }
  67.  
  68. To do anything synchronous, add `sync: true` to the options.
  69.  
  70. To filter entries, add `filter: <function>` to the options.
  71. Tar-creating methods call the filter with `filter(path, stat)`.
  72. Tar-reading methods (including extraction) call the filter with
  73. `filter(path, entry)`. The filter is called in the `this`-context of
  74. the stream object.
  75.  
  76. For those who _aren't_ fans of tar's single-character command names:
  77.  
  78. ```
  79. tar.c === tar.create
  80. tar.r === tar.replace (appends to archive, file is required)
  81. tar.u === tar.update (appends if newer, file is required)
  82. tar.x === tar.extract
  83. tar.t === tar.list
  84. ```
  85.  
  86. Future enhancement idea: if `@file` is passed as an entry argument,
  87. and `@file` is a tarball, then read the entries out of it, and add
  88. them as if they were files. (This allows for filtering items out of a
  89. tarball, into a new tarball, for example.)
  90.  
  91. options todo:
  92.  
  93. `C` or `cwd`: read entries out of or extract them into the specified
  94. dir
  95.  
  96. `strip` or `stripComponents` like `--strip` on the tar cli
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement