-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read-package-json.mts
95 lines (87 loc) · 2.43 KB
/
read-package-json.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @file readPackageJson
* @module mlly/lib/readPackageJson
*/
import dfs from '#internal/fs'
import isFile from '#lib/is-file'
import {
ERR_INVALID_PACKAGE_CONFIG,
type ErrInvalidPackageConfig
} from '@flex-development/errnode'
import type { FileSystem, ModuleId } from '@flex-development/mlly'
import pathe from '@flex-development/pathe'
import type { PackageJson } from '@flex-development/pkg-types'
/**
* Read a `package.json` file.
*
* Implements the `READ_PACKAGE_JSON` algorithm.
*
* @see {@linkcode ErrInvalidPackageConfig}
* @see {@linkcode FileSystem}
* @see {@linkcode ModuleId}
* @see {@linkcode PackageJson}
*
* @async
*
* @param {ModuleId} id
* URL of package directory, `package.json` file, or module in the same
* directory as a `package.json` file
* @param {string | null | undefined} [specifier]
* Module specifier that initiated reading of `package.json` file.\
* Should be a `file:` URL if `parent` is a not a URL
* @param {ModuleId | null | undefined} [parent]
* URL of parent module
* @param {FileSystem | null | undefined} [fs]
* File system API
* @return {Promise<PackageJson | null>}
* Parsed file contents or `null`
* @throws {ErrInvalidPackageConfig}
* If `package.json` file does not parse as valid JSON
*/
async function readPackageJson(
id: ModuleId,
specifier?: string | null | undefined,
parent?: ModuleId | null | undefined,
fs?: FileSystem | null | undefined
): Promise<PackageJson | null> {
/**
* URL of `package.json` file.
*
* @const {URL} url
*/
const url: URL = new URL('package.json', id)
if (await isFile(url, fs)) {
/**
* Stringified package config.
*
* @const {string} data
*/
const data: string = String(await (fs ?? dfs).readFile(url))
try {
return JSON.parse(data) as PackageJson
} catch (e: unknown) {
/**
* Error cause.
*
* @const {Error} cause
*/
const cause: Error = e as Error
/**
* Invalid package config error.
*
* @const {ErrInvalidPackageConfig} error
*/
const error: ErrInvalidPackageConfig = new ERR_INVALID_PACKAGE_CONFIG(
url.pathname,
parent && specifier
? `"${specifier}" from ${pathe.fileURLToPath(parent)}`
: specifier && pathe.fileURLToPath(specifier),
cause.message
)
error.cause = cause
throw error
}
}
return null
}
export default readPackageJson