Skip to content

Commit

Permalink
refactor(utils-node): update jsdocs and var renames
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Feb 8, 2023
1 parent 30659e3 commit 5d20fbf
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/utils/node.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import path from "node:path"
import { readFile } from "node:fs/promises"
import { join, resolve } from "node:path"

import { cyan } from "./colors.js"

/**
* Format Node.js `process.hrtime()` output to a human readable string.
*
* @param {[number, number]} input
* @param {[number, number]} input Node.js `process.hrtime()` output
* @returns {string}
*
* @example
Expand Down Expand Up @@ -40,21 +39,21 @@ export const getNodeVersion = () => {
/**
* Read a file and parse it as JSON.
*
* @param {string} path
* @param {string} input File path
*
* @returns {Promise<Record<string, unknown>>}
* @example
* readFileAsJSON("/home/user/tsd-lite-cli/package.json")
* // => { name: "tsd-lite-cli", version: "1.0.0", ... }
*/
export const readFileAsJSON = path =>
readFile(path, "utf8")
export const readFileAsJSON = input =>
readFile(input, "utf8")
.then(buffer => JSON.parse(buffer.toString()))
.catch(error => {
console.error(
"readFileAsJSON",
"ERROR",
`Failed to parse JSON file: ${cyan.fg(path)}`
`Failed to parse JSON file: ${cyan.fg(input)}`
)

throw error
Expand All @@ -78,7 +77,9 @@ export const readFileAsJSON = path =>
*/
export const getPackageInfo = () => {
const __dirname = new URL(import.meta.url).pathname
const path = join(__dirname, "..", "..", "..", "package.json")
const filePath = path.join(__dirname, "..", "..", "..", "package.json")

return /** @type {Promise<PackageJSON>} */ (readFileAsJSON(resolve(path)))
return /** @type {Promise<PackageJSON>} */ (
readFileAsJSON(path.resolve(filePath))
)
}

0 comments on commit 5d20fbf

Please sign in to comment.