Giter Site home page Giter Site logo

to-vfile's Introduction

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

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install to-vfile

Use

import {toVFile, readSync} from 'to-vfile'

console.log(toVFile('readme.md'))
console.log(toVFile(new URL('./readme.md', import.meta.url)))
console.log(readSync('.git/HEAD'))
console.log(readSync('.git/HEAD', 'utf8'))

Yields:

VFile {
  data: {},
  messages: [],
  history: ['readme.md'],
  cwd: '/Users/tilde/projects/oss/to-vfile'
}
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',
  value: <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',
  value: 'ref: refs/heads/main\n'
}

API

This package exports the following identifiers: toVFile, read, readSync, write, and writeSync. There is no default export.

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 {value: options}, or when options is a WHATWG URL object, in which case it’s treated as {path: fileURLToPath(options)}.

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

Creates a virtual file from options (toVFile(options)), reads the file from the file system and populates file.value 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.

readSync(options[, encoding])

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

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.

writeSync(options[, fsOptions])

Like 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

to-vfile's People

Contributors

aduh95 avatar danburzo avatar greenkeeperio-bot avatar kachkaev avatar remcohaszing avatar wooorm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.