-
Notifications
You must be signed in to change notification settings - Fork 167
/
npm.py
32 lines (25 loc) · 933 Bytes
/
npm.py
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
"""Parsers for npm related dependency files i.e. package.json"""
import json
from readmeai.parsers.base import BaseFileParser
class PackageJsonParser(BaseFileParser):
"""
Parser for package.json dependency files.
"""
def __init__(self) -> None:
"""Initializes the handler with given configuration."""
super().__init__()
def parse(self, content: str) -> list[str]:
"""Returns a list of dependencies parsed from a json file."""
try:
data = json.loads(content)
package_names = []
for section in [
"dependencies",
"devDependencies",
"peerDependencies",
]:
if section in data:
package_names.extend(data[section].keys())
return package_names
except json.JSONDecodeError as exc:
return self.handle_parsing_error(exc)