@miadi/relational-git-repository-mapper 0.3.0 → 0.5.0

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/dist/read.d.ts CHANGED
@@ -1,4 +1,9 @@
1
- import type { MappingIssue, MergedMapping } from "@medicine-wheel/developer-repository-mapping";
1
+ import { SHIPPED_CAPABILITIES_SCANS } from "./shipped-capabilities";
2
+ import { type BumpLevel, type ImpactAnswer, type PackageReading } from "./impact";
3
+ import { type RepositoryWhere } from "./hosts";
4
+ import { type CapabilityFacet } from "@medicine-wheel/capabilities";
5
+ import { type CapabilityKind, type ProvidesEdge } from "./capabilities";
6
+ import { type MappingIssue, type MergedMapping } from "@medicine-wheel/developer-repository-mapping";
2
7
  import { type SessionsByRepository } from "./join";
3
8
  import { type LineageEdge, type LineageGraph, type RepositoryLineage } from "./lineage";
4
9
  import type { HubState, InventoryState, RepositoryListing, RepositoryView } from "./types";
@@ -14,7 +19,61 @@ export interface ReadOptions {
14
19
  inventory?: string;
15
20
  /** a lineage JSONL read before the shipped one; default `MIADI_REPOSITORY_LINEAGE` */
16
21
  lineage?: string;
22
+ /** a capability JSONL read before the shipped rows; default `MIADI_REPOSITORY_CAPABILITIES` */
23
+ capabilities?: string;
24
+ /** a where JSONL (`miadi-repomap where --write`) read before the shipped readings; default `MIADI_REPOSITORY_HOSTS` */
25
+ hosts?: string;
17
26
  }
