@pnpm/workspace.projects-graph 1000.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-2016 Rico Sta. Cruz and other contributors
4
+ Copyright (c) 2016-2026 Zoltan Kochan and other contributors
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # @pnpm/workspace.projects-graph
2
+
3
+ > Create a graph from an array of packages
4
+
5
+ [![npm version](https://img.shields.io/npm/v/pkgs-graph.svg)](https://www.npmjs.com/package/pkgs-graph)
6
+
7
+ ## Installation
8
+
9
+ ```
10
+ pnpm add @pnpm/workspace.projects-graph
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```js
16
+ import createPkgsGraph from 'pkgs-graph'
17
+
18
+ const {graph} = createPkgsGraph([
19
+ {
20
+ dir: '/home/zkochan/src/foo',
21
+ manifest: {
22
+ name: 'foo',
23
+ version: '1.0.0',
24
+ dependencies: {
25
+ bar: '^1.0.0',
26
+ },
27
+ },
28
+ },
29
+ {
30
+ dir: '/home/zkochan/src/bar',
31
+ manifest: {
32
+ name: 'bar',
33
+ version: '1.1.0',
34
+ },
35
+ }
36
+ ])
37
+
38
+ console.log(graph)
39
+ //> {
40
+ // '/home/zkochan/src/foo': {
41
+ // dependencies: ['/home/zkochan/src/bar'],
42
+ // manifest: {
43
+ // name: 'foo',
44
+ // version: '1.0.0',
45
+ // dependencies: {
46
+ // bar: '^1.0.0',
47
+ // },
48
+ // },
49
+ // },
50
+ // '/home/zkochan/src/bar': {
51
+ // dependencies: [],
52
+ // manifest: {
53
+ // name: 'bar',
54
+ // version: '1.1.0',
55
+ // },
56
+ // },
57
+ // }
58
+ ```
59
+
60
+ ## License
61
+
62
+ [MIT](LICENSE) © [Zoltan Kochan](https://www.kochan.io)
package/lib/index.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import type { BaseManifest, ProjectRootDir } from '@pnpm/types';
2
+ export interface BaseProject {
3
+ manifest: BaseManifest;
4
+ rootDir: ProjectRootDir;
5
+ }
6
+ export interface ProjectGraphNode<Pkg extends BaseProject> {
7
+ package: Pkg;
8
+ dependencies: ProjectRootDir[];
9
+ }
10
+ export declare function createProjectsGraph<Pkg extends BaseProject>(projects: Pkg[], opts?: {
11
+ ignoreDevDeps?: boolean;
12
+ linkWorkspacePackages?: boolean;
13
+ }): {
14
+ graph: Record<ProjectRootDir, ProjectGraphNode<Pkg>>;
15
+ unmatched: Array<{
16
+ pkgName: string;
17
+ range: string;
18
+ }>;
19
+ };
package/lib/index.js ADDED
@@ -0,0 +1,110 @@
1
+ import path from 'node:path';
2
+ import npa from '@pnpm/npm-package-arg';
3
+ import { parseBareSpecifier, workspacePrefToNpm } from '@pnpm/resolving.npm-resolver';
4
+ import { resolveWorkspaceRange } from '@pnpm/workspace.range-resolver';
5
+ import { map as mapValues } from 'ramda';
6
+ export function createProjectsGraph(projects, opts) {
7
+ const projectMap = createProjectMap(projects);
8
+ const projectMapValues = Object.values(projectMap);
9
+ let projectMapByManifestName;
10
+ let projectMapByDir;
11
+ const unmatched = [];
12
+ const graph = mapValues((project) => ({
13
+ dependencies: createNode(project),
14
+ package: project,
15
+ }), projectMap);
16
+ return { graph, unmatched };
17
+ function createNode(project) {
18
+ const dependencies = {
19
+ ...project.manifest.peerDependencies,
20
+ ...(!opts?.ignoreDevDeps && project.manifest.devDependencies),
21
+ ...project.manifest.optionalDependencies,
22
+ ...project.manifest.dependencies,
23
+ };
24
+ return Object.entries(dependencies)
25
+ .map(([depName, rawSpec]) => {
26
+ let spec;
27
+ const isWorkspaceSpec = rawSpec.startsWith('workspace:');
28
+ try {
29
+ if (isWorkspaceSpec) {
30
+ const { fetchSpec, name } = parseBareSpecifier(workspacePrefToNpm(rawSpec), depName, 'latest', '');
31
+ rawSpec = fetchSpec;
32
+ depName = name;
33
+ }
34
+ spec = npa.resolve(depName, rawSpec, project.rootDir);
35
+ }
36
+ catch {
37
+ return '';
38
+ }
39
+ if (spec.type === 'directory') {
40
+ projectMapByDir ??= getProjectMapByDir(projectMapValues);
41
+ const resolvedPath = path.resolve(project.rootDir, spec.fetchSpec);
42
+ const found = projectMapByDir[resolvedPath];
43
+ if (found) {
44
+ return found.rootDir;
45
+ }
46
+ // Slow path; only needed when there are case mismatches on case-insensitive filesystems.
47
+ const matchedProject = projectMapValues.find(p => path.relative(p.rootDir, spec.fetchSpec) === '');
48
+ if (matchedProject == null) {
49
+ return '';
50
+ }
51
+ projectMapByDir[resolvedPath] = matchedProject;
52
+ return matchedProject.rootDir;
53
+ }
54
+ if (spec.type !== 'version' && spec.type !== 'range')
55
+ return '';
56
+ projectMapByManifestName ??= getProjectMapByManifestName(projectMapValues);
57
+ const candidates = projectMapByManifestName[depName];
58
+ if (!candidates || candidates.length === 0)
59
+ return '';
60
+ const versions = candidates.filter(({ manifest }) => manifest.version)
61
+ .map(p => p.manifest.version);
62
+ // explicitly check if false, backwards-compatibility (can be undefined)
63
+ const strictWorkspaceMatching = opts?.linkWorkspacePackages === false && !isWorkspaceSpec;
64
+ if (strictWorkspaceMatching) {
65
+ unmatched.push({ pkgName: depName, range: rawSpec });
66
+ return '';
67
+ }
68
+ if (isWorkspaceSpec && versions.length === 0) {
69
+ const matchedProject = candidates.find(p => p.manifest.name === depName);
70
+ return matchedProject.rootDir;
71
+ }
72
+ if (versions.includes(rawSpec)) {
73
+ const matchedProject = candidates.find(p => p.manifest.name === depName && p.manifest.version === rawSpec);
74
+ return matchedProject.rootDir;
75
+ }
76
+ const matched = resolveWorkspaceRange(rawSpec, versions);
77
+ if (!matched) {
78
+ unmatched.push({ pkgName: depName, range: rawSpec });
79
+ return '';
80
+ }
81
+ const matchedProject = candidates.find(p => p.manifest.name === depName && p.manifest.version === matched);
82
+ return matchedProject.rootDir;
83
+ })
84
+ .filter(Boolean);
85
+ }
86
+ }
87
+ function createProjectMap(projects) {
88
+ const projectMap = {};
89
+ for (const project of projects) {
90
+ projectMap[project.rootDir] = project;
91
+ }
92
+ return projectMap;
93
+ }
94
+ function getProjectMapByManifestName(projectMapValues) {
95
+ const projectMapByManifestName = {};
96
+ for (const project of projectMapValues) {
97
+ if (project.manifest.name) {
98
+ (projectMapByManifestName[project.manifest.name] ??= []).push(project);
99
+ }
100
+ }
101
+ return projectMapByManifestName;
102
+ }
103
+ function getProjectMapByDir(projectMapValues) {
104
+ const projectMapByDir = {};
105
+ for (const project of projectMapValues) {
106
+ projectMapByDir[path.resolve(project.rootDir)] = project;
107
+ }
108
+ return projectMapByDir;
109
+ }
110
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@pnpm/workspace.projects-graph",
3
+ "version": "1000.0.25",
4
+ "description": "Create a graph from an array of packages",
5
+ "keywords": [
6
+ "pnpm",
7
+ "pnpm11"
8
+ ],
9
+ "license": "MIT",
10
+ "funding": "https://opencollective.com/pnpm",
11
+ "repository": "https://github.com/pnpm/pnpm/tree/main/workspace/projects-graph",
12
+ "homepage": "https://github.com/pnpm/pnpm/tree/main/workspace/projects-graph#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/pnpm/pnpm/issues"
15
+ },
16
+ "type": "module",
17
+ "main": "lib/index.js",
18
+ "types": "lib/index.d.ts",
19
+ "exports": {
20
+ ".": "./lib/index.js"
21
+ },
22
+ "files": [
23
+ "lib",
24
+ "!*.map"
25
+ ],
26
+ "dependencies": {
27
+ "@pnpm/npm-package-arg": "^2.0.0",
28
+ "ramda": "npm:@pnpm/ramda@0.28.1",
29
+ "@pnpm/resolving.npm-resolver": "1004.4.1",
30
+ "@pnpm/workspace.range-resolver": "1000.0.0",
31
+ "@pnpm/types": "1000.9.0"
32
+ },
33
+ "devDependencies": {
34
+ "@types/ramda": "0.29.12",
35
+ "better-path-resolve": "2.0.0",
36
+ "@pnpm/workspace.projects-graph": "1000.0.25"
37
+ },
38
+ "engines": {
39
+ "node": ">=22.13"
40
+ },
41
+ "jest": {
42
+ "preset": "@pnpm/jest-config"
43
+ },
44
+ "scripts": {
45
+ "lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
46
+ "_test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules --disable-warning=ExperimentalWarning --disable-warning=DEP0169\" jest",
47
+ "test": "pnpm run compile && pnpm run _test",
48
+ "compile": "tsgo --build && pnpm run lint --fix"
49
+ }
50
+ }