diff --git a/lib/utils/node.js b/lib/utils/node.js index 9fbb29f..c8230d1 100644 --- a/lib/utils/node.js +++ b/lib/utils/node.js @@ -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 @@ -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>} * @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 @@ -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} */ (readFileAsJSON(resolve(path))) + return /** @type {Promise} */ ( + readFileAsJSON(path.resolve(filePath)) + ) }