unified

Project:vfile/to-vfile

Package:to-vfile@6.1.0

  1. Dependencies:2·Dependents:66
  2. vfile utility to create a vfile from a filepath
  1. util 105
  2. utility 105
  3. file 24
  4. virtual 21
  5. vfile 17
  6. vfile-util 16
  7. text 13
  8. processing 3
  9. path 3

to-vfile

Build Coverage Downloads Sponsors Backers Chat

Create a vfile from a filepath. Optionally populates them from the file system as well. Can write virtual files to file system too.

Install

npm:

npm install to-vfile

Note: the file-system stuff is not available in the browser.

Use

var vfile = require('to-vfile')

console.log(vfile('readme.md'))
console.log(vfile.readSync('.git/HEAD'))
console.log(vfile.readSync('.git/HEAD', 'utf8'))

Yields:

VFile {
  data: {},
  messages: [],
  history: ['readme.md'],
  cwd: '/Users/tilde/projects/oss/to-vfile'
}
VFile {
  data: {},
  messages: [],
  history: ['.git/HEAD'],
  cwd: '/Users/tilde/projects/oss/to-vfile',
  contents: <Buffer 72 65 66 3a 20 72 65 66 73 2f 68 65 61 64 73 2f 6d 61 73 74 65 72 0a>
}
VFile {
  data: {},
  messages: [],
  history: ['.git/HEAD'],
  cwd: '/Users/tilde/projects/oss/to-vfile',
  contents: 'ref: refs/heads/master\n'
}

API

toVFile(options)

Create a virtual file. Works like the vfile constructor, except when options is string or Buffer, in which case it’s treated as {path: options} instead of {contents: options}.

toVFile.read(options[, encoding][, callback])

Creates a virtual file from options (toVFile(options)), reads the file from the file-system and populates file.contents with the result. If encoding is specified, it’s passed to fs.readFile. If callback is given, invokes it with either an error or the populated virtual file. If callback is not given, returns a Promise that is rejected with an error or resolved with the populated virtual file.

toVFile.readSync(options[, encoding])

Like toVFile.read but synchronous. Either throws an error or returns a populated virtual file.

toVFile.write(options[, fsOptions][, callback])

Creates a virtual file from options (toVFile(options)), writes the file to the file-system. fsOptions are passed to fs.writeFile. If callback is given, invokes it with an error, if any. If callback is not given, returns a Promise that is rejected with an error or resolved with the written virtual file.

toVFile.writeSync(options[, fsOptions])

Like toVFile.write but synchronous. Either throws an error or returns a populated virtual file.

Contribute

See contributing.md in vfile/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer