@miadi/relational-git-repository-mapper 0.3.0 → 0.4.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,7 @@
1
- import type { MappingIssue, MergedMapping } from "@medicine-wheel/developer-repository-mapping";
1
+ import { SHIPPED_CAPABILITIES_SCANS } from "./shipped-capabilities";
2
+ import { type CapabilityFacet } from "@medicine-wheel/capabilities";
3
+ import { type CapabilityKind, type ProvidesEdge } from "./capabilities";
4
+ import { type MappingIssue, type MergedMapping } from "@medicine-wheel/developer-repository-mapping";
2
5
  import { type SessionsByRepository } from "./join";
3
6
  import { type LineageEdge, type LineageGraph, type RepositoryLineage } from "./lineage";
4
7
  import type { HubState, InventoryState, RepositoryListing, RepositoryView } from "./types";
@@ -14,7 +17,39 @@ export interface ReadOptions {
14
17
  inventory?: string;
15
18
  /** a lineage JSONL read before the shipped one; default `MIADI_REPOSITORY_LINEAGE` */
16
19
  lineage?: string;
20
+ /** a capability JSONL read before the shipped rows; default `MIADI_REPOSITORY_CAPABILITIES` */
21
+ capabilities?: string;
17
22
  }
23
+ export interface CapabilitiesRead {
24
+ /** the sources read, in precedence order */
25
+ from: string[];
26
+ /** the commits the shipped rows were scanned at */
27
+ scans: typeof SHIPPED_CAPABILITIES_SCANS;
28
+ capabilities: CapabilityFacet[];
29
+ issues: MappingIssue[];
30
+ }
31
+ /**
32
+ * The capabilities: a host's own JSONL first (`--capabilities` or
33
+ * `MIADI_REPOSITORY_CAPABILITIES`), then the rows this package ships, scanned
34
+ * from the repositories' default branches. The first source to name a
35
+ * capability speaks for it; a later row on the same id is reported and skipped.
36
+ */
37
+ export declare function readCapabilities(path?: string | undefined): CapabilitiesRead;
38
+ export interface CapabilitiesAnswer {
39
+ from: string[];
40
+ scans: typeof SHIPPED_CAPABILITIES_SCANS;
41
+ issues: MappingIssue[];
42
+ /** with a repository: the one asked about */
43
+ repository?: string;
44
+ /** how many of each kind */
45
+ counts: Record<CapabilityKind | "other", number>;
46
+ capabilities: (CapabilityFacet & {
47
+ kind?: CapabilityKind;
48
+ })[];
49
+ provides: ProvidesEdge[];
50
+ }
51
+ /** "What does this repository provide?", or with no repository, every capability. Reads no hub and no inventory. */
52
+ export declare function repositoryCapabilities(repository?: string, options?: Pick<ReadOptions, "capabilities">): CapabilitiesAnswer;
18
53
  export interface LineageRead {
19
54
  /** the files read, in precedence order */
20
55
  from: string[];
@@ -37,6 +72,7 @@ export interface MapperRead {
37
72
  inventory: InventoryState;
38
73
  sessions: SessionsByRepository;
39
74
  lineage: LineageRead;
75
+ capabilities: CapabilitiesRead;
40
76
  }
41
77
  export declare function readMapper(options?: ReadOptions): Promise<MapperRead>;
42
78
  export declare function repositoryListing(options?: ReadOptions): Promise<RepositoryListing>;
package/dist/read.js CHANGED
@@ -5,6 +5,8 @@
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.inventoryDir = inventoryDir;
7
7
  exports.hubPath = hubPath;
8
+ exports.readCapabilities = readCapabilities;
9
+ exports.repositoryCapabilities = repositoryCapabilities;
8
10
  exports.readLineage = readLineage;
9
11
  exports.readHubState = readHubState;
10
12
  exports.readMapper = readMapper;
@@ -13,9 +15,13 @@ exports.repositoryDetail = repositoryDetail;
13
15
  exports.repositoryLineage = repositoryLineage;
14
16
  const node_fs_1 = require("node:fs");
15
17
  const shipped_lineage_1 = require("./shipped-lineage");
18
+ const shipped_capabilities_1 = require("./shipped-capabilities");
19
+ const capabilities_1 = require("@medicine-wheel/capabilities");
20
+ const capabilities_2 = require("./capabilities");
16
21
  const node_os_1 = require("node:os");
17
22
  const node_path_1 = require("node:path");
18
23
  const node_1 = require("@medicine-wheel/developer-repository-mapping/node");
24
+ const developer_repository_mapping_1 = require("@medicine-wheel/developer-repository-mapping");
19
25
  const hooks_interpreter_1 = require("@miadi/hooks-interpreter");
20
26
  const join_1 = require("./join");
21
27
  const lineage_1 = require("./lineage");
@@ -27,6 +33,60 @@ function inventoryDir(env = process.env) {
27
33
  function hubPath(env = process.env) {
28
34
  return env.MIADI_REPOSITORY_MAPPING_HUB?.trim() || undefined;
29
35
  }
36
+ /**
37
+ * The capabilities: a host's own JSONL first (`--capabilities` or
38
+ * `MIADI_REPOSITORY_CAPABILITIES`), then the rows this package ships, scanned
39
+ * from the repositories' default branches. The first source to name a
40
+ * capability speaks for it; a later row on the same id is reported and skipped.
41
+ */
42
+ function readCapabilities(path = process.env.MIADI_REPOSITORY_CAPABILITIES?.trim() || undefined) {
43
+ const issues = [];
44
+ const sources = [];
45
+ if (path) {
46
+ try {
47
+ const parsed = (0, developer_repository_mapping_1.parseJsonl)((0, node_fs_1.readFileSync)(path, "utf8"), capabilities_1.CapabilityFacetSchema, path);
48
+ issues.push(...parsed.issues);
49
+ sources.push({ rows: parsed.rows, source: path });
50
+ }
51
+ catch (error) {
52
+ issues.push({ severity: "error", file: path, message: `capabilities not read: ${error.message}` });
53
+ }
54
+ }
55
+ sources.push({ rows: [...shipped_capabilities_1.SHIPPED_CAPABILITIES], source: shipped_capabilities_1.SHIPPED_CAPABILITIES_SOURCE });
56
+ const capabilities = [];
57
+ const seen = new Map();
58
+ for (const { rows, source } of sources) {
59
+ rows.forEach((row, index) => {
60
+ const parsed = capabilities_1.CapabilityFacetSchema.safeParse(row);
61
+ if (!parsed.success) {
62
+ issues.push({ severity: "error", file: source, line: index + 1, message: parsed.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ") });
63
+ return;
64
+ }
65
+ const earlier = seen.get(parsed.data.nodeId);
66
+ if (earlier) {
67
+ issues.push({ severity: "warning", file: source, nodeId: parsed.data.nodeId, message: `${parsed.data.nodeId} is already read from ${earlier}; skipped` });
68
+ return;
69
+ }
70
+ seen.set(parsed.data.nodeId, source);
71
+ capabilities.push(parsed.data);
72
+ });
73
+ }
74
+ return { from: sources.map((s) => s.source), scans: shipped_capabilities_1.SHIPPED_CAPABILITIES_SCANS, capabilities, issues };
75
+ }
76
+ /** "What does this repository provide?", or with no repository, every capability. Reads no hub and no inventory. */
77
+ function repositoryCapabilities(repository, options = {}) {
78
+ const read = readCapabilities(options.capabilities);
79
+ const id = repository ? (repository.startsWith("repository:") ? repository : `repository:${repository}`) : undefined;
80
+ const rows = id ? (0, capabilities_2.capabilitiesOf)(id, read.capabilities) : read.capabilities;
81
+ const counts = { package: 0, cli: 0, skill: 0, other: 0 };
82
+ const capabilities = rows.map((c) => {
83
+ const kind = (0, capabilities_2.kindOf)(c.nodeId);
84
+ counts[kind ?? "other"] += 1;
85
+ return kind ? { ...c, kind } : { ...c };
86
+ });
87
+ const provides = (0, capabilities_2.providesEdges)(rows).filter((e) => !id || e.from.toLowerCase() === id.toLowerCase());
88
+ return { from: read.from, scans: read.scans, issues: read.issues, ...(id ? { repository: id } : {}), counts, capabilities, provides };
89
+ }
30
90
  /**
31
91
  * The lineage: a host's own file first (`--lineage` or
32
92
  * `MIADI_REPOSITORY_LINEAGE`), then the rows this package ships. The first
@@ -70,15 +130,20 @@ async function readMapper(options = {}) {
70
130
  inventory: { from, records: sessions.records, without_repo: sessions.withoutRepo },
71
131
  sessions,
72
132
  lineage: readLineage(options.lineage),
133
+ capabilities: readCapabilities(options.capabilities),
73
134
  };
74
135
  }
75
136
  async function repositoryListing(options = {}) {
76
137
  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) };
138
+ return {
139
+ hub: read.hub,
140
+ inventory: read.inventory,
141
+ repositories: (0, join_1.listRepositories)(read.mapping, read.sessions, read.lineage.edges, read.capabilities.capabilities),
142
+ };
78
143
  }
79
144
  async function repositoryDetail(repository, options = {}) {
80
145
  const read = await readMapper(options);
81
- const view = (0, join_1.repositoryView)(repository, read.mapping, read.sessions, read.mapping?.sources, read.lineage.edges);
146
+ const view = (0, join_1.repositoryView)(repository, read.mapping, read.sessions, read.mapping?.sources, read.lineage.edges, read.capabilities.capabilities);
82
147
  return { hub: read.hub, inventory: read.inventory, ...(view ? { repository: view } : {}) };
83
148
  }
84
149
  /** "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,16 @@
1
+ import type { CapabilityFacet } from "@medicine-wheel/capabilities";
2
+ export interface ScanOptions {
3
+ /** the ref to read; default the remote's default branch (`origin/HEAD`), else HEAD */
4
+ ref?: string;
5
+ /** `owner/name` or `repository:owner/name`; default from the `origin` remote */
6
+ repository?: string;
7
+ observedBy: string;
8
+ now?: Date;
9
+ }
10
+ export interface ScanResult {
11
+ repository: string;
12
+ ref: string;
13
+ commit: string;
14
+ capabilities: CapabilityFacet[];
15
+ }
16
+ export declare function scanRepository(clone: string, options: ScanOptions): Promise<ScanResult>;
package/dist/scan.js ADDED
@@ -0,0 +1,65 @@
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
+ for (const manifest of (0, capabilities_1.packageManifests)(globs, files)) {
43
+ const pkg = (0, capabilities_1.parsePackageJson)((await show(manifest)) ?? "");
44
+ if (!pkg.name)
45
+ continue;
46
+ artefacts.push({ kind: "package", name: pkg.name, ...(pkg.description ? { description: pkg.description } : {}), path: manifest });
47
+ for (const bin of pkg.bins) {
48
+ artefacts.push({ kind: "cli", name: bin, description: `The ${bin} command, shipped by ${pkg.name}.`, path: manifest });
49
+ }
50
+ }
51
+ for (const file of (0, capabilities_1.skillFiles)(files)) {
52
+ const front = (0, capabilities_1.parseSkillFrontMatter)((await show(file)) ?? "");
53
+ const name = front.name ?? file.split("/").slice(-2, -1)[0];
54
+ if (!name)
55
+ continue;
56
+ artefacts.push({ kind: "skill", name, ...(front.description ? { description: front.description } : {}), path: file });
57
+ }
58
+ const observedAt = (options.now ?? new Date()).toISOString();
59
+ return {
60
+ repository,
61
+ ref,
62
+ commit,
63
+ capabilities: (0, capabilities_1.capabilityRows)(artefacts, { repository, commit, observedBy: options.observedBy, observedAt }),
64
+ };
65
+ }
@@ -0,0 +1,7 @@
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[];