@mark1russell7/site-pages 0.3.0 → 0.3.2

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/README.md CHANGED
@@ -362,7 +362,7 @@ The functions of the command are also in the package:
362
362
  | `versionCommitTime(repoDir, file)` | The time of the commit that set the version of a `package.json`, for the version in flight |
363
363
  | `changelogVersions(markdown)` | The versions of the headings of a changelog |
364
364
  | `readStatusBoard(options)` | The status data of a repository on disk, with GitHub |
365
- | `githubReleases`, `githubOpenWork`, `npmPublishTimes` | The data of one source. After an error, each function gives `{ available: false, reason }`. It does not throw. |
365
+ | `githubReleases`, `githubOpenWork`, `githubGreenRun`, `githubReleaseLag`, `npmPublishTimes` | The data of one source. After an error, each function gives `{ available: false, reason }`. It does not throw. |
366
366
  | `renderReleasesPage(releases, options)`, `renderStatusPage(data, options)` | The HTML of a page |
367
367
  | `timelineLayout(releases, { width })` | The positions of the marks of the timeline |
368
368
 
@@ -490,6 +490,13 @@ interface KpiFile {
490
490
  - A site with no Vite build calls `writeKpi(out, input)` from the root entry, before `site-pages build`.
491
491
  - `site-pages build` reads the file from the output folder. It adds the file to `data/status.json`, and the Status page shows the measures, the data date and the engines.
492
492
 
493
+ Two functions read a measure from GitHub at the build. Each result has the `at` of its data. Without a token, the result has only a reason, so leave the measure out.
494
+
495
+ | Function | Measure |
496
+ | --- | --- |
497
+ | `githubGreenRun({ owner, repo, token, workflow })` | The scheduled runs of a workflow that passed in a row, from the newest run |
498
+ | `githubReleaseLag({ owner, repo, token, tagPrefix, branch })` | The age in days of the oldest commit of the branch that is not in the newest release of the tag prefix. Drafts and pre-releases do not count. |
499
+
493
500
  ## The shell of a site
494
501
 
495
502
  The shell is the part of a page that is not its content. The package has two parts of the shell, and each part is optional.
@@ -426,38 +426,36 @@ async function githubOpenWork(options) {
426
426
  return failure(error, timeoutMs);
427
427
  }
428
428
  }
