-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.ts
More file actions
45 lines (39 loc) · 1.38 KB
/
Copy pathparse.ts
File metadata and controls
45 lines (39 loc) · 1.38 KB
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
/**
* @file `parse(filename, content)` — auto-detects format from filename then
* dispatches to `parseManifest` or `parseLockfile`. Throws
* `ManifestError(ERR_UNKNOWN_FORMAT)` when the filename doesn't match any
* recognized manifest or lockfile basename.
*/
import { ManifestError } from './manifest-error'
import { detectFormat } from './detect-format'
import { parseLockfile } from './parse-lockfile'
import { parseManifest } from './parse-manifest'
import { getSmolManifest } from '../../smol/manifest'
import type { ParsedLockfile, ParsedManifest } from './types'
export function jsParse(
filename: string,
content: string,
): ParsedManifest | ParsedLockfile {
const format = detectFormat(filename)
if (!format) {
throw new ManifestError(
`Unknown file format: ${filename}`,
'ERR_UNKNOWN_FORMAT',
)
}
if (format.type === 'manifest') {
return parseManifest(content, format.ecosystem)
}
return parseLockfile(content, format.ecosystem, format.format)
}
const smol = getSmolManifest()
/* c8 ignore start - smol-fallback branch is smol Node binary only. */
const smolParse = smol
? (filename: string, content: string) =>
smol.parse(filename, content) as ParsedManifest | ParsedLockfile
: undefined
/* c8 ignore stop */
export const parse: (
filename: string,
content: string,
) => ParsedManifest | ParsedLockfile = smolParse ?? jsParse