27
+ export interface WhereRead {
28
+ /** the sources read, in precedence order */
29
+ from: string[];
30
+ readings: RepositoryWhere[];
31
+ issues: MappingIssue[];
32
+ }
33
+ /** Where repositories live: a host's own readings first, then the readings this package ships. */
34
+ export declare function readWhere(path?: string | undefined): WhereRead;
35
+ /** The packages the shipped scan read, with their versions and first-party ranges. */
36
+ export declare function readPackages(): {
37
+ packages: PackageReading[];
38
+ issues: MappingIssue[];
39
+ };
40
+ export interface ImpactRead {
41
+ scans: typeof SHIPPED_CAPABILITIES_SCANS;
42
+ issues: MappingIssue[];
43
+ impact?: ImpactAnswer;
44
+ }
45
+ /** What bumping a package touches: what it consumes, who consumes it, which ranges stop reaching the new version. */
46
+ export declare function repositoryImpact(packageOrCapability: string, bump?: BumpLevel): ImpactRead;
47
+ export interface CapabilitiesRead {
48
+ /** the sources read, in precedence order */
49
+ from: string[];
50
+ /** the commits the shipped rows were scanned at */
51
+ scans: typeof SHIPPED_CAPABILITIES_SCANS;
52
+ capabilities: CapabilityFacet[];
53
+ issues: MappingIssue[];
54
+ }
55
+ /**
56
+ * The capabilities: a host's own JSONL first (`--capabilities` or
57
+ * `MIADI_REPOSITORY_CAPABILITIES`), then the rows this package ships, scanned
58
+ * from the repositories' default branches. The first source to name a
59
+ * capability speaks for it; a later row on the same id is reported and skipped.
60
+ */
61
+ export declare function readCapabilities(path?: string | undefined): CapabilitiesRead;
62
+ export interface CapabilitiesAnswer {
63
+ from: string[];
64
+ scans: typeof SHIPPED_CAPABILITIES_SCANS;
65
+ issues: MappingIssue[];
66
+ /** with a repository: the one asked about */
67
+ repository?: string;
68
+ /** how many of each kind */
69
+ counts: Record<CapabilityKind | "other", number>;
70
+ capabilities: (CapabilityFacet & {
71
+ kind?: CapabilityKind;
72
+ })[];
73
+ provides: ProvidesEdge[];
74
+ }
75
+ /** "What does this repository provide?", or with no repository, every capability. Reads no hub and no inventory. */
76
+ export declare function repositoryCapabilities(repository?: string, options?: Pick<ReadOptions, "capabilities">): CapabilitiesAnswer;
18
77
  export interface LineageRead {
19
78
  /** the files read, in precedence order */
20
79
  from: string[];
@@ -37,6 +96,8 @@ export interface MapperRead {
37
96
  inventory: InventoryState;
38
97
  sessions: SessionsByRepository;
39
98
  lineage: LineageRead;
99
+ capabilities: CapabilitiesRead;
100
+ where: WhereRead;
40
101
  }
41
102
  export declare function readMapper(options?: ReadOptions): Promise<MapperRead>;
42
103
  export declare function repositoryListing(options?: ReadOptions): Promise<RepositoryListing>;
package/dist/read.js CHANGED
@@ -5,6 +5,11 @@
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.inventoryDir = inventoryDir;
7
7
  exports.hubPath = hubPath;
8
+ exports.readWhere = readWhere;
9
+ exports.readPackages = readPackages;
10
+ exports.repositoryImpact = repositoryImpact;
11
+ exports.readCapabilities = readCapabilities;
12
+ exports.repositoryCapabilities = repositoryCapabilities;
8
13
  exports.readLineage = readLineage;
9
14
  exports.readHubState = readHubState;
10
15
  exports.readMapper = readMapper;
@@ -13,9 +18,16 @@ exports.repositoryDetail = repositoryDetail;
13
18
  exports.repositoryLineage = repositoryLineage;
14
19
  const node_fs_1 = require("node:fs");
15
20
  const shipped_lineage_1 = require("./shipped-lineage");
21
+ const shipped_capabilities_1 = require("./shipped-capabilities");
22
+ const shipped_hosts_1 = require("./shipped-hosts");
23
+ const impact_1 = require("./impact");
24
+ const hosts_1 = require("./hosts");
25
+ const capabilities_1 = require("@medicine-wheel/capabilities");
26
+ const capabilities_2 = require("./capabilities");
16
27
  const node_os_1 = require("node:os");
17
28
  const node_path_1 = require("node:path");
18
29
  const node_1 = require("@medicine-wheel/developer-repository-mapping/node");
30
+ const developer_repository_mapping_1 = require("@medicine-wheel/developer-repository-mapping");
19
31
  const hooks_interpreter_1 = require("@miadi/hooks-interpreter");
20
32
  const join_1 = require("./join");
21
33
  const lineage_1 = require("./lineage");
@@ -27,6 +39,95 @@ function inventoryDir(env = process.env) {
27
39
  function hubPath(env = process.env) {
28
40
  return env.MIADI_REPOSITORY_MAPPING_HUB?.trim() || undefined;
29
41
  }
42
+ /** Where repositories live: a host's own readings first, then the readings this package ships. */
43
+ function readWhere(path = process.env.MIADI_REPOSITORY_HOSTS?.trim() || undefined) {
44
+ const texts = [];
45
+ const unread = [];
46
+ if (path) {
47
+ try {
48
+ texts.push({ text: (0, node_fs_1.readFileSync)(path, "utf8"), source: path });
49
+ }
50
+ catch (error) {
51
+ unread.push({ severity: "error", file: path, message: `hosts not read: ${error.message}` });
52
+ }
53
+ }
54
+ texts.push({ text: shipped_hosts_1.SHIPPED_HOSTS.map((r) => JSON.stringify(r)).join("\n"), source: shipped_hosts_1.SHIPPED_HOSTS_SOURCE });
55
+ const parsed = (0, hosts_1.parseWhere)(texts);
56
+ return { from: texts.map((t) => t.source), readings: parsed.readings, issues: [...unread, ...parsed.issues] };
57
+ }
58
+ /** The packages the shipped scan read, with their versions and first-party ranges. */
59
+ function readPackages() {
60
+ const packages = [];
61
+ const issues = [];
62
+ shipped_capabilities_1.SHIPPED_PACKAGES.forEach((row, index) => {
63
+ const parsed = impact_1.PackageReadingSchema.safeParse(row);
64
+ if (parsed.success)
65
+ packages.push(parsed.data);
66
+ else
67
+ issues.push({ severity: "error", file: shipped_capabilities_1.SHIPPED_CAPABILITIES_SOURCE, line: index + 1, message: parsed.error.issues.map((i) => i.message).join("; ") });
68
+ });
69
+ return { packages, issues };
70
+ }
71
+ /** What bumping a package touches: what it consumes, who consumes it, which ranges stop reaching the new version. */
72
+ function repositoryImpact(packageOrCapability, bump) {
73
+ const read = readPackages();
74
+ const impact = (0, impact_1.impactOf)(packageOrCapability, read.packages, bump);
75
+ return { scans: shipped_capabilities_1.SHIPPED_CAPABILITIES_SCANS, issues: read.issues, ...(impact ? { impact } : {}) };
76
+ }
77
+ /**
78
+ * The capabilities: a host's own JSONL first (`--capabilities` or
79
+ * `MIADI_REPOSITORY_CAPABILITIES`), then the rows this package ships, scanned
80
+ * from the repositories' default branches. The first source to name a
81
+ * capability speaks for it; a later row on the same id is reported and skipped.
82
+ */
83
+ function readCapabilities(path = process.env.MIADI_REPOSITORY_CAPABILITIES?.trim() || undefined) {
84
+ const issues = [];
85
+ const sources = [];
86
+ if (path) {
87
+ try {
88
+ const parsed = (0, developer_repository_mapping_1.parseJsonl)((0, node_fs_1.readFileSync)(path, "utf8"), capabilities_1.CapabilityFacetSchema, path);
89
+ issues.push(...parsed.issues);
90
+ sources.push({ rows: parsed.rows, source: path });
91
+ }
92
+ catch (error) {
93
+ issues.push({ severity: "error", file: path, message: `capabilities not read: ${error.message}` });
94
+ }
95
+ }
96
+ sources.push({ rows: [...shipped_capabilities_1.SHIPPED_CAPABILITIES], source: shipped_capabilities_1.SHIPPED_CAPABILITIES_SOURCE });
97
+ const capabilities = [];
98
+ const seen = new Map();
99
+ for (const { rows, source } of sources) {
100
+ rows.forEach((row, index) => {
101
+ const parsed = capabilities_1.CapabilityFacetSchema.safeParse(row);
102
+ if (!parsed.success) {
103
+ issues.push({ severity: "error", file: source, line: index + 1, message: parsed.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ") });
104
+ return;
105
+ }
106
+ const earlier = seen.get(parsed.data.nodeId);
107
+ if (earlier) {
108
+ issues.push({ severity: "warning", file: source, nodeId: parsed.data.nodeId, message: `${parsed.data.nodeId} is already read from ${earlier}; skipped` });
109
+ return;
110
+ }
111
+ seen.set(parsed.data.nodeId, source);
112
+ capabilities.push(parsed.data);
113
+ });
114
+ }
115
+ return { from: sources.map((s) => s.source), scans: shipped_capabilities_1.SHIPPED_CAPABILITIES_SCANS, capabilities, issues };
116
+ }
117
+ /** "What does this repository provide?", or with no repository, every capability. Reads no hub and no inventory. */
118
+ function repositoryCapabilities(repository, options = {}) {
119
+ const read = readCapabilities(options.capabilities);
120
+ const id = repository ? (repository.startsWith("repository:") ? repository : `repository:${repository}`) : undefined;
121
+ const rows = id ? (0, capabilities_2.capabilitiesOf)(id, read.capabilities) : read.capabilities;
122
+ const counts = { package: 0, cli: 0, skill: 0, other: 0 };
123
+ const capabilities = rows.map((c) => {
124
+ const kind = (0, capabilities_2.kindOf)(c.nodeId);
125
+ counts[kind ?? "other"] += 1;
126
+ return kind ? { ...c, kind } : { ...c };
127
+ });
128
+ const provides = (0, capabilities_2.providesEdges)(rows).filter((e) => !id || e.from.toLowerCase() === id.toLowerCase());
129
+ return { from: read.from, scans: read.scans, issues: read.issues, ...(id ? { repository: id } : {}), counts, capabilities, provides };
130
+ }
30
131
  /**
31
132
  * The lineage: a host's own file first (`--lineage` or
32
133
  * `MIADI_REPOSITORY_LINEAGE`), then the rows this package ships. The first
@@ -70,15 +171,23 @@ async function readMapper(options = {}) {
70
171
  inventory: { from, records: sessions.records, without_repo: sessions.withoutRepo },
71
172
  sessions,
72
173
  lineage: readLineage(options.lineage),
174
+ capabilities: readCapabilities(options.capabilities),
175
+ where: readWhere(options.hosts),
73
176
  };
74
177
  }
75
178
  async function repositoryListing(options = {}) {
76
179
  const read = await readMapper(options);
77
- return { hub: read.hub, inventory: read.inventory, repositories: (0, join_1.listRepositories)(read.mapping, read.sessions, read.lineage.edges) };
180
+ const all = (0, join_1.listRepositories)(read.mapping, read.sessions, read.lineage.edges, read.capabilities.capabilities, read.where.readings);
181
+ return {
182
+ hub: read.hub,
183
+ inventory: read.inventory,
184
+ repositories: all.filter((r) => r.host !== "missing"),
185
+ missing: all.filter((r) => r.host === "missing"),
186
+ };
78
187
  }
79
188
  async function repositoryDetail(repository, options = {}) {
80
189
  const read = await readMapper(options);
81
- const view = (0, join_1.repositoryView)(repository, read.mapping, read.sessions, read.mapping?.sources, read.lineage.edges);
190
+ const view = (0, join_1.repositoryView)(repository, read.mapping, read.sessions, read.mapping?.sources, read.lineage.edges, read.capabilities.capabilities, read.where.readings);
82
191
  return { hub: read.hub, inventory: read.inventory, ...(view ? { repository: view } : {}) };
83
192
  }
84
193
  /** "What did this repository learn from?", or with no repository, every lineage. Reads no hub and no inventory. */
package/dist/scan.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import type { CapabilityFacet } from "@medicine-wheel/capabilities";
2
+ import type { PackageReading } from "./impact";
3
+ export interface ScanOptions {
4
+ /** the ref to read; default the remote's default branch (`origin/HEAD`), else HEAD */
5
+ ref?: string;
6
+ /** `owner/name` or `repository:owner/name`; default from the `origin` remote */
7
+ repository?: string;
8
+ observedBy: string;
9
+ now?: Date;
10
+ }
11
+ export interface ScanResult {
12
+ repository: string;
13
+ ref: string;
14
+ commit: string;
15
+ capabilities: CapabilityFacet[];
16
+ /** each package's version and the ranges it declares, all of them */
17
+ packages: PackageReading[];
18
+ }
19
+ export declare function scanRepository(clone: string, options: ScanOptions): Promise<ScanResult>;
package/dist/scan.js ADDED
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ // The scan, on Node: a clone's packages, CLIs and skills read at one commit,
3
+ // from git objects, never from the working tree. A clone on another branch
4
+ // or with edits of its own reads the same as a clean one, and every row names
5
+ // the commit it was read at. Read-only: git ls-tree, git show, git rev-parse.
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.scanRepository = scanRepository;
8
+ const node_child_process_1 = require("node:child_process");
9
+ const node_util_1 = require("node:util");
10
+ const developer_repository_mapping_1 = require("@medicine-wheel/developer-repository-mapping");
11
+ const capabilities_1 = require("./capabilities");
12
+ const run = (0, node_util_1.promisify)(node_child_process_1.execFile);
13
+ async function git(clone, args) {
14
+ try {
15
+ const { stdout } = await run("git", ["-C", clone, ...args], { maxBuffer: 256 * 1024 * 1024 });
16
+ return stdout;
17
+ }
18
+ catch {
19
+ return undefined;
20
+ }
21
+ }
22
+ async function scanRepository(clone, options) {
23
+ if ((await git(clone, ["rev-parse", "--git-dir"])) === undefined)
24
+ throw new Error(`not a git clone: ${clone}`);
25
+ const ref = options.ref ?? ((await git(clone, ["rev-parse", "--verify", "-q", "origin/HEAD"])) ? "origin/HEAD" : "HEAD");
26
+ const commit = (await git(clone, ["rev-parse", "--verify", `${ref}^{commit}`]))?.trim();
27
+ if (!commit)
28
+ throw new Error(`${clone}: no commit at ${ref}`);
29
+ let repository = options.repository
30
+ ? options.repository.startsWith("repository:") ? options.repository : `repository:${options.repository}`
31
+ : undefined;
32
+ if (!repository) {
33
+ const remote = (0, developer_repository_mapping_1.parseGithubRemote)((await git(clone, ["remote", "get-url", "origin"]))?.trim() ?? "");
34
+ if (!remote)
35
+ throw new Error(`${clone}: origin is not on GitHub; pass --repository owner/name`);
36
+ repository = (0, developer_repository_mapping_1.repositoryNodeId)(remote.owner, remote.name);
37
+ }
38
+ const files = ((await git(clone, ["ls-tree", "-r", "--name-only", commit])) ?? "").split("\n").filter(Boolean);
39
+ const show = (path) => git(clone, ["show", `${commit}:${path}`]);
40
+ const globs = (0, capabilities_1.workspaceGlobs)(await show("package.json"), files.includes("pnpm-workspace.yaml") ? await show("pnpm-workspace.yaml") : undefined);
41
+ const artefacts = [];
42
+ const packages = [];
43
+ for (const manifest of (0, capabilities_1.packageManifests)(globs, files)) {
44
+ const pkg = (0, capabilities_1.parsePackageJson)((await show(manifest)) ?? "");
45
+ if (!pkg.name)
46
+ continue;
47
+ packages.push({
48
+ capability: (0, capabilities_1.capabilityId)("package", pkg.name),
49
+ name: pkg.name,
50
+ ...(pkg.version ? { version: pkg.version } : {}),
51
+ repository,
52
+ path: manifest,
53
+ commit: commit.slice(0, 12),
54
+ dependsOn: pkg.dependsOn,
55
+ });
56
+ artefacts.push({ kind: "package", name: pkg.name, ...(pkg.description ? { description: pkg.description } : {}), path: manifest });
57
+ for (const bin of pkg.bins) {
58
+ artefacts.push({ kind: "cli", name: bin, description: `The ${bin} command, shipped by ${pkg.name}.`, path: manifest });
59
+ }
60
+ }
61
+ for (const file of (0, capabilities_1.skillFiles)(files)) {
62
+ const front = (0, capabilities_1.parseSkillFrontMatter)((await show(file)) ?? "");
63
+ const name = front.name ?? file.split("/").slice(-2, -1)[0];
64
+ if (!name)
65
+ continue;
66
+ artefacts.push({ kind: "skill", name, ...(front.description ? { description: front.description } : {}), path: file });
67
+ }
68
+ const observedAt = (options.now ?? new Date()).toISOString();
69
+ return {
70
+ repository,
71
+ ref,
72
+ commit,
73
+ capabilities: (0, capabilities_1.capabilityRows)(artefacts, { repository, commit, observedBy: options.observedBy, observedAt }),
74
+ packages,
75
+ };
76
+ }
@@ -0,0 +1,8 @@
1
+ export declare const SHIPPED_CAPABILITIES_SOURCE = "@miadi/relational-git-repository-mapper (shipped capabilities)";
2
+ export declare const SHIPPED_CAPABILITIES_SCANS: readonly {
3
+ repository: string;
4
+ commit: string;
5
+ observedAt: string;
6
+ }[];
7
+ export declare const SHIPPED_CAPABILITIES: readonly unknown[];
8
+ export declare const SHIPPED_PACKAGES: readonly unknown[];