Git Product home page Git Product logo

vfile-read's Introduction

vfile-read

Read a file or directory into a vfile.

Travis Coveralls github

Read a file or directory into a vfile while keeping the directories structure using vfiles contents key. Vfile-read returns a promise if no callback is given.

install

npm i vfile-read

usage

Given:

./foo
|_ bar
  |_ foo.txt
    |_ "Foo"
var read = require('vfile-read')

read('./foo')
  .then(console.log)
  .catch(console.error)

Outputs:

VFile {
  data: {},
  messages: [],
  history: ['foo'],
  cwd: './',
  contents: [
    VFile {
      data: {},
      messages: [],
      history: ["foo/bar"],
      cwd: "./",
      contents: [
        VFile {
          data: {},
          messages: [],
          history: ["foo/bar/foo.txt"],
          cwd: "./",
          contents: "Foo"
        }
      ]
    }
  ]
}

api

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


location

string - Location to read from.


options?

[ string | array | object ] - If options is a string then options.encoding is set to options. If options is an array then options.ignores is set to options.

options.encoding

string - default = 'utf-8'

options.ignores

array - default = []


callback?

function - If no callback is given, then read returns a promise.


read#sync

Synchronous version of vfile-read

var read = require('vfile-read')

try {
  var file = read.sync('./', {ignores: ['node_modules'])
  ...
} catch (err) {
  ...
}

Vfile-read uses fs.readdir and fs.readFile and options will be passed down to those functions.

related

to-vfile - Create a vfile from a file-path

License

MIT © Paul Zimmer

vfile-read's People

Contributors

dependabot[bot] avatar mrzmmr avatar

Watchers

 avatar  avatar

vfile-read's Issues

updating paths incorrectly

While prepending dirnames, paths are updated incorrectly.

example if path /foo/bar:

read('/foo')
  .then(function (file) {
    console.log(file.contents[0].path) //=> 'foo/bar'
  })

Outputs foo/bar and drops the leading /, but path should be /foo/bar

options.ignores aren't ignored

With path foo/bar calling,

read(process.cwd(), ['bar'])
  .then(function (file) {
    console.log(file.contents[0].path) // => 'foo/bar'
  })

Should be undefined.

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.