429
- /** This function gives the URL of the next page from a `Link` header of GitHub, or null. */
430
- function nextPage(link) {
429
+ /** This function gives the URL of the page with the relation `rel` from a `Link` header of GitHub, or null. */
430
+ function linkedPage(link, rel) {
431
431
  if (!link) return null;
432
432
  for (const part of link.split(",")) {
433
- const match = /<([^>]+)>\s*;\s*rel="next"/.exec(part);
434
- if (match?.[1]) return match[1];
433
+ const match = /<([^>]+)>\s*;\s*rel="([a-z]+)"/.exec(part);
434
+ if (match?.[1] && match[2] === rel) return match[1];
435
435
  }
436
436
  return null;
437
437
  }
438
+ const restHeaders = (token) => ({
439
+ accept: "application/vnd.github+json",
440
+ authorization: `Bearer ${token}`,
441
+ "user-agent": USER_AGENT,
442
+ "x-github-api-version": "2022-11-28"
443
+ });
438
444
  /**
439
- * This function reads the published releases of a repository with the REST API, the newest first.
440
- * It skips the drafts. It gives `{ available: false, reason }` without a token or after an error.
441
- * It does not throw.
442
- *
443
- * @remarks The function reads 100 releases on each page, and at most `maxPages` pages (default 10).
445
+ * This function reads the published releases, the newest first, and stops after the first release for which `enough` is true.
446
+ * It reads 100 releases on each page, and at most `maxPages` pages.
444
447
  */
445
- async function githubReleases(options) {
448
+ async function readReleasePages(options, enough) {
446
449
  const stop = precheck(options);
447
450
  if (stop) return stop;
448
- const { owner, repo, token, endpoint = GITHUB_API, maxPages = 10, timeoutMs = 15e3 } = options;
451
+ const { owner, repo, token = "", endpoint = GITHUB_API, maxPages = 10, timeoutMs = 15e3 } = options;
449
452
  const fetcher = options.fetch ?? globalThis.fetch;
450
453
  const releases = [];
451
454
  let url = `${endpoint}/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/releases?per_page=100`;
452
455
  try {
453
456
  for (let page = 0; url !== null && page < maxPages; page++) {
454
457
  const response = await fetcher(url, {
455
- headers: {
456
- accept: "application/vnd.github+json",
457
- authorization: `Bearer ${token}`,
458
- "user-agent": USER_AGENT,
459
- "x-github-api-version": "2022-11-28"
460
- },
458
+ headers: restHeaders(token),
461
459
  signal: AbortSignal.timeout(timeoutMs)
462
460
  });
463
461
  if (response.status === 404) return {
@@ -477,16 +475,21 @@ async function githubReleases(options) {
477
475
  const release = object(value);
478
476
  const tag = release["tag_name"];
479
477
  if (release["draft"] === true || typeof tag !== "string") continue;
480
- releases.push({
478
+ const entry = {
481
479
  tag,
482
480
  name: text(release["name"]) || tag,
483
481
  body: text(release["body"]),
484
482
  publishedAt: text(release["published_at"]) || text(release["created_at"]),
485
483
  url: text(release["html_url"]),
486
484
  prerelease: release["prerelease"] === true
487
- });
485
+ };
486
+ releases.push(entry);
487
+ if (enough(entry)) return {
488
+ available: true,
489
+ releases
490
+ };
488
491
  }
489
- url = nextPage(response.headers.get("link"));
492
+ url = linkedPage(response.headers.get("link"), "next");
490
493
  }
491
494
  return {
492
495
  available: true,
@@ -497,6 +500,96 @@ async function githubReleases(options) {
497
500
  }
498
501
  }
499
502
  /**
503
+ * This function reads the published releases of a repository with the REST API, the newest first.
504
+ * It skips the drafts. It gives `{ available: false, reason }` without a token or after an error.
505
+ * It does not throw.
506
+ *
507
+ * @remarks The function reads 100 releases on each page, and at most `maxPages` pages (default 10).
508
+ */
509
+ async function githubReleases(options) {
510
+ return readReleasePages(options, () => false);
511
+ }
512
+ const DAY_MS$1 = 864e5;
513
+ /**
514
+ * This function gives the age in days of the oldest commit after a release, rounded to 2 decimals. It gives 0 when no commit
515
+ * follows the release.
516
+ *
517
+ * @param oldest - The commit date of the oldest commit after the release, or undefined for no commit.
518
+ * @param now - The time of the read, as an ISO date.
519
+ */
520
+ function releaseLagOf(oldest, now) {
521
+ if (oldest === void 0) return 0;
522
+ const days = (Date.parse(now) - Date.parse(oldest)) / DAY_MS$1;
523
+ return Number.isFinite(days) ? Math.max(0, Math.round(days * 100) / 100) : NaN;
524
+ }
525
+ /**
526
+ * This function reads how many days a branch is ahead of its newest release. That is the age of the oldest commit of the branch
527
+ * that is not in the newest release of the tag prefix `tagPrefix`. It skips drafts and pre-releases, because
528
+ * a pre-release is not the version that most users get. It gives `{ available: false, reason }` without a token, after an error,
529
+ * or with no such release.
530
+ *
531
+ * @remarks The function reads the releases until it finds one, at most `maxPages` pages of 100 (default 10). It then compares the
532
+ * tag with the branch, with one commit on each page. GitHub gives the commits of a comparison from the oldest, so the first page
533
+ * holds the oldest commit. A commit that is merged after the tag and before the release is published counts, because it is not
534
+ * in the release. The time of the release does not decide.
535
+ */
536
+ async function githubReleaseLag(options) {
537
+ const { owner, repo, token = "", tagPrefix, branch = "main", endpoint = GITHUB_API, timeoutMs = 15e3 } = options;
538
+ const now = options.now ?? (/* @__PURE__ */ new Date()).toISOString().replace(/\.\d+Z$/, "Z");
539
+ const wanted = (release) => !release.prerelease && release.tag.startsWith(tagPrefix);
540
+ const releases = await readReleasePages(options, wanted);
541
+ if (!releases.available) return releases;
542
+ const release = releases.releases.find(wanted);
543
+ if (release === void 0) return {
544
+ available: false,
545
+ reason: `no release has a tag that starts with ${tagPrefix}`
546
+ };
547
+ if (!Number.isFinite(Date.parse(release.publishedAt))) return {
548
+ available: false,
549
+ reason: `the release ${release.tag} has no date`
550
+ };
551
+ const fetcher = options.fetch ?? globalThis.fetch;
552
+ const read = async (url) => fetcher(url, {
553
+ headers: restHeaders(token),
554
+ signal: AbortSignal.timeout(timeoutMs)
555
+ });
556
+ const compare = `${endpoint}/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/compare/${encodeURIComponent(release.tag)}...${encodeURIComponent(branch)}?per_page=1`;
557
+ try {
558
+ const response = await read(compare);
559
+ if (response.status === 404) return {
560
+ available: false,
561
+ reason: `GitHub has no tag ${release.tag} or no branch ${branch} in ${owner}/${repo}`
562
+ };
563
+ if (!response.ok) return {
564
+ available: false,
565
+ reason: `GitHub answered with HTTP ${response.status}`
566
+ };
567
+ const commits = object(await response.json())["commits"];
568
+ if (!Array.isArray(commits)) return {
569
+ available: false,
570
+ reason: "GitHub gave no list of commits"
571
+ };
572
+ const commit = commits.length === 0 ? void 0 : text(object(object(object(commits[0])["commit"])["committer"])["date"]);
573
+ if (commit === "") return {
574
+ available: false,
575
+ reason: "the oldest commit has no date"
576
+ };
577
+ const value = releaseLagOf(commit, now);
578
+ if (!Number.isFinite(value)) return {
579
+ available: false,
580
+ reason: "the time of the read is not a date"
581
+ };
582
+ return {
583
+ available: true,
584
+ value,
585
+ at: now,
586
+ release: release.tag
587
+ };
588
+ } catch (error) {
589
+ return failure(error, timeoutMs);
590
+ }
591
+ }
592
+ /**
500
593
  * This function counts the runs that passed in a row, from the newest run. A run that has not ended does not count and does not break the row.
501
594
  * Each other conclusion breaks the row, a cancelled run too. It gives null when no run ended.
502
595
  *
@@ -2572,6 +2665,6 @@ async function runCli(argv, io) {
2572
2665
  throw error;
2573
2666
  }
2574
2667
  }
2575
- export { greenRunOf as $, STATUS_CLASSES as A, DEFAULT_CHANGELOGS as B, pageShell as C, utcDay as D, shortDate as E, plainText as F, releaseReading as G, mergeReleases as H, readStatusBoard as I, GITHUB_GRAPHQL as J, globFiles as K, roadmapItems as L, linksOf as M, markdownTables as N, OPEN_CLASSES as O, parseRoadmap as P, githubReleases as Q, splitRow as R, BASE_CSS as S, longDate as T, readChangelogs as U, ReleaseDataError as V, readReleases as W, githubGreenRun as X, checkStateOf as Y, githubOpenWork as Z, renderReleasesPage as _, runCli as a, releaseBodyEntries as at, releaseSummary as b, STATUS_SCRIPT as c, renderStatusPage as d, changelogFaults as et, sectionsOf as f, releasesLead as g, RELEASES_SCRIPT as h, parseBuildArgs as i, parseChangelog as it, buildStatusBoard as j, RoadmapParseError as k, classCounts as l, statusLead as m, UsageError as n, firstSentence as nt, TYPESCRIPT_PROBE as o, sectionMarksBreak as ot, slugOf as p, GITHUB_API as q, build as r, headingAnchor as rt, probeScript as s, HELP as t, changelogVersions as tt, openGroups as u, laneCenter as v, dayRange as w, timelineLayout as x, oldestFirst as y, statusClassOf as z };
2668
+ export { githubReleases as $, STATUS_CLASSES as A, DEFAULT_CHANGELOGS as B, pageShell as C, utcDay as D, shortDate as E, plainText as F, releaseReading as G, mergeReleases as H, readStatusBoard as I, GITHUB_GRAPHQL as J, globFiles as K, roadmapItems as L, linksOf as M, markdownTables as N, OPEN_CLASSES as O, parseRoadmap as P, githubReleaseLag as Q, splitRow as R, BASE_CSS as S, longDate as T, readChangelogs as U, ReleaseDataError as V, readReleases as W, githubGreenRun as X, checkStateOf as Y, githubOpenWork as Z, renderReleasesPage as _, runCli as a, headingAnchor as at, releaseSummary as b, STATUS_SCRIPT as c, sectionMarksBreak as ct, renderStatusPage as d, greenRunOf as et, sectionsOf as f, releasesLead as g, RELEASES_SCRIPT as h, parseBuildArgs as i, firstSentence as it, buildStatusBoard as j, RoadmapParseError as k, classCounts as l, statusLead as m, UsageError as n, changelogFaults as nt, TYPESCRIPT_PROBE as o, parseChangelog as ot, slugOf as p, GITHUB_API as q, build as r, changelogVersions as rt, probeScript as s, releaseBodyEntries as st, HELP as t, releaseLagOf as tt, openGroups as u, laneCenter as v, dayRange as w, timelineLayout as x, oldestFirst as y, statusClassOf as z };
2576
2669
 
2577
2670
  //# sourceMappingURL=build.js.map