@miadi/relational-git-repository-mapper 0.7.2 → 0.9.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.js CHANGED
@@ -3,14 +3,19 @@
3
3
  // the hubs it registers) and the session inventory. Read-only; nothing here
4
4
  // writes a mapping or a record.
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.stageOf = void 0;
6
+ exports.stageOf = exports.OWN_FILE_NOT_A_MAP = void 0;
7
7
  exports.inventoryDir = inventoryDir;
8
8
  exports.hubPath = hubPath;
9
+ exports.cloneRoots = cloneRoots;
10
+ exports.findRepositoryClone = findRepositoryClone;
9
11
  exports.readWhere = readWhere;
10
12
  exports.readPackages = readPackages;
11
13
  exports.repositoryImpact = repositoryImpact;
12
14
  exports.readCapabilities = readCapabilities;
15
+ exports.isRepositoryCapabilityFile = isRepositoryCapabilityFile;
13
16
  exports.repositoryCapabilities = repositoryCapabilities;
17
+ exports.readCloneCapabilities = readCloneCapabilities;
18
+ exports.repositoryCapabilitiesLive = repositoryCapabilitiesLive;
14
19
  exports.findCapability = findCapability;
15
20
  exports.readLineage = readLineage;
16
21
  exports.readHubState = readHubState;
@@ -41,6 +46,60 @@ function inventoryDir(env = process.env) {
41
46
  function hubPath(env = process.env) {
42
47
  return env.MIADI_REPOSITORY_MAPPING_HUB?.trim() || undefined;
43
48
  }
49
+ /**
50
+ * The folders that hold clones on this host, each as `<root>/<owner>/<name>` or
51
+ * `<root>/<name>`: `MIADI_REPOSITORY_CLONES`, colon-separated. Unset, the
52
+ * folders around the hub clone: `…/repos/jgwill/jgwill` sits beside its owner's
53
+ * other clones, so its parent holds `<name>` and its grandparent `<owner>/<name>`.
54
+ * With no hub either, none: a repository is then read only from a clone a person names.
55
+ */
56
+ function cloneRoots(env = process.env) {
57
+ const named = env.MIADI_REPOSITORY_CLONES?.split(":").map((root) => root.trim()).filter(Boolean);
58
+ if (named?.length)
59
+ return named;
60
+ const hub = hubPath(env);
61
+ if (!hub)
62
+ return [];
63
+ const clone = (0, node_path_1.resolve)(hub.replace(/\/\.miadi\/repository-mapping\/?$/, ""));
64
+ return [(0, node_path_1.dirname)(clone), (0, node_path_1.dirname)((0, node_path_1.dirname)(clone))];
65
+ }
66
+ /**
67
+ * The clone of a repository under the roots: the first folder that is a git
68
+ * repository whose `origin` names it, compared case-folded. A folder of the
69
+ * right name that is a clone of something else is never read as it.
70
+ */
71
+ async function findRepositoryClone(repository, roots = cloneRoots()) {
72
+ const id = (repository.startsWith("repository:") ? repository : `repository:${repository}`).toLowerCase();
73
+ const [owner, name] = id.slice("repository:".length).split("/");
74
+ if (!owner || !name)
75
+ return undefined;
76
+ for (const root of roots) {
77
+ for (const candidate of [(0, node_path_1.join)(root, owner, name), (0, node_path_1.join)(root, name)]) {
78
+ if (!(0, node_fs_1.existsSync)((0, node_path_1.join)(candidate, ".git")))
79
+ continue;
80
+ if ((await (0, node_1.originRepository)(candidate))?.toLowerCase() === id)
81
+ return candidate;
82
+ }
83
+ // Folder names keep the repository's own spelling (jgwill/Miadi): match the remote, not the case of the path.
84
+ for (const dir of [(0, node_path_1.join)(root, owner), root]) {
85
+ let entries = [];
86
+ try {
87
+ entries = (0, node_fs_1.existsSync)(dir) ? (0, node_fs_1.readdirSync)(dir) : [];
88
+ }
89
+ catch {
90
+ entries = [];
91
+ }
92
+ for (const entry of entries) {
93
+ if (entry.toLowerCase() !== name)
94
+ continue;
95
+ const candidate = (0, node_path_1.join)(dir, entry);
96
+ if ((0, node_fs_1.existsSync)((0, node_path_1.join)(candidate, ".git")) && (await (0, node_1.originRepository)(candidate))?.toLowerCase() === id)
97
+ return candidate;
98
+ }
99
+ }
100
+ }
101
+ return undefined;
102
+ }
44
103
  /** Where repositories live: a host's own readings first, then the readings this package ships. */
45
104
  function readWhere(path = process.env.MIADI_REPOSITORY_HOSTS?.trim() || undefined) {
46
105
  const texts = [];
@@ -102,9 +161,16 @@ function readCapabilities(path = process.env.MIADI_REPOSITORY_CAPABILITIES?.trim
102
161
  const sources = [];
103
162
  if (path) {
104
163
  try {
105
- const parsed = (0, developer_repository_mapping_1.parseJsonl)((0, node_fs_1.readFileSync)(path, "utf8"), capabilities_1.CapabilityFacetSchema, path);
106
- issues.push(...parsed.issues);
107
- sources.push({ rows: parsed.rows, source: path });
164
+ const text = (0, node_fs_1.readFileSync)(path, "utf8");
165
+ if (isRepositoryCapabilityFile(text)) {
166
+ // One line instead of an error on every row: the two files hold different rows.
167
+ issues.push({ severity: "error", file: path, message: exports.OWN_FILE_NOT_A_MAP });
168
+ }
169
+ else {
170
+ const parsed = (0, developer_repository_mapping_1.parseJsonl)(text, capabilities_1.CapabilityFacetSchema, path);
171
+ issues.push(...parsed.issues);
172
+ sources.push({ rows: parsed.rows, source: path });
173
+ }
108
174
  }
109
175
  catch (error) {
110
176
  issues.push({ severity: "error", file: path, message: `capabilities not read: ${error.message}` });
@@ -133,6 +199,18 @@ function readCapabilities(path = process.env.MIADI_REPOSITORY_CAPABILITIES?.trim
133
199
  }
134
200
  return { from: sources.map((s) => s.source), scans: shipped_capabilities_1.SHIPPED_CAPABILITIES_SCANS, capabilities, issues };
135
201
  }
202
+ /** What `--capabilities` and `MIADI_REPOSITORY_CAPABILITIES` are told when they are given a repository's own file. */
203
+ exports.OWN_FILE_NOT_A_MAP = `this is a repository's own ${developer_repository_mapping_1.CAPABILITY_FILE}, not a capability map. A map holds one row per capability and provider, written by "mw-repomap capabilities map <clone>... --out <file>". To read one repository from its clone: miadi-repomap capabilities <owner/name> --clone <clone>`;
204
+ /** True for the text of a repository's own capability file: its first line is that format's header. */
205
+ function isRepositoryCapabilityFile(text) {
206
+ const first = text.replace(/^\uFEFF/, "").split(/\r?\n/, 1)[0] ?? "";
207
+ try {
208
+ return JSON.parse(first)?.schema === developer_repository_mapping_1.CAPABILITY_FILE_SCHEMA;
209
+ }
210
+ catch {
211
+ return false;
212
+ }
213
+ }
136
214
  /** Where a capability stands, `unstated` when nobody has said. */
137
215
  const stageOf = (c) => c.stage?.stage ?? "unstated";
138
216
  exports.stageOf = stageOf;
@@ -141,7 +219,67 @@ exports.stageOf = stageOf;
141
219
  * With `stage`, only the capabilities at that stage: `specified` lists what is written down and not yet built.
142
220
  */
143
221
  function repositoryCapabilities(repository, options = {}) {
222
+ return answerCapabilities(readCapabilities(options.capabilities), repository, options.stage);
223
+ }
224
+ /**
225
+ * Read one repository from its clone on this host: the clone named, else the
226
+ * one found under {@link cloneRoots}. Its own committed capability file first,
227
+ * merged with what the commit's files say, at its default branch as last
228
+ * fetched. Undefined when this host holds no clone of it. A clone named that
229
+ * is another repository's is an error and is not read.
230
+ */
231
+ async function readCloneCapabilities(repository, options = {}) {
232
+ const id = repository.startsWith("repository:") ? repository : `repository:${repository}`;
233
+ const clone = options.clone ?? (await findRepositoryClone(id, options.clones ?? cloneRoots()));
234
+ if (!clone)
235
+ return undefined;
236
+ try {
237
+ const read = await (0, node_1.readRepositoryCapabilities)(clone, { ...(options.ref ? { ref: options.ref } : {}), observedBy: options.observedBy ?? "miadi-repomap" });
238
+ if (read.repository.toLowerCase() !== id.toLowerCase()) {
239
+ return { clone, error: `${clone} is a clone of ${read.repository.replace(/^repository:/, "")}, not of ${id.replace(/^repository:/, "")}` };
240
+ }
241
+ return { clone, repository: read.repository, ref: read.ref, commit: read.commit, source: read.source, capabilities: read.capabilities, issues: read.issues };
242
+ }
243
+ catch (error) {
244
+ return { clone, error: error.message };
245
+ }
246
+ }
247
+ /** A map's rows with one repository's replaced by what its clone says: the clone speaks for its repository. */
248
+ function withClone(read, reading) {
249
+ const id = reading.repository.toLowerCase();
250
+ const others = read.capabilities.filter((c) => !c.providedBy.some((p) => p.toLowerCase() === id));
251
+ const where = `${reading.clone} @ ${reading.commit.slice(0, 12)} (${reading.source === "file" ? `its own ${developer_repository_mapping_1.CAPABILITY_FILE}` : "its files; it holds no capability file"})`;
252
+ return {
253
+ ...read,
254
+ from: [where, ...read.from],
255
+ capabilities: [...reading.capabilities, ...others],
256
+ issues: [...reading.issues.map((i) => ({ ...i, file: `${reading.repository.replace(/^repository:/, "")}:${i.file ?? ""}` })), ...read.issues],
257
+ };
258
+ }
259
+ /**
260
+ * "What does this repository provide?", read from its clone on this host when
261
+ * there is one (jgwill/Miadi#1148): a repository that committed its own
262
+ * capability file is shown as that file says, with no map made by hand. With no
263
+ * clone, or no repository, the answer is {@link repositoryCapabilities}'s.
264
+ */
265
+ async function repositoryCapabilitiesLive(repository, options = {}) {
144
266
  const read = readCapabilities(options.capabilities);
267
+ if (!repository)
268
+ return answerCapabilities(read, repository, options.stage);
269
+ const reading = await readCloneCapabilities(repository, options);
270
+ if (!reading)
271
+ return answerCapabilities(read, repository, options.stage);
272
+ if ("error" in reading) {
273
+ const answer = answerCapabilities(read, repository, options.stage);
274
+ return { ...answer, issues: [{ severity: "error", file: reading.clone, message: `the clone was not read: ${reading.error}` }, ...answer.issues] };
275
+ }
276
+ return {
277
+ ...answerCapabilities(withClone(read, reading), repository, options.stage),
278
+ clone: { clone: reading.clone, ref: reading.ref, commit: reading.commit, source: reading.source },
279
+ };
280
+ }
281
+ function answerCapabilities(read, repository, stage) {
282
+ const options = { stage };
145
283
  const id = repository ? (repository.startsWith("repository:") ? repository : `repository:${repository}`) : undefined;
146
284
  const provided = id ? (0, capabilities_2.capabilitiesOf)(id, read.capabilities) : read.capabilities;
147
285
  const stages = Object.fromEntries([...capabilities_1.CAPABILITY_STAGES, "unstated"].map((k) => [k, 0]));
@@ -263,6 +401,10 @@ async function repositoryListing(options = {}) {
263
401
  }
264
402
  async function repositoryDetail(repository, options = {}) {
265
403
  const read = await readMapper(options);
404
+ // The repository asked about is read from its clone on this host when there is one: its own committed file speaks first.
405
+ const reading = options.live || options.clone ? await readCloneCapabilities(repository, options) : undefined;
406
+ if (reading && !("error" in reading))
407
+ read.capabilities = withClone(read.capabilities, reading);
266
408
  const view = (0, join_1.repositoryView)(repository, read.mapping, read.sessions, read.mapping?.sources, read.lineage.edges, read.capabilities.capabilities, read.where.readings);
267
409
  return { hub: read.hub, inventory: read.inventory, ...(view ? { repository: view } : {}) };
268
410
  }