MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / assemble

Method assemble

src/packagedcode/npm.py:82–210  ·  view source on GitHub ↗

If ``resource``, or one of its siblings, is a package.json file, use it to create and yield the package, the package dependencies, and the package resources. When reporting the resources of a package, we walk the codebase, skipping the node_modules directory

(cls, package_data, resource, codebase, package_adder)

Source from the content-addressed store, hash-verified

80
81 @classmethod
82 def assemble(cls, package_data, resource, codebase, package_adder):
83 """
84 If ``resource``, or one of its siblings, is a package.json file, use it
85 to create and yield the package, the package dependencies, and the
86 package resources.
87
88 When reporting the resources of a package, we walk the codebase, skipping
89 the node_modules directory, assign resources to the package and yield
90 resources.
91
92 For each lock file, assign dependencies to package instances and yield dependencies.
93
94 If there is no package.json, we do not have a package instance. In this
95 case, we yield each of the dependencies in each lock file.
96 """
97
98 package_resource = None
99 if resource.name == 'package.json':
100 package_resource = resource
101 elif resource.name in cls.lockfile_names:
102 if resource.has_parent():
103 siblings = resource.siblings(codebase)
104 package_resource = [r for r in siblings if r.name == 'package.json']
105 if package_resource:
106 package_resource = package_resource[0]
107
108 if not package_resource:
109 # we do not have a package.json
110 yield from yield_dependencies_from_package_resource(resource)
111 return
112
113 if codebase.has_single_resource:
114 yield from models.DatafileHandler.assemble(package_data, resource, codebase, package_adder)
115 return
116
117 # We do not have any package data detected here
118 if not package_resource.package_data:
119 return
120
121 assert len(package_resource.package_data) == 1, f'Invalid package.json for {package_resource.path}'
122 pkg_data = package_resource.package_data[0]
123 pkg_data = models.PackageData.from_dict(pkg_data)
124
125 workspace_root = package_resource.parent(codebase)
126 workspace_root_path = None
127 if workspace_root:
128 workspace_root_path = workspace_root.path
129 workspaces = pkg_data.extra_data.get('workspaces') or []
130
131 # Also look for pnpm workspaces
132 pnpm_workspace = None
133 if not workspaces and workspace_root:
134 pnpm_workspace_path = os.path.join(workspace_root_path, 'pnpm-workspace.yaml')
135 pnpm_workspace = codebase.get_resource(path=pnpm_workspace_path)
136 if pnpm_workspace:
137 pnpm_workspace_pkg_data = pnpm_workspace.package_data
138 if pnpm_workspace_pkg_data:
139 workspace_package = pnpm_workspace_pkg_data[0]

Callers 1

get_package_and_depsFunction · 0.45

Calls 15

has_parentMethod · 0.80
siblingsMethod · 0.80
parentMethod · 0.80
get_resourceMethod · 0.80
get_workspace_membersMethod · 0.80
from_package_dataMethod · 0.80
walk_npmMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected