@cubicecho/agent-docs 0.0.0-stage → 0.2.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.
Files changed (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +52 -2
  3. package/_agents/docs/01-install.md +55 -0
  4. package/_agents/docs/checks.md +16 -0
  5. package/_agents/docs/ci.md +71 -0
  6. package/_agents/docs/cli.md +39 -0
  7. package/_agents/docs/common-mistakes.md +54 -0
  8. package/_agents/docs/config.md +25 -0
  9. package/dist/bin.d.ts +7 -0
  10. package/dist/bin.js +11 -0
  11. package/dist/check/check-agent-docs.d.ts +23 -0
  12. package/dist/check/check-agent-docs.js +314 -0
  13. package/dist/check/generation.d.ts +37 -0
  14. package/dist/check/generation.js +86 -0
  15. package/dist/cli/run.d.ts +24 -0
  16. package/dist/cli/run.js +131 -0
  17. package/dist/consts.d.ts +16 -0
  18. package/dist/consts.js +16 -0
  19. package/dist/core/config.d.ts +31 -0
  20. package/dist/core/config.js +69 -0
  21. package/dist/core/errors.d.ts +17 -0
  22. package/dist/core/errors.js +21 -0
  23. package/dist/core/files.d.ts +28 -0
  24. package/dist/core/files.js +67 -0
  25. package/dist/core/json.d.ts +26 -0
  26. package/dist/core/json.js +44 -0
  27. package/dist/core/package-manifest.d.ts +38 -0
  28. package/dist/core/package-manifest.js +58 -0
  29. package/dist/defaults.d.ts +26 -0
  30. package/dist/defaults.js +14 -0
  31. package/dist/exports/entry-points.d.ts +30 -0
  32. package/dist/exports/entry-points.js +119 -0
  33. package/dist/exports/export-index.d.ts +44 -0
  34. package/dist/exports/export-index.js +233 -0
  35. package/dist/exports/module-source.d.ts +74 -0
  36. package/dist/exports/module-source.js +161 -0
  37. package/dist/exports/scan.d.ts +29 -0
  38. package/dist/exports/scan.js +156 -0
  39. package/dist/exports/summary.d.ts +36 -0
  40. package/dist/exports/summary.js +84 -0
  41. package/dist/index.d.ts +16 -0
  42. package/dist/index.js +16 -0
  43. package/dist/llms/identity.d.ts +21 -0
  44. package/dist/llms/identity.js +67 -0
  45. package/dist/llms/plan.d.ts +53 -0
  46. package/dist/llms/plan.js +318 -0
  47. package/dist/llms/sync.d.ts +16 -0
  48. package/dist/llms/sync.js +36 -0
  49. package/dist/markdown/anchors.d.ts +35 -0
  50. package/dist/markdown/anchors.js +66 -0
  51. package/dist/markdown/page.d.ts +20 -0
  52. package/dist/markdown/page.js +57 -0
  53. package/llms.txt +42 -0
  54. package/package.json +64 -4
@@ -0,0 +1,69 @@
1
+ import { CONFIG_FILE } from '../consts.js';
2
+ import { LAYOUT_DEFAULTS } from '../defaults.js';
3
+ import { AgentDocsError } from './errors.js';
4
+ import { isJsonObject, readJsonFile } from './json.js';
5
+ /**
6
+ * Reads `_agents/llms.json`, or the defaults when it is absent.
7
+ *
8
+ * @param root - Package root.
9
+ * @returns The config, every field checked.
10
+ * @throws {@link AgentDocsError} If the file is not JSON or a field has the wrong type.
11
+ */
12
+ export function loadLlmsConfig(root) {
13
+ const parsed = readJsonFile(root, CONFIG_FILE) ?? {};
14
+ if (isJsonObject(parsed) === false) {
15
+ throw new AgentDocsError([`${CONFIG_FILE} must hold a JSON object.`]);
16
+ }
17
+ const problems = [];
18
+ const text = (key) => {
19
+ const value = parsed[key];
20
+ if (value === undefined || typeof value === 'string') {
21
+ return value;
22
+ }
23
+ problems.push(`${CONFIG_FILE} "${key}" must be a string.`);
24
+ return undefined;
25
+ };
26
+ const config = {
27
+ title: text('title'),
28
+ summary: text('summary'),
29
+ extra: text('extra'),
30
+ docsDir: (text('docsDir') ?? LAYOUT_DEFAULTS.docsDir).replace(/^\.\//, '').replace(/\/$/, ''),
31
+ sections: sectionNames(parsed, problems),
32
+ exports: parsed.exports !== false,
33
+ };
34
+ const hasBadExports = parsed.exports !== undefined && typeof parsed.exports !== 'boolean';
35
+ if (hasBadExports) {
36
+ problems.push(`${CONFIG_FILE} "exports" must be true or false.`);
37
+ }
38
+ if (problems.length > 0) {
39
+ throw new AgentDocsError(problems);
40
+ }
41
+ return config;
42
+ }
43
+ /**
44
+ * The `sections` map of the config, keeping only string headings.
45
+ *
46
+ * @param parsed - Parsed llms.json.
47
+ * @param problems - Receives a line for each entry that is not a string; appended to in place.
48
+ * @returns Heading text per folder name.
49
+ */
50
+ function sectionNames(parsed, problems) {
51
+ const { sections } = parsed;
52
+ if (sections === undefined) {
53
+ return {};
54
+ }
55
+ if (isJsonObject(sections) === false) {
56
+ problems.push(`${CONFIG_FILE} "sections" must map folder names to heading text.`);
57
+ return {};
58
+ }
59
+ const named = {};
60
+ for (const [folder, heading] of Object.entries(sections)) {
61
+ if (typeof heading === 'string') {
62
+ named[folder] = heading;
63
+ }
64
+ else {
65
+ problems.push(`${CONFIG_FILE} "sections.${folder}" must be a string.`);
66
+ }
67
+ }
68
+ return named;
69
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The docs or the package can't be read or generated as they stand.
3
+ *
4
+ * @remarks
5
+ * Each entry in `problems` is one line that names the file and says what to change, so the CLI prints them as
6
+ * they are.
7
+ */
8
+ export declare class AgentDocsError extends Error {
9
+ /** One line per problem, each naming the file and the fix. */
10
+ readonly problems: readonly string[];
11
+ /**
12
+ * Collects the problems into one error.
13
+ *
14
+ * @param problems - One line per problem; at least one.
15
+ */
16
+ constructor(problems: readonly string[]);
17
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * The docs or the package can't be read or generated as they stand.
3
+ *
4
+ * @remarks
5
+ * Each entry in `problems` is one line that names the file and says what to change, so the CLI prints them as
6
+ * they are.
7
+ */
8
+ export class AgentDocsError extends Error {
9
+ /** One line per problem, each naming the file and the fix. */
10
+ problems;
11
+ /**
12
+ * Collects the problems into one error.
13
+ *
14
+ * @param problems - One line per problem; at least one.
15
+ */
16
+ constructor(problems) {
17
+ super(problems.join('\n'));
18
+ this.name = 'AgentDocsError';
19
+ this.problems = problems;
20
+ }
21
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Every `.md` file under a folder, in the order llms.txt lists them.
3
+ *
4
+ * @param root - Package root.
5
+ * @param dir - Folder relative to the root, with `/` separators.
6
+ * @returns Paths relative to the root, with `/` separators; empty when the folder is absent.
7
+ *
8
+ * @remarks
9
+ * Each folder's entries are sorted by name and a subfolder's files sit where its name sorts, so a `01-` prefix
10
+ * sets a page's place.
11
+ */
12
+ export declare function markdownFiles(root: string, dir: string): string[];
13
+ /**
14
+ * Orders two `/`-separated paths the way {@link markdownFiles} walks them.
15
+ *
16
+ * @param a - First path.
17
+ * @param b - Second path.
18
+ * @returns Negative when `a` comes first, positive when `b` does, 0 when equal.
19
+ */
20
+ export declare function compareWalkOrder(a: string, b: string): number;
21
+ /**
22
+ * Whether any `.ts` file sits anywhere under a folder.
23
+ *
24
+ * @param root - Package root.
25
+ * @param dir - Folder relative to the root.
26
+ * @returns false when the folder is absent.
27
+ */
28
+ export declare function hasTypeScript(root: string, dir: string): boolean;
@@ -0,0 +1,67 @@
1
+ import { existsSync, readdirSync, statSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ /**
4
+ * Every `.md` file under a folder, in the order llms.txt lists them.
5
+ *
6
+ * @param root - Package root.
7
+ * @param dir - Folder relative to the root, with `/` separators.
8
+ * @returns Paths relative to the root, with `/` separators; empty when the folder is absent.
9
+ *
10
+ * @remarks
11
+ * Each folder's entries are sorted by name and a subfolder's files sit where its name sorts, so a `01-` prefix
12
+ * sets a page's place.
13
+ */
14
+ export function markdownFiles(root, dir) {
15
+ const full = join(root, dir);
16
+ if (existsSync(full) === false) {
17
+ return [];
18
+ }
19
+ const found = [];
20
+ for (const entry of readdirSync(full).sort()) {
21
+ const path = `${dir}/${entry}`;
22
+ if (statSync(join(root, path)).isDirectory()) {
23
+ found.push(...markdownFiles(root, path));
24
+ }
25
+ else if (entry.endsWith('.md')) {
26
+ found.push(path);
27
+ }
28
+ }
29
+ return found;
30
+ }
31
+ /**
32
+ * Orders two `/`-separated paths the way {@link markdownFiles} walks them.
33
+ *
34
+ * @param a - First path.
35
+ * @param b - Second path.
36
+ * @returns Negative when `a` comes first, positive when `b` does, 0 when equal.
37
+ */
38
+ export function compareWalkOrder(a, b) {
39
+ const left = a.split('/');
40
+ const right = b.split('/');
41
+ for (let i = 0; i < Math.min(left.length, right.length); i++) {
42
+ if (left[i] !== right[i]) {
43
+ return left[i] < right[i] ? -1 : 1;
44
+ }
45
+ }
46
+ return left.length - right.length;
47
+ }
48
+ /**
49
+ * Whether any `.ts` file sits anywhere under a folder.
50
+ *
51
+ * @param root - Package root.
52
+ * @param dir - Folder relative to the root.
53
+ * @returns false when the folder is absent.
54
+ */
55
+ export function hasTypeScript(root, dir) {
56
+ const full = join(root, dir);
57
+ if (existsSync(full) === false) {
58
+ return false;
59
+ }
60
+ return readdirSync(full).some((entry) => {
61
+ const path = `${dir}/${entry}`;
62
+ if (statSync(join(root, path)).isDirectory()) {
63
+ return entry !== 'node_modules' && hasTypeScript(root, path);
64
+ }
65
+ return /\.(ts|tsx|mts)$/.test(entry) && entry.endsWith('.d.ts') === false;
66
+ });
67
+ }
@@ -0,0 +1,26 @@
1
+ /** A parsed JSON object whose fields are not yet narrowed. */
2
+ export type JsonObject = Record<string, unknown>;
3
+ /**
4
+ * Tells a JSON object from an array, null or a scalar.
5
+ *
6
+ * @param value - Any parsed JSON value.
7
+ * @returns true when `value` is a plain object.
8
+ */
9
+ export declare function isJsonObject(value: unknown): value is JsonObject;
10
+ /**
11
+ * Reads and parses a JSON file under the package root.
12
+ *
13
+ * @param root - Package root.
14
+ * @param path - File path relative to the root, with `/` separators.
15
+ * @returns The parsed value, or undefined when the file is absent.
16
+ * @throws {@link AgentDocsError} If the file is not valid JSON.
17
+ */
18
+ export declare function readJsonFile(root: string, path: string): unknown;
19
+ /**
20
+ * A string field of an object, if it holds one.
21
+ *
22
+ * @param object - Parsed JSON object.
23
+ * @param key - Field name.
24
+ * @returns The value, or undefined when the field is missing or not a string.
25
+ */
26
+ export declare function stringField(object: JsonObject, key: string): string | undefined;
@@ -0,0 +1,44 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { AgentDocsError } from './errors.js';
4
+ /**
5
+ * Tells a JSON object from an array, null or a scalar.
6
+ *
7
+ * @param value - Any parsed JSON value.
8
+ * @returns true when `value` is a plain object.
9
+ */
10
+ export function isJsonObject(value) {
11
+ return typeof value === 'object' && value !== null && Array.isArray(value) === false;
12
+ }
13
+ /**
14
+ * Reads and parses a JSON file under the package root.
15
+ *
16
+ * @param root - Package root.
17
+ * @param path - File path relative to the root, with `/` separators.
18
+ * @returns The parsed value, or undefined when the file is absent.
19
+ * @throws {@link AgentDocsError} If the file is not valid JSON.
20
+ */
21
+ export function readJsonFile(root, path) {
22
+ const full = join(root, path);
23
+ if (existsSync(full) === false) {
24
+ return undefined;
25
+ }
26
+ try {
27
+ return JSON.parse(readFileSync(full, 'utf8'));
28
+ }
29
+ catch (error) {
30
+ const reason = error instanceof Error ? error.message : String(error);
31
+ throw new AgentDocsError([`${path} is not valid JSON: ${reason}. Fix the syntax.`]);
32
+ }
33
+ }
34
+ /**
35
+ * A string field of an object, if it holds one.
36
+ *
37
+ * @param object - Parsed JSON object.
38
+ * @param key - Field name.
39
+ * @returns The value, or undefined when the field is missing or not a string.
40
+ */
41
+ export function stringField(object, key) {
42
+ const value = object[key];
43
+ return typeof value === 'string' ? value : undefined;
44
+ }
@@ -0,0 +1,38 @@
1
+ /** A peer dependency as llms.txt states it. */
2
+ export interface PeerDependency {
3
+ /** Package name. */
4
+ name: string;
5
+ /** Version range. */
6
+ range: string;
7
+ /** Whether `peerDependenciesMeta` marks it optional. */
8
+ isOptional: boolean;
9
+ }
10
+ /** The parts of package.json both commands read. */
11
+ export interface PackageManifest {
12
+ /** `name`, when it is a string. */
13
+ name?: string;
14
+ /** `description`, when it is a string. */
15
+ description?: string;
16
+ /** `private: true`: the package is never published. */
17
+ isPrivate: boolean;
18
+ /** A private package with `workspaces`: a monorepo root that publishes nothing itself. */
19
+ isWorkspaceRoot: boolean;
20
+ /** `files`, when it is a list; undefined means npm's default of everything. */
21
+ files?: string[];
22
+ /** `exports` as written; undefined when absent. */
23
+ exports: unknown;
24
+ /** `engines.node`. */
25
+ nodeRange?: string;
26
+ /** `peerDependencies`, in the order written. */
27
+ peers: PeerDependency[];
28
+ /** `scripts`, keeping only string-valued entries. */
29
+ scripts: Record<string, string>;
30
+ }
31
+ /**
32
+ * Reads package.json at the package root.
33
+ *
34
+ * @param root - Package root.
35
+ * @returns The manifest, or undefined when there is no package.json.
36
+ * @throws {@link AgentDocsError} If package.json is not valid JSON.
37
+ */
38
+ export declare function readPackageManifest(root: string): PackageManifest | undefined;
@@ -0,0 +1,58 @@
1
+ import { isJsonObject, readJsonFile, stringField } from './json.js';
2
+ /**
3
+ * Reads package.json at the package root.
4
+ *
5
+ * @param root - Package root.
6
+ * @returns The manifest, or undefined when there is no package.json.
7
+ * @throws {@link AgentDocsError} If package.json is not valid JSON.
8
+ */
9
+ export function readPackageManifest(root) {
10
+ const parsed = readJsonFile(root, 'package.json');
11
+ if (isJsonObject(parsed) === false) {
12
+ return undefined;
13
+ }
14
+ const isPrivate = parsed.private === true;
15
+ const files = Array.isArray(parsed.files) ? parsed.files.filter((entry) => typeof entry === 'string') : undefined;
16
+ const engines = isJsonObject(parsed.engines) ? parsed.engines : {};
17
+ return {
18
+ name: stringField(parsed, 'name'),
19
+ description: stringField(parsed, 'description'),
20
+ isPrivate,
21
+ isWorkspaceRoot: isPrivate && Boolean(parsed.workspaces),
22
+ files,
23
+ exports: parsed.exports,
24
+ nodeRange: stringField(engines, 'node'),
25
+ peers: peerDependencies(parsed),
26
+ scripts: stringEntries(parsed.scripts),
27
+ };
28
+ }
29
+ /**
30
+ * The peer dependencies a manifest declares.
31
+ *
32
+ * @param manifest - Parsed package.json.
33
+ * @returns One entry per string-valued `peerDependencies` field.
34
+ */
35
+ function peerDependencies(manifest) {
36
+ const peers = isJsonObject(manifest.peerDependencies) ? manifest.peerDependencies : {};
37
+ const meta = isJsonObject(manifest.peerDependenciesMeta) ? manifest.peerDependenciesMeta : {};
38
+ return Object.entries(peers).flatMap(([name, range]) => {
39
+ if (typeof range !== 'string') {
40
+ return [];
41
+ }
42
+ const entryMeta = meta[name];
43
+ const isOptional = isJsonObject(entryMeta) && entryMeta.optional === true;
44
+ return [{ name, range, isOptional }];
45
+ });
46
+ }
47
+ /**
48
+ * The string-valued fields of a JSON object, such as `scripts`.
49
+ *
50
+ * @param value - The parsed field.
51
+ * @returns Its string entries; empty when it is not an object.
52
+ */
53
+ function stringEntries(value) {
54
+ if (isJsonObject(value) === false) {
55
+ return {};
56
+ }
57
+ return Object.fromEntries(Object.entries(value).filter((entry) => typeof entry[1] === 'string'));
58
+ }
@@ -0,0 +1,26 @@
1
+ /** The size limits both commands hold the docs to. */
2
+ export interface LimitSettings {
3
+ /** Largest agent doc page, in KB: skills servers and file tools refuse or truncate larger reads. */
4
+ pageLimitKb: number;
5
+ /** Line count past which a page must open with a contents list linking each `##` heading. */
6
+ contentsAfterLines: number;
7
+ /** Largest llms.txt, in KB, before the export index moves out of it into generated pages. */
8
+ llmsLimitKb: number;
9
+ }
10
+ /** The limits the agent docs standard sets. */
11
+ export declare const LIMIT_DEFAULTS: Readonly<LimitSettings>;
12
+ /** Where the docs and the source they index are found. */
13
+ export interface LayoutSettings {
14
+ /** The agent-facing pages, relative to the package root; `docsDir` in `_agents/llms.json` replaces it. */
15
+ docsDir: string;
16
+ /** Heading of the llms.txt section that lists the pages sitting directly in the docs folder. */
17
+ topLevelSection: string;
18
+ /** Folder under the docs folder that receives the export index when llms.txt would be too large. */
19
+ exportsFolder: string;
20
+ /** Build output that `exports` targets point into. */
21
+ outDir: string;
22
+ /** Source folder an `exports` target maps back to. */
23
+ sourceDir: string;
24
+ }
25
+ /** The layout the agent docs standard and the cubicecho build use. */
26
+ export declare const LAYOUT_DEFAULTS: Readonly<LayoutSettings>;
@@ -0,0 +1,14 @@
1
+ /** The limits the agent docs standard sets. */
2
+ export const LIMIT_DEFAULTS = Object.freeze({
3
+ pageLimitKb: 25,
4
+ contentsAfterLines: 100,
5
+ llmsLimitKb: 25,
6
+ });
7
+ /** The layout the agent docs standard and the cubicecho build use. */
8
+ export const LAYOUT_DEFAULTS = Object.freeze({
9
+ docsDir: '_agents/docs',
10
+ topLevelSection: 'Docs',
11
+ exportsFolder: 'exports',
12
+ outDir: 'dist',
13
+ sourceDir: 'src',
14
+ });
@@ -0,0 +1,30 @@
1
+ /** One package.json `exports` entry, traced to the source file it is built from. */
2
+ export interface EntryPoint {
3
+ /** The subpath as written in `exports`: `.` or `./mdns`. */
4
+ subpath: string;
5
+ /** What a consumer imports: the package name plus the subpath. */
6
+ importPath: string;
7
+ /** The entry's source file, relative to the package root, with `/` separators. */
8
+ source: string;
9
+ }
10
+ /** The entry points found, and the entries that point at no source. */
11
+ export interface EntryPointScan {
12
+ /** Entries traced to a source file, in `exports` order. */
13
+ entries: EntryPoint[];
14
+ /** One line per entry whose target has no source file. */
15
+ problems: string[];
16
+ }
17
+ /**
18
+ * Maps each package.json `exports` entry back to its source file.
19
+ *
20
+ * @param root - Package root.
21
+ * @param packageName - The package's name, for the import paths.
22
+ * @param exportsField - package.json `exports` as written; undefined when absent.
23
+ * @returns The entries, and a problem for each that points into the build at a file `src/` lacks.
24
+ *
25
+ * @remarks
26
+ * A `types` or `import` target in `dist/` maps to the same path in `src/` with a `.ts` extension. Without
27
+ * `exports`, `src/index.ts` is the one entry when it exists. Wildcard subpaths and non-code targets (JSON, CSS)
28
+ * are left out, since they name no module to read.
29
+ */
30
+ export declare function findEntryPoints(root: string, packageName: string, exportsField: unknown): EntryPointScan;
@@ -0,0 +1,119 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { isJsonObject } from '../core/json.js';
4
+ import { LAYOUT_DEFAULTS } from '../defaults.js';
5
+ const { outDir, sourceDir } = LAYOUT_DEFAULTS;
6
+ /** Conditions read for an entry's file, most telling first. */
7
+ const TARGET_CONDITIONS = ['types', 'import', 'default', 'node', 'require'];
8
+ const BUILT_EXTENSION = /\.(d\.ts|d\.mts|js|mjs)$/;
9
+ const SOURCE_EXTENSION = /\.(ts|mts|tsx)$/;
10
+ const SOURCE_EXTENSIONS = ['.ts', '.mts', '.tsx'];
11
+ /**
12
+ * Maps each package.json `exports` entry back to its source file.
13
+ *
14
+ * @param root - Package root.
15
+ * @param packageName - The package's name, for the import paths.
16
+ * @param exportsField - package.json `exports` as written; undefined when absent.
17
+ * @returns The entries, and a problem for each that points into the build at a file `src/` lacks.
18
+ *
19
+ * @remarks
20
+ * A `types` or `import` target in `dist/` maps to the same path in `src/` with a `.ts` extension. Without
21
+ * `exports`, `src/index.ts` is the one entry when it exists. Wildcard subpaths and non-code targets (JSON, CSS)
22
+ * are left out, since they name no module to read.
23
+ */
24
+ export function findEntryPoints(root, packageName, exportsField) {
25
+ if (exportsField === undefined) {
26
+ const fallback = `${sourceDir}/index.ts`;
27
+ const hasFallback = existsSync(join(root, fallback));
28
+ const entries = hasFallback ? [{ subpath: '.', importPath: packageName, source: fallback }] : [];
29
+ return { entries, problems: [] };
30
+ }
31
+ const entries = [];
32
+ const problems = [];
33
+ for (const [subpath, value] of subpathEntries(exportsField)) {
34
+ const target = pickTarget(value);
35
+ const isIndexable = target !== undefined && subpath.includes('*') === false && isCodeTarget(target);
36
+ if (isIndexable === false) {
37
+ continue;
38
+ }
39
+ const importPath = subpath === '.' ? packageName : `${packageName}${subpath.slice(1)}`;
40
+ const source = sourceFor(root, target);
41
+ if (source === undefined) {
42
+ const expected = expectedSource(target);
43
+ problems.push(`package.json exports["${subpath}"] points at ${target}, but ${expected} does not exist: build it from ${sourceDir}/, or set "exports": false in _agents/llms.json.`);
44
+ continue;
45
+ }
46
+ entries.push({ subpath, importPath, source });
47
+ }
48
+ return { entries, problems };
49
+ }
50
+ /**
51
+ * The subpath map of an `exports` field, whichever of its shapes it has.
52
+ *
53
+ * @param exportsField - package.json `exports`.
54
+ * @returns `[subpath, value]` pairs; a bare string or a conditions object is the `.` entry.
55
+ */
56
+ function subpathEntries(exportsField) {
57
+ if (isJsonObject(exportsField) === false) {
58
+ return [['.', exportsField]];
59
+ }
60
+ const keys = Object.keys(exportsField);
61
+ const isSubpathMap = keys.length > 0 && keys.every((key) => key.startsWith('.'));
62
+ return isSubpathMap ? Object.entries(exportsField) : [['.', exportsField]];
63
+ }
64
+ /**
65
+ * The file an `exports` value resolves to, through nested conditions.
66
+ *
67
+ * @param value - One entry's value.
68
+ * @returns The first file path found under the preferred conditions; undefined for `null` or an empty object.
69
+ */
70
+ function pickTarget(value) {
71
+ if (typeof value === 'string') {
72
+ return value;
73
+ }
74
+ if (Array.isArray(value)) {
75
+ return value.map(pickTarget).find((target) => target !== undefined);
76
+ }
77
+ if (isJsonObject(value) === false) {
78
+ return undefined;
79
+ }
80
+ const preferred = TARGET_CONDITIONS.filter((condition) => condition in value);
81
+ const others = Object.keys(value).filter((condition) => TARGET_CONDITIONS.includes(condition) === false);
82
+ return [...preferred, ...others].map((condition) => pickTarget(value[condition])).find((t) => t !== undefined);
83
+ }
84
+ /**
85
+ * Whether a target names a module, built or source.
86
+ *
87
+ * @param target - A path from `exports`.
88
+ * @returns true for `.js`, `.mjs`, `.d.ts` and TypeScript files.
89
+ */
90
+ function isCodeTarget(target) {
91
+ return BUILT_EXTENSION.test(target) || SOURCE_EXTENSION.test(target);
92
+ }
93
+ /**
94
+ * The source path a target maps to before checking it exists.
95
+ *
96
+ * @param target - A path from `exports`.
97
+ * @returns The `src/…/x.ts` path, relative to the root.
98
+ */
99
+ function expectedSource(target) {
100
+ const path = target.replace(/^\.\//, '');
101
+ if (SOURCE_EXTENSION.test(path) && BUILT_EXTENSION.test(path) === false) {
102
+ return path;
103
+ }
104
+ const inSource = path.startsWith(`${outDir}/`) ? `${sourceDir}/${path.slice(outDir.length + 1)}` : path;
105
+ return `${inSource.replace(BUILT_EXTENSION, '')}.ts`;
106
+ }
107
+ /**
108
+ * The existing source file a target maps to.
109
+ *
110
+ * @param root - Package root.
111
+ * @param target - A path from `exports`.
112
+ * @returns The path relative to the root, or undefined when no candidate exists.
113
+ */
114
+ function sourceFor(root, target) {
115
+ const expected = expectedSource(target);
116
+ const stem = expected.replace(SOURCE_EXTENSION, '');
117
+ const candidates = [expected, ...SOURCE_EXTENSIONS.map((extension) => `${stem}${extension}`)];
118
+ return candidates.find((candidate) => existsSync(join(root, candidate)));
119
+ }
@@ -0,0 +1,44 @@
1
+ import type { EntryPoint } from './entry-points.ts';
2
+ /** One name an entry point exports, as the index lists it. */
3
+ export interface IndexedExport {
4
+ /** The name a consumer imports. */
5
+ name: string;
6
+ /** Importable as a type only. */
7
+ isType: boolean;
8
+ /** The first sentence of its doc comment; empty when it has none. */
9
+ summary: string;
10
+ }
11
+ /** Names an entry re-exports from another package, which that package documents. */
12
+ export interface ForeignReExport {
13
+ /** The package specifier. */
14
+ specifier: string;
15
+ /** The names, or `*` for `export * from`. */
16
+ names: string[];
17
+ }
18
+ /** What one entry point exports, ready to render. */
19
+ export interface EntryIndex {
20
+ /** What a consumer imports. */
21
+ importPath: string;
22
+ /** The entry's source file, relative to the package root. */
23
+ source: string;
24
+ /** The first paragraph of the entry file's own doc comment; empty when it has none. */
25
+ summary: string;
26
+ /** Its exports from this package's source, in source order. */
27
+ exports: IndexedExport[];
28
+ /** Its re-exports from other packages, one entry per package. */
29
+ foreign: ForeignReExport[];
30
+ }
31
+ /**
32
+ * Lists what each entry point exports, each name with the first sentence of its doc comment.
33
+ *
34
+ * @param root - Package root.
35
+ * @param entries - The entry points, as {@link findEntryPoints} traces them.
36
+ * @returns One index per entry, in the same order.
37
+ * @throws {@link AgentDocsError} If a re-exported module is missing or an `export` statement can't be read.
38
+ *
39
+ * @remarks
40
+ * A re-exported name is followed through `export … from`, `export *` and imports to the declaration that
41
+ * documents it. Where a name is declared twice (a vocabulary's const and its union type), the first doc comment
42
+ * wins.
43
+ */
44
+ export declare function buildExportIndex(root: string, entries: readonly EntryPoint[]): EntryIndex[];