Read and write directories of files with pull-stream

Usage no npm install needed!

<script type="module">
  import nichothPullFiles from 'https://cdn.skypack.dev/@nichoth/pull-files';



I don't know what I'm doing. This fork fixes something in ssb for me

Read and write directories of files with pull-stream

var pull = require('pull-stream')
var { read, write } = require('pull-files')

  // Read js files out of `node_modules`

  // Compile files' contents
  pull.through(file => {
    file.data = compile(file.data)

  // Write them to `out` directory
  write('out', err => {

The file objects are a minimal take on Vinyl containing only properties that are necessary:

{ base: '/home/jamen/jamen/pull-files/test',
  path: 'bar/pluto.txt',
  data: <Buffer 68 65 6c 6c 6f 20 69 20 61 6d 20 70 6c 75 74 6f 0a> }

This lets you create them without any dependencies, and you may also add custom properties not concerned with this module


npm install --save pull-files


read(glob, options?)

Read files from a glob or path (or arrays of either) using micromatch patterns. Supply cwd if your paths are relative and will change depending on where you execute node (most likely want __dirname)

Options can contain:

  • cwd: Used to resolve relative paths (commonly set as __dirname)
  • stream: Enable stream mode, where file.data is a source stream
  // Read js files from node_modules, excluding pull-files directory
  read([ 'node_modules/**/*.js', '!node_modules/pull-files' ], { cwd: __dirname }),
  drain(file => console.log(file))

write(dest, done?)

Write files to dest and calls done(err?) when finished

    { path: 'earth.js', data: 'hello earth' },
    { path: 'mars.js', data: 'hello mars' },
    { path: 'pluto.js', data: 'hello pluto' },

  write('example', err => {
    // wrote all 3 files to `example/...`

Here you can see that files don't have to be created from read either, but can be from anywhere. Nor do you have to provide base for unglobbed files.

{ base, path, data }

These Represent files, where:

  • base is an optional property present if path is relative. It allows you to retain directory structure and move the base (e.g. to an out/ folder if you're compiling)
  • path: The path of the data. Either absolute or relative. If absolute, base will be null.
  • data: A buffer or stream of the file's data.

For a simple way to get a file's full path, regardless of relativity, do:

var full = base ? join(base, path) : path

Maintained by Jamen Marz (See on Twitter and GitHub for questions & updates)