Comparing version 7.2.3 to 7.2.4
@@ -0,7 +1,7 @@ | ||
export type BufferEncoding = import('./lib/index.js').BufferEncoding | ||
export type Callback = import('./lib/index.js').Callback | ||
export type Compatible = import('./lib/index.js').Compatible | ||
export type Callback = import('./lib/index.js').Callback | ||
export type BufferEncoding = import('./lib/index.js').BufferEncoding | ||
export type Mode = import('./lib/index.js').Mode | ||
export type ReadOptions = import('./lib/index.js').ReadOptions | ||
export type WriteOptions = import('./lib/index.js').WriteOptions | ||
export type Mode = number | string | ||
export {toVFile, read, readSync, write, writeSync} from './lib/index.js' |
10
index.js
/** | ||
* @typedef {import('./lib/index.js').BufferEncoding} BufferEncoding | ||
* @typedef {import('./lib/index.js').Callback} Callback | ||
* @typedef {import('./lib/index.js').Compatible} Compatible | ||
* @typedef {import('./lib/index.js').Callback} Callback | ||
* @typedef {import('./lib/index.js').BufferEncoding} BufferEncoding | ||
* @typedef {import('./lib/index.js').Mode} Mode | ||
* @typedef {import('./lib/index.js').ReadOptions} ReadOptions | ||
@@ -10,2 +9,7 @@ * @typedef {import('./lib/index.js').WriteOptions} WriteOptions | ||
// To do: next major: don’t expose `Mode`. | ||
/** | ||
* @typedef {number | string} Mode | ||
*/ | ||
export {toVFile, read, readSync, write, writeSync} from './lib/index.js' |
@@ -1,12 +0,17 @@ | ||
/// <reference types="node" /> | ||
/// <reference types="node" resolution-mode="require"/> | ||
/** | ||
* Create a virtual file from a description. | ||
* If `options` is a string or a buffer, it’s used as the path. | ||
* If it’s a VFile itself, it’s returned instead. | ||
* In all other cases, the options are passed through to `vfile()`. | ||
* | ||
* @param {Compatible} [options] | ||
* 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()`. | ||
* | ||
* @param {Compatible | null | undefined} [description] | ||
* Path to file, file options, or file itself. | ||
* @returns {VFile} | ||
* Given file or new file. | ||
*/ | ||
export function toVFile(options?: Compatible): VFile | ||
export function toVFile(description?: Compatible | null | undefined): VFile | ||
export namespace toVFile { | ||
@@ -22,20 +27,29 @@ export {readSync} | ||
* @param {Compatible} description | ||
* @param {ReadOptions} [options] | ||
* Path to file, file options, or file itself. | ||
* @param {BufferEncoding | ReadOptions | null | undefined} [options] | ||
* Encoding to use or Node.JS read options. | ||
* @returns {VFile} | ||
* Given file or new file. | ||
*/ | ||
export function readSync(description: Compatible, options?: ReadOptions): VFile | ||
export function readSync( | ||
description: Compatible, | ||
options?: BufferEncoding | ReadOptions | null | undefined | ||
): VFile | ||
/** | ||
* Create a virtual file and write it in, synchronously. | ||
* Create a virtual file and write it, synchronously. | ||
* | ||
* @param {Compatible} description | ||
* @param {WriteOptions} [options] | ||
* Path to file, file options, or file itself. | ||
* @param {BufferEncoding | WriteOptions | null | undefined} [options] | ||
* Encoding to use or Node.JS write options. | ||
* @returns {VFile} | ||
* Given file or new file. | ||
*/ | ||
export function writeSync( | ||
description: Compatible, | ||
options?: WriteOptions | ||
options?: BufferEncoding | WriteOptions | null | undefined | ||
): VFile | ||
export function read( | ||
description: Compatible, | ||
options: ReadOptions, | ||
options: BufferEncoding | ReadOptions | null | undefined, | ||
callback: Callback | ||
@@ -46,7 +60,7 @@ ): void | ||
description: Compatible, | ||
options?: ReadOptions | ||
options?: BufferEncoding | ReadOptions | null | undefined | ||
): Promise<VFile> | ||
export function write( | ||
description: Compatible, | ||
options: WriteOptions, | ||
options: BufferEncoding | WriteOptions | null | undefined, | ||
callback: Callback | ||
@@ -57,36 +71,60 @@ ): void | ||
description: Compatible, | ||
options?: WriteOptions | ||
options?: BufferEncoding | WriteOptions | null | undefined | ||
): Promise<VFile> | ||
export type Value = import('vfile').VFileValue | ||
export type Options = import('vfile').VFileOptions | ||
/** | ||
* Encodings supported by the buffer class. | ||
* | ||
* This is a copy of the types from Node and `VFile`. | ||
*/ | ||
export type BufferEncoding = import('vfile').BufferEncoding | ||
export type Mode = number | string | ||
export type ReadOptions = | ||
| BufferEncoding | ||
| { | ||
encoding?: null | BufferEncoding | ||
flag?: string | ||
} | ||
export type WriteOptions = | ||
| BufferEncoding | ||
| { | ||
encoding?: null | BufferEncoding | ||
mode: Mode | null | ||
flag?: string | ||
} | ||
/** | ||
* Path of the file. | ||
* Note: `Value` is used here because it’s a smarter `Buffer` | ||
* Configuration for `fs.readFile`. | ||
*/ | ||
export type ReadOptions = { | ||
/** | ||
* Encoding to read file as, will turn `file.value` into a string if passed. | ||
*/ | ||
encoding?: BufferEncoding | null | undefined | ||
/** | ||
* File system flags to use. | ||
*/ | ||
flag?: string | undefined | ||
} | ||
/** | ||
* Configuration for `fs.writeFile`. | ||
*/ | ||
export type WriteOptions = { | ||
/** | ||
* Encoding to write file as. | ||
*/ | ||
encoding?: BufferEncoding | null | undefined | ||
/** | ||
* File mode (permission and sticky bits) if the file was newly created. | ||
*/ | ||
mode?: number | string | undefined | ||
/** | ||
* File system flags to use. | ||
*/ | ||
flag?: string | undefined | ||
} | ||
/** | ||
* URL to file or path to file. | ||
* | ||
* > 👉 **Note**: `Value` is used here because it’s a smarter `Buffer` | ||
*/ | ||
export type Path = URL | Value | ||
/** | ||
* Things that can be | ||
* passed to the function. | ||
* URL to file, path to file, options for file, or actual file. | ||
*/ | ||
export type Compatible = Path | Options | VFile | ||
/** | ||
* Callback called after reading or writing a file. | ||
*/ | ||
export type Callback = ( | ||
error: NodeJS.ErrnoException | null, | ||
file: VFile | null | ||
file: VFile | null | undefined | ||
) => any | ||
import {VFile} from 'vfile' | ||
import {URL} from 'url' |
167
lib/index.js
@@ -5,11 +5,28 @@ /** | ||
* @typedef {import('vfile').BufferEncoding} BufferEncoding | ||
* Encodings supported by the buffer class. | ||
* | ||
* @typedef {number|string} Mode | ||
* @typedef {BufferEncoding|{encoding?: null|BufferEncoding, flag?: string}} ReadOptions | ||
* @typedef {BufferEncoding|{encoding?: null|BufferEncoding, mode: Mode?, flag?: string}} WriteOptions | ||
* This is a copy of the types from Node and `VFile`. | ||
* | ||
* @typedef {URL|Value} Path Path of the file. | ||
* Note: `Value` is used here because it’s a smarter `Buffer` | ||
* @typedef {Path|Options|VFile} Compatible Things that can be | ||
* passed to the function. | ||
* @typedef ReadOptions | ||
* Configuration for `fs.readFile`. | ||
* @property {BufferEncoding | null | undefined} [encoding] | ||
* Encoding to read file as, will turn `file.value` into a string if passed. | ||
* @property {string | undefined} [flag] | ||
* File system flags to use. | ||
* | ||
* @typedef WriteOptions | ||
* Configuration for `fs.writeFile`. | ||
* @property {BufferEncoding | null | undefined} [encoding] | ||
* Encoding to write file as. | ||
* @property {number | string | undefined} [mode] | ||
* File mode (permission and sticky bits) if the file was newly created. | ||
* @property {string | undefined} [flag] | ||
* File system flags to use. | ||
* | ||
* @typedef {URL | Value} Path | ||
* URL to file or path to file. | ||
* | ||
* > 👉 **Note**: `Value` is used here because it’s a smarter `Buffer` | ||
* @typedef {Path | Options | VFile} Compatible | ||
* URL to file, path to file, options for file, or actual file. | ||
*/ | ||
@@ -19,4 +36,7 @@ | ||
* @callback Callback | ||
* @param {NodeJS.ErrnoException|null} error | ||
* @param {VFile|null} file | ||
* Callback called after reading or writing a file. | ||
* @param {NodeJS.ErrnoException | null} error | ||
* Error when reading or writing was not successful. | ||
* @param {VFile | null | undefined} file | ||
* File when reading or writing was successful. | ||
*/ | ||
@@ -30,19 +50,31 @@ | ||
// To do: next major: use `node:` prefix. | ||
// To do: next major: use `URL` from global. | ||
// To do: next major: Only pass `undefined`. | ||
/** | ||
* Create a virtual file from a description. | ||
* If `options` is a string or a buffer, it’s used as the path. | ||
* If it’s a VFile itself, it’s returned instead. | ||
* In all other cases, the options are passed through to `vfile()`. | ||
* | ||
* @param {Compatible} [options] | ||
* 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()`. | ||
* | ||
* @param {Compatible | null | undefined} [description] | ||
* Path to file, file options, or file itself. | ||
* @returns {VFile} | ||
* Given file or new file. | ||
*/ | ||
export function toVFile(options) { | ||
if (typeof options === 'string' || options instanceof URL) { | ||
options = {path: options} | ||
} else if (buffer(options)) { | ||
options = {path: String(options)} | ||
export function toVFile(description) { | ||
if (typeof description === 'string' || description instanceof URL) { | ||
description = {path: description} | ||
} else if (buffer(description)) { | ||
description = {path: String(description)} | ||
} | ||
return looksLikeAVFile(options) ? options : new VFile(options) | ||
return looksLikeAVFile(description) | ||
? description | ||
: // To do: remove when `VFile` allows explicit `null`. | ||
new VFile(description || undefined) | ||
} | ||
@@ -54,4 +86,7 @@ | ||
* @param {Compatible} description | ||
* @param {ReadOptions} [options] | ||
* Path to file, file options, or file itself. | ||
* @param {BufferEncoding | ReadOptions | null | undefined} [options] | ||
* Encoding to use or Node.JS read options. | ||
* @returns {VFile} | ||
* Given file or new file. | ||
*/ | ||
@@ -65,7 +100,10 @@ export function readSync(description, options) { | ||
/** | ||
* Create a virtual file and write it in, synchronously. | ||
* Create a virtual file and write it, synchronously. | ||
* | ||
* @param {Compatible} description | ||
* @param {WriteOptions} [options] | ||
* Path to file, file options, or file itself. | ||
* @param {BufferEncoding | WriteOptions | null | undefined} [options] | ||
* Encoding to use or Node.JS write options. | ||
* @returns {VFile} | ||
* Given file or new file. | ||
*/ | ||
@@ -78,8 +116,21 @@ export function writeSync(description, options) { | ||
/** | ||
* Create a virtual file and read it in, async. | ||
* | ||
* @param description | ||
* Path to file, file options, or file itself. | ||
* @param options | ||
* Encoding to use or Node.JS read options. | ||
* @param callback | ||
* Callback called when done. | ||
* @returns | ||
* Nothing when a callback is given, otherwise promise that resolves to given | ||
* file or new file. | ||
*/ | ||
export const read = | ||
/** | ||
* @type {{ | ||
* (description: Compatible, options: ReadOptions, callback: Callback): void | ||
* (description: Compatible, options: BufferEncoding | ReadOptions | null | undefined, callback: Callback): void | ||
* (description: Compatible, callback: Callback): void | ||
* (description: Compatible, options?: ReadOptions): Promise<VFile> | ||
* (description: Compatible, options?: BufferEncoding | ReadOptions | null | undefined): Promise<VFile> | ||
* }} | ||
@@ -89,7 +140,5 @@ */ | ||
/** | ||
* Create a virtual file and read it in, asynchronously. | ||
* | ||
* @param {Compatible} description | ||
* @param {ReadOptions} [options] | ||
* @param {Callback} [callback] | ||
* @param {BufferEncoding | ReadOptions | null | undefined} [options] | ||
* @param {Callback | null | undefined} [callback] | ||
*/ | ||
@@ -114,2 +163,3 @@ function (description, options, callback) { | ||
function resolve(result) { | ||
// @ts-expect-error: `callback` always defined. | ||
callback(null, result) | ||
@@ -119,4 +169,4 @@ } | ||
/** | ||
* @param {(x: VFile) => void} resolve | ||
* @param {(x: Error, y?: VFile) => void} reject | ||
* @param {(error: VFile) => void} resolve | ||
* @param {(error: NodeJS.ErrnoException, file?: VFile | undefined) => void} reject | ||
*/ | ||
@@ -130,3 +180,4 @@ function executor(resolve, reject) { | ||
} catch (error) { | ||
return reject(error) | ||
const exception = /** @type {NodeJS.ErrnoException} */ (error) | ||
return reject(exception) | ||
} | ||
@@ -137,3 +188,3 @@ | ||
/** | ||
* @param {Error} error | ||
* @param {NodeJS.ErrnoException | null} error | ||
* @param {Value} result | ||
@@ -153,8 +204,21 @@ */ | ||
/** | ||
* Create a virtual file and write it, async. | ||
* | ||
* @param description | ||
* Path to file, file options, or file itself. | ||
* @param options | ||
* Encoding to use or Node.JS write options. | ||
* @param callback | ||
* Callback called when done. | ||
* @returns | ||
* Nothing when a callback is given, otherwise promise that resolves to given | ||
* file or new file. | ||
*/ | ||
export const write = | ||
/** | ||
* @type {{ | ||
* (description: Compatible, options: WriteOptions, callback: Callback): void | ||
* (description: Compatible, options: BufferEncoding | WriteOptions | null | undefined, callback: Callback): void | ||
* (description: Compatible, callback: Callback): void | ||
* (description: Compatible, options?: WriteOptions): Promise<VFile> | ||
* (description: Compatible, options?: BufferEncoding | WriteOptions | null | undefined): Promise<VFile> | ||
* }} | ||
@@ -164,7 +228,5 @@ */ | ||
/** | ||
* Create a virtual file and write it in, asynchronously. | ||
* | ||
* @param {Compatible} description | ||
* @param {WriteOptions} [options] | ||
* @param {Callback} [callback] | ||
* @param {BufferEncoding | WriteOptions | null | undefined} [options] | ||
* @param {Callback | null | undefined} [callback] | ||
*/ | ||
@@ -190,2 +252,3 @@ function (description, options, callback) { | ||
function resolve(result) { | ||
// @ts-expect-error: `callback` always defined. | ||
callback(null, result) | ||
@@ -195,4 +258,4 @@ } | ||
/** | ||
* @param {(x: VFile) => void} resolve | ||
* @param {(x: Error, y?: VFile) => void} reject | ||
* @param {(error: VFile) => void} resolve | ||
* @param {(error: NodeJS.ErrnoException, file: VFile | null) => void} reject | ||
*/ | ||
@@ -206,13 +269,14 @@ function executor(resolve, reject) { | ||
} catch (error) { | ||
return reject(error) | ||
const exception = /** @type {NodeJS.ErrnoException} */ (error) | ||
return reject(exception, null) | ||
} | ||
fs.writeFile(fp, file.value || '', options, done) | ||
fs.writeFile(fp, file.value || '', options || null, done) | ||
/** | ||
* @param {Error} error | ||
* @param {NodeJS.ErrnoException | null} error | ||
*/ | ||
function done(error) { | ||
if (error) { | ||
reject(error) | ||
reject(error, null) | ||
} else { | ||
@@ -227,14 +291,19 @@ resolve(file) | ||
/** | ||
* @param {Compatible} value | ||
* Check if something looks like a vfile. | ||
* | ||
* @param {Compatible | null | undefined} value | ||
* Value. | ||
* @returns {value is VFile} | ||
* Whether `value` looks like a `VFile`. | ||
*/ | ||
function looksLikeAVFile(value) { | ||
return ( | ||
return Boolean( | ||
value && | ||
typeof value === 'object' && | ||
'message' in value && | ||
'messages' in value | ||
typeof value === 'object' && | ||
'message' in value && | ||
'messages' in value | ||
) | ||
} | ||
// To do: next major: remove? | ||
toVFile.readSync = readSync | ||
@@ -241,0 +310,0 @@ toVFile.writeSync = writeSync |
{ | ||
"name": "to-vfile", | ||
"version": "7.2.3", | ||
"version": "7.2.4", | ||
"description": "vfile utility to create a vfile from a filepath", | ||
@@ -42,19 +42,17 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.47.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"{lib/**,}*.d.ts\" && tsc && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -61,0 +59,0 @@ }, |
325
readme.md
@@ -10,13 +10,42 @@ # to-vfile | ||
Create a [`vfile`][vfile] from a filepath. | ||
Optionally populates them from the file system as well. | ||
Can write virtual files to file system too. | ||
[vfile][] utility to read and write to the file system. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`toVFile(description)`](#tovfiledescription) | ||
* [`read(description[, options][, callback])`](#readdescription-options-callback) | ||
* [`readSync(description[, options])`](#readsyncdescription-options) | ||
* [`write(description[, options][, callback])`](#writedescription-options-callback) | ||
* [`writeSync(description[, options])`](#writesyncdescription-options) | ||
* [`BufferEncoding`](#bufferencoding) | ||
* [`Callback`](#callback) | ||
* [`Compatible`](#compatible) | ||
* [`ReadOptions`](#readoptions) | ||
* [`WriteOptions`](#writeoptions) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This utility places file paths and 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 files. | ||
## When should I use this? | ||
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. | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -26,11 +55,25 @@ npm install to-vfile | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {toVFile, read, readSync, write, writeSync} from 'https://esm.sh/to-vfile@7' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {toVFile, read, readSync, write, writeSync} from 'https://esm.sh/to-vfile@7?bundle' | ||
</script> | ||
``` | ||
## Use | ||
```js | ||
import {toVFile, readSync} from 'to-vfile' | ||
import {toVFile, read} 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')) | ||
console.log(toVFile(new URL('readme.md', import.meta.url))) | ||
console.log(await read('.git/HEAD')) | ||
console.log(await read('.git/HEAD', 'utf8')) | ||
``` | ||
@@ -44,4 +87,4 @@ | ||
messages: [], | ||
history: ['readme.md'], | ||
cwd: '/Users/tilde/projects/oss/to-vfile' | ||
history: [ 'readme.md' ], | ||
cwd: '/Users/tilde/Projects/oss/to-vfile' | ||
} | ||
@@ -51,4 +94,4 @@ VFile { | ||
messages: [], | ||
history: ['readme.md'], | ||
cwd: '/Users/tilde/projects/oss/to-vfile' | ||
history: [ '/Users/tilde/Projects/oss/to-vfile/readme.md' ], | ||
cwd: '/Users/tilde/Projects/oss/to-vfile' | ||
} | ||
@@ -58,5 +101,5 @@ VFile { | ||
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> | ||
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 69 6e 0a> | ||
} | ||
@@ -66,4 +109,4 @@ VFile { | ||
messages: [], | ||
history: ['.git/HEAD'], | ||
cwd: '/Users/tilde/projects/oss/to-vfile', | ||
history: [ '.git/HEAD' ], | ||
cwd: '/Users/tilde/Projects/oss/to-vfile', | ||
value: 'ref: refs/heads/main\n' | ||
@@ -75,43 +118,195 @@ } | ||
This package exports the following identifiers: `toVFile`, `read`, `readSync`, | ||
`write`, and `writeSync`. | ||
This package exports the identifiers [`read`][api-read], | ||
[`readSync`][api-read-sync], [`toVFile`][api-to-vfile], | ||
[`write`][api-write], and [`writeSync`][api-write-sync]. | ||
There is no default export. | ||
### `toVFile(options)` | ||
### `toVFile(description)` | ||
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)}`. | ||
Create a virtual file from a description. | ||
### `read(options[, encoding][, callback])` | ||
This is like `VFile`, but it accepts a file path instead of file cotnents. | ||
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`][promise] that is rejected with | ||
an error or resolved with the populated virtual file. | ||
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()`. | ||
### `readSync(options[, encoding])` | ||
###### Parameters | ||
Like `read` but synchronous. | ||
Either throws an error or returns a populated virtual file. | ||
* `description` ([`Compatible`][api-compatible], optional) | ||
— fath to file, file options, or file itself | ||
### `write(options[, fsOptions][, callback])` | ||
###### Returns | ||
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`][promise] that is rejected with | ||
an error or resolved with the written virtual file. | ||
Given file or new file ([`VFile`][vfile]). | ||
### `writeSync(options[, fsOptions])` | ||
### `read(description[, options][, callback])` | ||
Like `write` but synchronous. | ||
Either throws an error or returns a populated virtual file. | ||
Create a virtual file and read it in, async. | ||
###### Signatures | ||
* `(description[, options], Callback): void` | ||
* `(description[, options]): Promise<VFile>` | ||
###### Parameters | ||
* `description` ([`Compatible`][api-compatible]) | ||
— path to file, file options, or file itself | ||
* `options` ([`BufferEncoding`][api-buffer-encoding], | ||
[`ReadOptions`][api-read-options], optional) | ||
* `callback` ([`Callback`][api-callback], optional) | ||
— callback called when done | ||
###### Returns | ||
Nothing when a callback is given, otherwise [promise][] that resolves to given | ||
file or new file ([`VFile`][vfile]). | ||
### `readSync(description[, options])` | ||
Create a virtual file and read it in, synchronously. | ||
###### Parameters | ||
* `description` ([`Compatible`][api-compatible]) | ||
— path to file, file options, or file itself | ||
* `options` ([`BufferEncoding`][api-buffer-encoding], | ||
[`ReadOptions`][api-read-options], optional) | ||
###### Returns | ||
Given file or new file ([`VFile`][vfile]). | ||
### `write(description[, options][, callback])` | ||
Create a virtual file and write it, async. | ||
###### Signatures | ||
* `(description[, options], Callback): void` | ||
* `(description[, options]): Promise<VFile>` | ||
###### Parameters | ||
* `description` ([`Compatible`][api-compatible]) | ||
— path to file, file options, or file itself | ||
* `options` ([`BufferEncoding`][api-buffer-encoding], | ||
[`WriteOptions`][api-write-options], optional) | ||
* `callback` ([`Callback`][api-callback], optional) | ||
— callback called when done | ||
###### Returns | ||
Nothing when a callback is given, otherwise [promise][] that resolves to given | ||
file or new file ([`VFile`][vfile]). | ||
### `writeSync(description[, options])` | ||
Create a virtual file and write it, synchronously. | ||
###### Parameters | ||
* `description` ([`Compatible`][api-compatible]) | ||
— path to file, file options, or file itself | ||
* `options` ([`BufferEncoding`][api-buffer-encoding], | ||
[`WriteOptions`][api-write-options], optional) | ||
###### Returns | ||
Given file or new file ([`VFile`][vfile]). | ||
### `BufferEncoding` | ||
Encodings supported by the buffer class (TypeScript type). | ||
This is a copy of the types from Node and [`VFile`][vfile]. | ||
###### Type | ||
```ts | ||
type BufferEncoding = | ||
| 'ascii' | ||
| 'utf8' | ||
| 'utf-8' | ||
| 'utf16le' | ||
| 'ucs2' | ||
| 'ucs-2' | ||
| 'base64' | ||
| 'base64url' | ||
| 'latin1' | ||
| 'binary' | ||
| 'hex' | ||
``` | ||
### `Callback` | ||
Callback called after reading or writing a file (TypeScript type). | ||
###### Parameters | ||
* `error` (`Error`, optional) | ||
— error when reading or writing was not successful | ||
* `file` ([`VFile`][vfile], optional) | ||
— file when reading or writing was successful | ||
###### Returns | ||
Nothing (`void`). | ||
### `Compatible` | ||
URL to file, path to file, options for file, or actual file (TypeScript type). | ||
###### Type | ||
```ts | ||
type Compatible = Buffer | URL | VFileOptions | VFile | string | ||
``` | ||
<!-- To do: fix link to `VFileOptions` when `VFile` is updated --> | ||
See [`VFileOptions`][vfile] and [`VFile`][vfile]. | ||
### `ReadOptions` | ||
Configuration for `fs.readFile` (TypeScript type). | ||
###### Fields | ||
* `encoding` ([`BufferEncoding`][api-buffer-encoding], optional) | ||
— encoding to read file as, will turn `file.value` into a string if passed | ||
* `flag` (`string`, optional) | ||
— file system flags to use | ||
### `WriteOptions` | ||
Configuration for `fs.writeFile` (TypeScript type). | ||
###### Fields | ||
* `encoding` ([`BufferEncoding`][api-buffer-encoding], optional) | ||
— encoding to write file as | ||
* `mode` (`number | string`, optional) | ||
— file mode (permission and sticky bits) if the file was newly created | ||
* `flag` (`string`, optional) | ||
— file system flags to use | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional types | ||
[`BufferEncoding`][api-buffer-encoding], | ||
[`Callback`][api-callback], | ||
[`Compatible`][api-compatible], | ||
[`ReadOptions`][api-read-options], and | ||
[`WriteOptions`][api-write-options]. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
## Contribute | ||
@@ -157,9 +352,15 @@ | ||
[contributing]: https://github.com/vfile/.github/blob/HEAD/contributing.md | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[support]: https://github.com/vfile/.github/blob/HEAD/support.md | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[contributing]: https://github.com/vfile/.github/blob/main/contributing.md | ||
[support]: https://github.com/vfile/.github/blob/main/support.md | ||
[health]: https://github.com/vfile/.github | ||
[coc]: https://github.com/vfile/.github/blob/HEAD/code-of-conduct.md | ||
[coc]: https://github.com/vfile/.github/blob/main/code-of-conduct.md | ||
@@ -173,1 +374,21 @@ [license]: license | ||
[promise]: https://developer.mozilla.org/Web/JavaScript/Reference/Global_Objects/Promise | ||
[api-read]: #readdescription-options-callback | ||
[api-read-sync]: #readsyncdescription-options | ||
[api-to-vfile]: #tovfiledescription | ||
[api-write]: #writedescription-options-callback | ||
[api-write-sync]: #writesyncdescription-options | ||
[api-buffer-encoding]: #bufferencoding | ||
[api-callback]: #callback | ||
[api-compatible]: #compatible | ||
[api-read-options]: #readoptions | ||
[api-write-options]: #writeoptions |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25977
8
414
386