1
+ import path from "node:path"
1
2
import { readFile } from "node:fs/promises"
2
- import { join , resolve } from "node:path"
3
-
4
3
import { cyan } from "./colors.js"
5
4
6
5
/**
7
6
* Format Node.js `process.hrtime()` output to a human readable string.
8
7
*
9
- * @param {[number, number] } input
8
+ * @param {[number, number] } input Node.js `process.hrtime()` output
10
9
* @returns {string }
11
10
*
12
11
* @example
@@ -40,21 +39,21 @@ export const getNodeVersion = () => {
40
39
/**
41
40
* Read a file and parse it as JSON.
42
41
*
43
- * @param {string } path
42
+ * @param {string } input File path
44
43
*
45
44
* @returns {Promise<Record<string, unknown>> }
46
45
* @example
47
46
* readFileAsJSON("/home/user/tsd-lite-cli/package.json")
48
47
* // => { name: "tsd-lite-cli", version: "1.0.0", ... }
49
48
*/
50
- export const readFileAsJSON = path =>
51
- readFile ( path , "utf8" )
49
+ export const readFileAsJSON = input =>
50
+ readFile ( input , "utf8" )
52
51
. then ( buffer => JSON . parse ( buffer . toString ( ) ) )
53
52
. catch ( error => {
54
53
console . error (
55
54
"readFileAsJSON" ,
56
55
"ERROR" ,
57
- `Failed to parse JSON file: ${ cyan . fg ( path ) } `
56
+ `Failed to parse JSON file: ${ cyan . fg ( input ) } `
58
57
)
59
58
60
59
throw error
@@ -78,7 +77,9 @@ export const readFileAsJSON = path =>
78
77
*/
79
78
export const getPackageInfo = ( ) => {
80
79
const __dirname = new URL ( import . meta. url ) . pathname
81
- const path = join ( __dirname , ".." , ".." , ".." , "package.json" )
80
+ const filePath = path . join ( __dirname , ".." , ".." , ".." , "package.json" )
82
81
83
- return /** @type {Promise<PackageJSON> } */ ( readFileAsJSON ( resolve ( path ) ) )
82
+ return /** @type {Promise<PackageJSON> } */ (
83
+ readFileAsJSON ( path . resolve ( filePath ) )
84
+ )
84
85
}
0 commit comments