Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 13, 2023
1 parent 6e0dd65 commit 8d484e8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 91 deletions.
56 changes: 36 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/**
* @typedef {import('vfile').VFileValue} Value
* @typedef {import('vfile').VFileOptions} Options
* @typedef {import('vfile').BufferEncoding} BufferEncoding
* Encodings supported by the buffer class.
*
* This is a copy of the types from Node and `VFile`.
*
* @typedef {import('vfile').VFileOptions} Options
* @typedef {import('vfile').VFileValue} Value
*/

/**
* @typedef ReadOptions
* Configuration for `fs.readFile`.
* @property {BufferEncoding | null | undefined} [encoding]
Expand All @@ -17,16 +19,16 @@
* 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.
* @property {number | string | undefined} [mode]
* File mode (permission and sticky bits) if the file was newly created.
*
* @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
* @typedef {Options | Path | VFile} Compatible
* URL to file, path to file, options for file, or actual file.
*/

Expand All @@ -37,6 +39,20 @@
* Error when reading or writing was not successful.
* @param {VFile | null | undefined} file
* File when reading or writing was successful.
*
* @callback Resolve
* @param {VFile} result
* File.
* @returns {void}
* Nothing (note: has to be `void` for TS’s `Promise` interface).
*
* @callback Reject
* @param {NodeJS.ErrnoException} error
* Error.
* @param {VFile | undefined} [result]
* File.
* @returns {void}
* Nothing (note: has to be `void` for TS’s `Promise` interface).
*/

import fs from 'fs'
Expand Down Expand Up @@ -70,10 +86,8 @@ export function toVFile(description) {
description = {path: String(description)}
}

return looksLikeAVFile(description)
? description
: // To do: remove when `VFile` allows explicit `null`.
new VFile(description || undefined)
// To do: do not return given file.
return looksLikeAVFile(description) ? description : new VFile(description)
}

/**
Expand Down Expand Up @@ -124,8 +138,8 @@ export function writeSync(description, options) {
export const read =
/**
* @type {{
* (description: Compatible, options: BufferEncoding | ReadOptions | null | undefined, callback: Callback): void
* (description: Compatible, callback: Callback): void
* (description: Compatible, options: BufferEncoding | ReadOptions | null | undefined, callback: Callback): undefined
* (description: Compatible, callback: Callback): undefined
* (description: Compatible, options?: BufferEncoding | ReadOptions | null | undefined): Promise<VFile>
* }}
*/
Expand Down Expand Up @@ -158,8 +172,10 @@ export const read =
}

/**
* @param {(error: VFile) => void} resolve
* @param {(error: NodeJS.ErrnoException, file?: VFile | undefined) => void} reject
* @param {Resolve} resolve
* @param {Reject} reject
* @returns {void}
* Nothing (note: has to be `void` for TS’s `Promise` interface).
*/
function executor(resolve, reject) {
/** @type {string} */
Expand Down Expand Up @@ -206,8 +222,8 @@ export const read =
export const write =
/**
* @type {{
* (description: Compatible, options: BufferEncoding | WriteOptions | null | undefined, callback: Callback): void
* (description: Compatible, callback: Callback): void
* (description: Compatible, options: BufferEncoding | WriteOptions | null | undefined, callback: Callback): undefined
* (description: Compatible, callback: Callback): undefined
* (description: Compatible, options?: BufferEncoding | WriteOptions | null | undefined): Promise<VFile>
* }}
*/
Expand Down Expand Up @@ -241,8 +257,8 @@ export const write =
}

/**
* @param {(error: VFile) => void} resolve
* @param {(error: NodeJS.ErrnoException, file: VFile | null) => void} reject
* @param {Resolve} resolve
* @param {Reject} reject
*/
function executor(resolve, reject) {
/** @type {string} */
Expand All @@ -252,7 +268,7 @@ export const write =
fp = path.resolve(file.cwd, file.path)
} catch (error) {
const exception = /** @type {NodeJS.ErrnoException} */ (error)
return reject(exception, null)
return reject(exception)
}

fs.writeFile(fp, file.value || '', options || null, done)
Expand All @@ -262,7 +278,7 @@ export const write =
*/
function done(error) {
if (error) {
reject(error, null)
reject(error)
} else {
resolve(file)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "to-vfile",
"version": "7.2.4",
"description": "vfile utility to create a vfile from a filepath",
"description": "vfile utility to read and write to the file system",
"license": "MIT",
"keywords": [
"vfile",
Expand Down
Loading

0 comments on commit 8d484e8

Please sign in to comment.