@theholocron/holocron-plugin-fern 5.0.0-alpha.480 → 5.0.0-alpha.481

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/index.d.mts CHANGED
@@ -9,7 +9,7 @@
9
9
  * The Fern CLI token (`FERN_TOKEN`) is only required in CI (via the
10
10
  * `wiki.yml` reusable workflow) — local setup writes config files only.
11
11
  */
12
- import { AuthError, DnsRecordRequest, PluginToken, ResolveTokenInput, Wiki, WikiProvisionOpts, WikiProxyConfig } from "@theholocron/cli";
12
+ import { AuthError, DnsRecordRequest, PluginToken, ResolveTokenInput, Wiki, WikiInspection, WikiProvisionOpts, WikiProxyConfig } from "@theholocron/cli";
13
13
  /** Resolves the Fern token from `--token`, `HOLOCRON_FERN_TOKEN`, `FERN_TOKEN` or the keyring entry for `fern`, and throws `AuthError` when none is set. */
14
14
  export declare const resolveToken: import("@theholocron/cli").TokenResolver;
15
15
  /** Every token this plugin takes, for `holocron doctor secrets --verify`, with the layers that hold it. */
@@ -64,11 +64,19 @@ interface FernWikiOptions extends ResolveTokenInput {
64
64
  /** `wiki` capability for Fern: writes the Fern config and docs scaffold for a repo. */
65
65
  export declare class FernWiki implements Wiki {
66
66
  private readonly opts;
67
+ private readonly deps;
67
68
  /** Capability key; always `"wiki"`. */
68
69
  readonly key: "wiki";
69
70
  /** Provider name; always `"fern"`. */
70
71
  readonly providerName = "fern";
71
- constructor(opts: FernWikiOptions);
72
+ constructor(opts: FernWikiOptions, deps?: {
73
+ fetch?: typeof fetch;
74
+ });
75
+ /**
76
+ * Reads `fern/` and `docs/wiki` and asks the public site, sending no token (see `inspectWiki`). The instance and
77
+ * domain it expects are the ones `provision()` writes.
78
+ */
79
+ inspect(): Promise<WikiInspection>;
72
80
  provision(callOpts?: WikiProvisionOpts): Promise<string>;
73
81
  proxyConfig(): WikiProxyConfig | null;
74
82
  /**
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { AuthError, connectionProbe, createResolveToken, tokenExpectations } from "@theholocron/cli";
2
- import { mkdir, readFile, writeFile } from "node:fs/promises";
3
- import { join } from "node:path";
2
+ import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
3
+ import { join, relative, resolve } from "node:path";
4
4
  import { stringifyJson } from "@theholocron/json-utils";
5
5
  // src/probes/token/expectations.ts
6
6
  /**
@@ -23,18 +23,150 @@ const tokens = [{
23
23
  ...resolveToken.token,
24
24
  expect: EXPECTED
25
25
  }];
26
+ // src/capabilities/inspect-wiki.ts
27
+ /** How long to wait for the site before calling it unreachable. */
28
+ const SITE_TIMEOUT_MS = 8e3;
29
+ async function readText(path) {
30
+ try {
31
+ return await readFile(path, "utf8");
32
+ } catch {
33
+ return null;
34
+ }
35
+ }
36
+ /** The value of `key:` inside the `instances:` block, without quotes. */
37
+ function instanceValue(docs, key) {
38
+ const value = (docs.match(/^instances:(?:\n[ \t][^\n]*)*/m)?.[0])?.match(new RegExp(`^[ \\t-]*${key}:[ \\t]*(\\S+)`, "m"))?.[1];
39
+ return value ? value.replace(/^["']|["']$/g, "") : null;
40
+ }
41
+ /** Every Markdown file below `dir`, as a path relative to `root`; an absent directory is an empty list. */
42
+ async function markdownBelow(root, dir) {
43
+ let entries;
44
+ try {
45
+ entries = await readdir(dir, { withFileTypes: true });
46
+ } catch {
47
+ return [];
48
+ }
49
+ const found = [];
50
+ for (const entry of entries) {
51
+ const path = join(dir, entry.name);
52
+ if (entry.isDirectory()) found.push(...await markdownBelow(root, path));
53
+ else if (entry.name.endsWith(".md") || entry.name.endsWith(".mdx")) found.push(relative(root, path));
54
+ }
55
+ return found.sort();
56
+ }
57
+ /** One GET, reduced to a status or a reason. The body is read only when `keep` is set, and only on success. */
58
+ async function ask(url, doFetch, keep = false) {
59
+ try {
60
+ const response = await doFetch(`https://${url}`, {
61
+ redirect: "follow",
62
+ signal: AbortSignal.timeout(SITE_TIMEOUT_MS)
63
+ });
64
+ const text = keep && response.ok ? await response.text() : void 0;
65
+ if (text === void 0) await response.body?.cancel();
66
+ return {
67
+ probe: {
68
+ url,
69
+ status: response.status
70
+ },
71
+ ...text === void 0 ? {} : { text }
72
+ };
73
+ } catch (err) {
74
+ return { probe: {
75
+ url,
76
+ error: err instanceof Error && err.name === "TimeoutError" ? `no answer within ${SITE_TIMEOUT_MS / 1e3}s` : "could not be reached"
77
+ } };
78
+ }
79
+ }
80
+ /**
81
+ * Read the Fern wiki without changing anything: `fern/fern.config.json` and the `instances:` block of `fern/docs.yml`
82
+ * against what the config declares, the navigation's `path:` entries against the files under `docs/wiki`, and one GET
83
+ * each for the instance address, the custom domain and the sitemap. No token is sent; the sitemap's body is counted
84
+ * and dropped.
85
+ */
86
+ async function inspectWiki(input) {
87
+ const doFetch = input.fetch ?? fetch;
88
+ const fernDir = join(input.repoRoot, "fern");
89
+ const configText = await readText(join(fernDir, "fern.config.json"));
90
+ let found;
91
+ try {
92
+ const organization = (configText === null ? null : JSON.parse(configText))?.organization;
93
+ found = typeof organization === "string" ? organization : null;
94
+ } catch {
95
+ found = null;
96
+ }
97
+ const docs = await readText(join(fernDir, "docs.yml"));
98
+ const instance = docs === null ? null : {
99
+ expectedUrl: input.expectedUrl,
100
+ foundUrl: instanceValue(docs, "url"),
101
+ ...input.expectedDomain ? {
102
+ expectedDomain: input.expectedDomain,
103
+ foundDomain: instanceValue(docs, "custom-domain")
104
+ } : {}
105
+ };
106
+ let navigation = null;
107
+ if (docs !== null) {
108
+ const listed = new Set([...docs.matchAll(/^[ \t-]*path:[ \t]*(\S+)/gm)].map((m) => relative(input.repoRoot, resolve(fernDir, m[1].replace(/^["']|["']$/g, "")))));
109
+ const missing = [];
110
+ for (const path of listed) if (await readText(join(input.repoRoot, path)) === null) missing.push(path);
111
+ const onDisk = await markdownBelow(input.repoRoot, join(input.repoRoot, "docs", "wiki"));
112
+ navigation = {
113
+ listed: listed.size,
114
+ missing,
115
+ unlisted: onDisk.filter((p) => !listed.has(p))
116
+ };
117
+ }
118
+ const site = [(await ask(input.expectedUrl, doFetch)).probe];
119
+ if (input.expectedDomain) site.push((await ask(input.expectedDomain, doFetch)).probe);
120
+ const sitemap = await ask(`${(input.expectedDomain ?? input.expectedUrl).replace(/\/$/, "")}/sitemap.xml`, doFetch, true);
121
+ const published = {
122
+ ...sitemap.probe,
123
+ ...sitemap.text === void 0 ? {} : { pages: (sitemap.text.match(/<loc>/g) ?? []).length }
124
+ };
125
+ return {
126
+ organization: {
127
+ declared: input.fernOrg,
128
+ found
129
+ },
130
+ instance,
131
+ site,
132
+ navigation,
133
+ published
134
+ };
135
+ }
26
136
  // src/capabilities/wiki.ts
27
137
  /** The Fern CLI version written into `fern/fern.config.json`. */
28
138
  const FERN_VERSION = "5.114.1";
29
139
  /** `wiki` capability for Fern: writes the Fern config and docs scaffold for a repo. */
30
140
  var FernWiki = class {
31
141
  opts;
142
+ deps;
32
143
  /** Capability key; always `"wiki"`. */
33
144
  key = "wiki";
34
145
  /** Provider name; always `"fern"`. */
35
146
  providerName = "fern";
36
- constructor(opts) {
147
+ constructor(opts, deps = {}) {
37
148
  this.opts = opts;
149
+ this.deps = deps;
150
+ }
151
+ /**
152
+ * Reads `fern/` and `docs/wiki` and asks the public site, sending no token (see `inspectWiki`). The instance and
153
+ * domain it expects are the ones `provision()` writes.
154
+ */
155
+ async inspect() {
156
+ const { org, repo, domain, fernOrg } = this.opts;
157
+ const resolvedFernOrg = fernOrg ?? org ?? "holocron";
158
+ const { instanceUrl, customDomain } = expectedInstance({
159
+ fernOrg: resolvedFernOrg,
160
+ repoName: repo?.split("/").pop(),
161
+ domain
162
+ });
163
+ return inspectWiki({
164
+ repoRoot: this.opts.repoRoot ?? process.cwd(),
165
+ fernOrg: resolvedFernOrg,
166
+ expectedUrl: instanceUrl,
167
+ ...customDomain ? { expectedDomain: customDomain } : {},
168
+ ...this.deps.fetch ? { fetch: this.deps.fetch } : {}
169
+ });
38
170
  }
39
171
  async provision(callOpts) {
40
172
  const repoRoot = this.opts.repoRoot ?? process.cwd();
@@ -123,24 +255,11 @@ async function writeFernDocsYml({ repoRoot, fernOrg, repo, name, domain }) {
123
255
  const docsPath = join(fernDir, "docs.yml");
124
256
  const resolvedFernOrg = fernOrg ?? "holocron";
125
257
  const repoName = repo?.split("/").pop();
126
- let instanceUrl;
127
- let customDomain;
128
- let multiSource = false;
129
- if (domain) {
130
- const slashIdx = domain.indexOf("/");
131
- if (slashIdx !== -1) {
132
- instanceUrl = `${resolvedFernOrg}.docs.buildwithfern.com/${domain.slice(slashIdx + 1)}`;
133
- customDomain = domain;
134
- multiSource = true;
135
- } else if (repoName) {
136
- instanceUrl = `${resolvedFernOrg}.docs.buildwithfern.com/${repoName}`;
137
- customDomain = `${domain}/${repoName}`;
138
- multiSource = true;
139
- } else {
140
- instanceUrl = `${resolvedFernOrg}.docs.buildwithfern.com`;
141
- customDomain = domain;
142
- }
143
- } else instanceUrl = `${resolvedFernOrg}.docs.buildwithfern.com`;
258
+ const { instanceUrl, customDomain, multiSource } = expectedInstance({
259
+ fernOrg: resolvedFernOrg,
260
+ repoName,
261
+ domain
262
+ });
144
263
  const repoOwner = repo?.split("/")[0];
145
264
  let existing = null;
146
265
  try {
@@ -150,8 +269,7 @@ async function writeFernDocsYml({ repoRoot, fernOrg, repo, name, domain }) {
150
269
  const updated = updateInstancesBlock(existing, instanceUrl, customDomain, multiSource, repoOwner, repoName);
151
270
  if (updated !== existing) {
152
271
  await writeFile(docsPath, updated, "utf8");
153
- const domainPart = customDomain ? `, domain=${customDomain}${multiSource ? " (multi-source)" : ""}` : "";
154
- return `docs.yml: updated instances (url=${instanceUrl}${domainPart})`;
272
+ return `docs.yml: updated instances (url=${instanceUrl}${customDomain ? `, domain=${customDomain}${multiSource ? " (multi-source)" : ""}` : ""})`;
155
273
  }
156
274
  return "docs.yml: instances block already up to date";
157
275
  }
@@ -165,8 +283,35 @@ async function writeFernDocsYml({ repoRoot, fernOrg, repo, name, domain }) {
165
283
  if (multiSource) lines.push(` multi-source: true`);
166
284
  lines.push(``, `title: ${name ?? resolvedFernOrg} Engineering`, ``, `layout:`, ` page-width: full`, ` tabs-placement: header`, ` searchbar-placement: header`, ``, `tabs:`, ` decisions:`, ` display-name: Decisions`, ` icon: fa-duotone fa-scale-balanced`, ` standards:`, ` display-name: Standards`, ` icon: fa-duotone fa-book`, ` specifications:`, ` display-name: Specifications`, ` icon: fa-duotone fa-file-lines`, ``, `navigation:`, ` - tab: decisions`, ` layout:`, ` - section: Architecture Decision Records`, ` contents:`, ` - page: Index`, ` path: ../docs/wiki/decisions/README.md`, ` - tab: standards`, ` layout:`, ` - section: Standards`, ` contents:`, ` - page: Overview`, ` path: ../docs/wiki/standards/README.md`, ` - tab: specifications`, ` layout:`, ` - section: Specifications`, ` contents:`, ` - page: Overview`, ` path: ../docs/wiki/specifications/README.md`, ``, `colors:`, ` accent-primary:`, ` dark: "#70E155"`, ` light: "#008700"`, ``, `logo:`, ` height: 20`, ``, `metadata:`, ` og:dynamic: true`, ``);
167
285
  await writeFile(docsPath, lines.join("\n"), "utf8");
168
- const domainSummary = customDomain ? `, domain=${customDomain}${multiSource ? " (multi-source)" : ""}` : "";
169
- return `docs.yml: url=${instanceUrl}${domainSummary}`;
286
+ return `docs.yml: url=${instanceUrl}${customDomain ? `, domain=${customDomain}${multiSource ? " (multi-source)" : ""}` : ""}`;
287
+ }
288
+ /**
289
+ * The Fern instance URL and custom domain a docs site is declared with, and whether it is multi-source. A domain with
290
+ * no path gets the repo name as its basepath so every repo in the org shares one domain; Fern needs the instance URL
291
+ * to carry the same basepath when `multi-source` is on.
292
+ */
293
+ function expectedInstance({ fernOrg, repoName, domain }) {
294
+ if (!domain) return {
295
+ instanceUrl: `${fernOrg}.docs.buildwithfern.com`,
296
+ customDomain: void 0,
297
+ multiSource: false
298
+ };
299
+ const slashIdx = domain.indexOf("/");
300
+ if (slashIdx !== -1) return {
301
+ instanceUrl: `${fernOrg}.docs.buildwithfern.com/${domain.slice(slashIdx + 1)}`,
302
+ customDomain: domain,
303
+ multiSource: true
304
+ };
305
+ if (repoName) return {
306
+ instanceUrl: `${fernOrg}.docs.buildwithfern.com/${repoName}`,
307
+ customDomain: `${domain}/${repoName}`,
308
+ multiSource: true
309
+ };
310
+ return {
311
+ instanceUrl: `${fernOrg}.docs.buildwithfern.com`,
312
+ customDomain: domain,
313
+ multiSource: false
314
+ };
170
315
  }
171
316
  /**
172
317
  * Replace the `instances:` block in an existing docs.yml, preserving
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-fern",
3
- "version": "5.0.0-alpha.480",
3
+ "version": "5.0.0-alpha.481",
4
4
  "description": "Holocron plugin for Fern. Implements the wiki capability for engineering knowledge publishing.",
5
5
  "keywords": [
6
6
  "fern",
@@ -43,14 +43,14 @@
43
43
  "tsx": "4.23.15",
44
44
  "typescript": "^5.9.3",
45
45
  "vitest": "^5.1.0",
46
- "@theholocron/eslint-config": "^8.9.0-alpha.21",
47
- "@theholocron/cli": "5.0.0-alpha.480",
48
- "@theholocron/tsconfig": "^8.9.0-alpha.21",
49
46
  "@theholocron/tsdown-config": "^8.9.0-alpha.21",
50
- "@theholocron/vitest-config": "^8.9.0-alpha.21"
47
+ "@theholocron/vitest-config": "^8.9.0-alpha.21",
48
+ "@theholocron/cli": "5.0.0-alpha.481",
49
+ "@theholocron/tsconfig": "^8.9.0-alpha.21",
50
+ "@theholocron/eslint-config": "^8.9.0-alpha.21"
51
51
  },
52
52
  "peerDependencies": {
53
- "@theholocron/cli": "5.0.0-alpha.480"
53
+ "@theholocron/cli": "5.0.0-alpha.481"
54
54
  },
55
55
  "engines": {
56
56
  "node": ">=22"