vfile utility to read and write to the file system.
This utility puts the file system first.
Where vfile
itself focusses on file values (the file contents), this instead
focuses on the file system, which is a common case when working with actual
files from Node.js.
Use this if you know there’s a file system and want to use it.
Use vfile
if there might not be a file system.
This package is ESM only. In Node.js (version 16+), install with npm:
npm install to-vfile
In Deno with esm.sh
:
import {toVFile, read, readSync, write, writeSync} from 'https://esm.sh/to-vfile@8'
import {toVFile, read} from 'to-vfile'
console.log(toVFile('readme.md'))
console.log(toVFile(new URL('readme.md', import.meta.url)))
console.log(await read('.git/HEAD'))
console.log(await read('.git/HEAD', 'utf8'))
Yields:
VFile {
cwd: '/Users/tilde/Projects/oss/to-vfile',
data: {},
history: [ 'readme.md' ],
messages: []
}
VFile {
cwd: '/Users/tilde/Projects/oss/to-vfile',
data: {},
history: [ '/Users/tilde/Projects/oss/to-vfile/readme.md' ],
messages: []
}
VFile {
cwd: '/Users/tilde/Projects/oss/to-vfile',
data: {},
history: [ '.git/HEAD' ],
messages: [],
value: <Buffer 72 65 66 3a 20 72 65 66 73 2f 68 65 61 64 73 2f 6d 61 69 6e 0a>
}
VFile {
cwd: '/Users/tilde/Projects/oss/to-vfile',
data: {},
history: [ '.git/HEAD' ],
messages: [],
value: 'ref: refs/heads/main\n'
}
This package exports the identifiers read
,
readSync
, toVFile
,
write
, and writeSync
.
There is no default export.
Create a virtual file from a description.
This is like VFile
, but it accepts a file path instead of file cotnents.
If options
is a string, URL, or buffer, it’s used as the path.
Otherwise, if it’s a file, that’s returned instead.
Otherwise, the options are passed through to new VFile()
.
description
(Compatible
, optional) — path to file, file options, or file itself
Given file or new file (VFile
).
Create a virtual file and read it in, async.
(description[, options], Callback): undefined
(description[, options]): Promise<VFile>
description
(Compatible
) — path to file, file options, or file itselfoptions
(BufferEncoding
,ReadOptions
, optional)callback
(Callback
, optional) — callback called when done
Nothing when a callback is given, otherwise promise that resolves to given
file or new file (VFile
).
Create a virtual file and read it in, synchronously.
description
(Compatible
) — path to file, file options, or file itselfoptions
(BufferEncoding
,ReadOptions
, optional)
Given file or new file (VFile
).
Create a virtual file and write it, async.
(description[, options], Callback): undefined
(description[, options]): Promise<VFile>
description
(Compatible
) — path to file, file options, or file itselfoptions
(BufferEncoding
,WriteOptions
, optional)callback
(Callback
, optional) — callback called when done
Nothing when a callback is given, otherwise promise that resolves to given
file or new file (VFile
).
Create a virtual file and write it, synchronously.
description
(Compatible
) — path to file, file options, or file itselfoptions
(BufferEncoding
,WriteOptions
, optional)
Given file or new file (VFile
).
Encodings supported by the buffer class (TypeScript type).
This is a copy of the types from Node.
type BufferEncoding =
| 'ascii'
| 'base64'
| 'base64url'
| 'binary'
| 'hex'
| 'latin1'
| 'ucs-2'
| 'ucs2'
| 'utf-8'
| 'utf16le'
| 'utf8'
Callback called after reading or writing a file (TypeScript type).
error
(Error
, optional) — error when reading or writing was not successfulfile
(VFile
, optional) — file when reading or writing was successful
Nothing (undefined
).
URL to file, path to file, options for file, or actual file (TypeScript type).
type Compatible = Uint8Array | URL | VFile | VFileOptions | string
See VFileOptions
and VFile
.
Configuration for fs.readFile
(TypeScript type).
encoding
(BufferEncoding
, optional) — encoding to read file as, will turnfile.value
into astring
if passedflag
(string
, optional) — file system flags to use
Configuration for fs.writeFile
(TypeScript type).
encoding
(BufferEncoding
, optional) — encoding to write file as whenfile.value
is astring
mode
(number | string
, optional) — file mode (permission and sticky bits) if the file was newly createdflag
(string
, optional) — file system flags to use
This package is fully typed with TypeScript.
It exports the additional types
BufferEncoding
,
Callback
,
Compatible
,
ReadOptions
, and
WriteOptions
.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, vfile@^8
,
compatible with Node.js 16.
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.