Skip to content

Commit 470ebc4

Browse files
committed
Add improved docs
1 parent 164d3a7 commit 470ebc4

File tree

4 files changed

+238
-53
lines changed

4 files changed

+238
-53
lines changed

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
/**
2-
* @typedef {import('./lib/index.js').Compatible} Compatible
3-
* @typedef {import('./lib/index.js').Callback} Callback
42
* @typedef {import('./lib/index.js').BufferEncoding} BufferEncoding
5-
* @typedef {import('./lib/index.js').Mode} Mode
3+
* @typedef {import('./lib/index.js').Callback} Callback
4+
* @typedef {import('./lib/index.js').Compatible} Compatible
65
* @typedef {import('./lib/index.js').ReadOptions} ReadOptions
76
* @typedef {import('./lib/index.js').WriteOptions} WriteOptions
87
*/
98

9+
// To do: next major: don’t expose `Mode`.
10+
/**
11+
* @typedef {number | string} Mode
12+
*/
13+
1014
export {toVFile, read, readSync, write, writeSync} from './lib/index.js'

lib/index.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@
22
* @typedef {import('vfile').VFileValue} Value
33
* @typedef {import('vfile').VFileOptions} Options
44
* @typedef {import('vfile').BufferEncoding} BufferEncoding
5+
* Encodings supported by the buffer class.
56
*
6-
* @typedef {number | string} Mode
7-
* @typedef {{encoding?: BufferEncoding | null | undefined, flag?: string | undefined}} ReadOptions
8-
* @typedef {{encoding?: null | undefined | BufferEncoding, mode?: Mode | undefined, flag?: string | undefined}} WriteOptions
7+
* This is a copy of the types from Node and `VFile`.
98
*
9+
* @typedef ReadOptions
10+
* Configuration for `fs.readFile`.
11+
* @property {BufferEncoding | null | undefined} [encoding]
12+
* Encoding to read file as, will turn `file.value` into a string if passed.
13+
* @property {string | undefined} [flag]
14+
* File system flags to use.
15+
*
16+
* @typedef WriteOptions
17+
* Configuration for `fs.writeFile`.
18+
* @property {BufferEncoding | null | undefined} [encoding]
19+
* Encoding to write file as.
20+
* @property {number | string | undefined} [mode]
21+
* File mode (permission and sticky bits) if the file was newly created.
22+
* @property {string | undefined} [flag]
23+
* File system flags to use.
1024
*
1125
* @typedef {URL | Value} Path
1226
* URL to file or path to file.
@@ -38,14 +52,16 @@ import {VFile} from 'vfile'
3852
/**
3953
* Create a virtual file from a description.
4054
*
55+
* This is like `VFile`, but it accepts a file path instead of file cotnents.
56+
*
4157
* If `options` is a string, URL, or buffer, it’s used as the path.
42-
* Otherwise, if it’s a `VFile`, that’s returned instead.
58+
* Otherwise, if it’s a file, that’s returned instead.
4359
* Otherwise, the options are passed through to `new VFile()`.
4460
*
4561
* @param {Compatible | null | undefined} [description]
4662
* Path to file, file options, or file itself.
4763
* @returns {VFile}
48-
* Given `VFile` or new `VFile`.
64+
* Given file or new file.
4965
*/
5066
export function toVFile(description) {
5167
if (typeof description === 'string' || description instanceof URL) {
@@ -68,7 +84,7 @@ export function toVFile(description) {
6884
* @param {BufferEncoding | ReadOptions | null | undefined} [options]
6985
* Encoding to use or Node.JS read options.
7086
* @returns {VFile}
71-
* Given `VFile` or new `VFile`.
87+
* Given file or new file.
7288
*/
7389
export function readSync(description, options) {
7490
const file = toVFile(description)
@@ -84,7 +100,7 @@ export function readSync(description, options) {
84100
* @param {BufferEncoding | WriteOptions | null | undefined} [options]
85101
* Encoding to use or Node.JS write options.
86102
* @returns {VFile}
87-
* Given `VFile` or new `VFile`.
103+
* Given file or new file.
88104
*/
89105
export function writeSync(description, options) {
90106
const file = toVFile(description)
@@ -103,12 +119,12 @@ export function writeSync(description, options) {
103119
* Callback called when done.
104120
* @returns
105121
* Nothing when a callback is given, otherwise promise that resolves to given
106-
* `VFile` or new `VFile`.
122+
* file or new file.
107123
*/
108124
export const read =
109125
/**
110126
* @type {{
111-
* (description: Compatible, options: BufferEncoding | ReadOptions, callback: Callback): void
127+
* (description: Compatible, options: BufferEncoding | ReadOptions | null | undefined, callback: Callback): void
112128
* (description: Compatible, callback: Callback): void
113129
* (description: Compatible, options?: BufferEncoding | ReadOptions | null | undefined): Promise<VFile>
114130
* }}
@@ -185,12 +201,12 @@ export const read =
185201
* Callback called when done.
186202
* @returns
187203
* Nothing when a callback is given, otherwise promise that resolves to given
188-
* `VFile` or new `VFile`.
204+
* file or new file.
189205
*/
190206
export const write =
191207
/**
192208
* @type {{
193-
* (description: Compatible, options: BufferEncoding | WriteOptions, callback: Callback): void
209+
* (description: Compatible, options: BufferEncoding | WriteOptions | null | undefined, callback: Callback): void
194210
* (description: Compatible, callback: Callback): void
195211
* (description: Compatible, options?: BufferEncoding | WriteOptions | null | undefined): Promise<VFile>
196212
* }}
@@ -272,6 +288,7 @@ function looksLikeAVFile(value) {
272288
)
273289
}
274290

291+
// To do: next major: remove?
275292
toVFile.readSync = readSync
276293
toVFile.writeSync = writeSync
277294
toVFile.read = read

0 commit comments

Comments
 (0)