@xh/hoist-docs-builder 0.0.0-stage → 0.0.1

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.
@@ -0,0 +1,6 @@
1
+ export declare const log: {
2
+ info: (...args: unknown[]) => void;
3
+ warn: (...args: unknown[]) => void;
4
+ error: (...args: unknown[]) => void;
5
+ debug: (...args: unknown[]) => void;
6
+ };
@@ -0,0 +1,31 @@
1
+ /*
2
+ * This file belongs to Hoist, an application development toolkit
3
+ * developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
4
+ *
5
+ * Copyright © 2026 Extremely Heavy Industries Inc.
6
+ */
7
+ /**
8
+ * Stderr-only logger for the Hoist MCP server and CLI tools.
9
+ *
10
+ * CRITICAL: All MCP server logging MUST use this utility. The stdout stream is
11
+ * reserved exclusively for JSON-RPC protocol messages -- any non-protocol output
12
+ * on stdout will corrupt the MCP communication channel.
13
+ *
14
+ * Environment variables:
15
+ * - HOIST_MCP_DEBUG: Enable verbose debug output.
16
+ * - HOIST_MCP_QUIET: Suppress info-level messages (used by CLI tools).
17
+ */
18
+ const quiet = !!process.env.HOIST_MCP_QUIET;
19
+ export const log = {
20
+ info: (...args) => {
21
+ if (!quiet)
22
+ console.error('[hoist-docs-index]', ...args);
23
+ },
24
+ warn: (...args) => console.error('[hoist-docs-index] WARN:', ...args),
25
+ error: (...args) => console.error('[hoist-docs-index] ERROR:', ...args),
26
+ debug: (...args) => {
27
+ if (process.env.HOIST_MCP_DEBUG) {
28
+ console.error('[hoist-docs-index] DEBUG:', ...args);
29
+ }
30
+ }
31
+ };
@@ -0,0 +1,103 @@
1
+ /** Options for the library being indexed. */
2
+ export interface LibraryOptions {
3
+ /** Package name its imports start with, e.g. `@xh/hoist` or `@xh/hoist-chat-kit`. */
4
+ importPrefix: string;
5
+ /** Top-level folders that hold no published source, e.g. `test`. */
6
+ excludeDirs: string[];
7
+ }
8
+ /** Set the options for the library being indexed, before building. */
9
+ export declare function setLibraryOptions(options: LibraryOptions): void;
10
+ export declare function getLibraryOptions(): LibraryOptions;
11
+ /**
12
+ * Set the hoist-react checkout to index. Must hold hoist-react's `package.json` and, for type
13
+ * resolution, its installed `node_modules`.
14
+ */
15
+ export declare function setRepoRoot(path: string): void;
16
+ /**
17
+ * The hoist-react checkout being indexed: as set by {@link setRepoRoot}, else the
18
+ * `HOIST_REACT_ROOT` env var.
19
+ */
20
+ export declare function resolveRepoRoot(): string;
21
+ /** Convert a filesystem path to POSIX (forward-slash) form. */
22
+ export declare function toPosixPath(p: string): string;
23
+ /**
24
+ * Repo root in POSIX (forward-slash) form, matching ts-morph's `getFilePath()`
25
+ * convention, which always reports forward slashes on every platform.
26
+ *
27
+ * On Windows, {@link resolveRepoRoot} returns a backslash path (e.g.
28
+ * `D:\hoist-react`) while ts-morph reports `D:/hoist-react/...`; comparing or
29
+ * slicing the two directly fails, silently filtering out every source file and
30
+ * yielding an empty symbol index. Use this whenever comparing against or
31
+ * slicing a ts-morph source-file path (see `data/ts-registry.ts` and
32
+ * `formatters/typescript.ts`). Filesystem access that stays within Node's
33
+ * `path` APIs should keep using {@link resolveRepoRoot}.
34
+ */
35
+ export declare function resolveRepoRootPosix(): string;
36
+ /**
37
+ * True if a source file is library source to index. Excludes files outside the repo root and,
38
+ * within it, dependencies, build output, this MCP sub-project, specs, and the CI reporter for
39
+ * hoist-react's own tests - none of which ship. The test kit in `test-support/` ships as
40
+ * `@xh/hoist/test-support`, so it is indexed.
41
+ *
42
+ * Tests the path relative to `repoRoot`, never the absolute path. In an app, the package root
43
+ * itself sits under `node_modules`, and a folder above it may be named `build` or `docs-index`. A test
44
+ * of the absolute path then rejects every file. The symbol indexer and the import path resolver
45
+ * both filter with this function so that they always agree.
46
+ *
47
+ * @param filePath - absolute POSIX path, as ts-morph `getFilePath()` reports it.
48
+ * @param repoRoot - repo root in POSIX form - see {@link resolveRepoRootPosix}.
49
+ */
50
+ export declare function isLibrarySource(filePath: string, repoRoot: string): boolean;
51
+ /**
52
+ * Resolve the `@xh/hoist` library version from the repo root `package.json`.
53
+ *
54
+ * Used by the connectivity-check surfaces (`hoist-ping` tool, `hoist-docs ping`
55
+ * CLI) so a sanity check also reports which hoist version is being indexed.
56
+ * The result is cached after the first call.
57
+ */
58
+ export declare function resolveHoistVersion(): string;
59
+ /** The subset of a package.json the tools read. */
60
+ export interface PackageJson {
61
+ name?: string;
62
+ version?: string;
63
+ description?: string;
64
+ dependencies?: Record<string, string>;
65
+ devDependencies?: Record<string, string>;
66
+ peerDependencies?: Record<string, string>;
67
+ hoist?: {
68
+ docRegistry?: string;
69
+ };
70
+ }
71
+ /** Read `<dir>/package.json`, or return null if it is missing or unreadable. */
72
+ export declare function readPackageJson(dir: string): PackageJson | null;
73
+ /**
74
+ * Resolve the root of the package that consumes `@xh/hoist` - the anchor for companion library
75
+ * discovery. The server cannot find companion libraries from its own location, because under
76
+ * pnpm `@xh/hoist` lives in the `.pnpm` store and does not depend on them.
77
+ *
78
+ * Checks, in order:
79
+ * 1. The `HOIST_MCP_APP_ROOT` env var, if set. Used as given.
80
+ * 2. `cwd` and `cwd/client-app` (the standard Hoist app layout), then the same pair in each
81
+ * parent folder. The first package.json that is `@xh/hoist` or depends on it wins, so a run
82
+ * from deep inside an app (e.g. `client-app/src`) still finds it.
83
+ * 3. Else the first package.json in `cwd` or `cwd/client-app`.
84
+ *
85
+ * Returns null when no package.json is found. Params exist for specs - callers pass none.
86
+ */
87
+ export declare function resolveAppRoot(cwd?: string, env?: Record<string, string | undefined>): string | null;
88
+ /**
89
+ * Find an installed package by walking up from `fromDir` through each `node_modules/<name>`,
90
+ * as Node does. Returns its `node_modules` path, which under pnpm is a link into the store, or
91
+ * null if not installed. Reads the folder directly rather than `require.resolve()`, which fails
92
+ * for packages whose `exports` map hides `package.json`.
93
+ */
94
+ export declare function findInstalledPackage(fromDir: string, name: string): string | null;
95
+ /**
96
+ * Resolve a relative path within the repo root, with traversal protection.
97
+ *
98
+ * Rejects any `relativePath` containing `..` segments and verifies that the
99
+ * resolved absolute path falls within (or equals) `repoRoot`.
100
+ *
101
+ * @throws Error if path traversal is detected.
102
+ */
103
+ export declare function resolveDocPath(repoRoot: string, relativePath: string): string;
@@ -0,0 +1,200 @@
1
+ /*
2
+ * This file belongs to Hoist, an application development toolkit
3
+ * developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
4
+ *
5
+ * Copyright © 2026 Extremely Heavy Industries Inc.
6
+ */
7
+ /**
8
+ * Path utilities for the hoist-react index builder.
9
+ *
10
+ * The builder indexes a hoist-react checkout given on the command line (see `setRepoRoot`), so it
11
+ * can index any tag. Also provides safe path construction that prevents directory traversal
12
+ * outside the checkout.
13
+ */
14
+ import { existsSync, readFileSync } from 'node:fs';
15
+ import { resolve, dirname, sep } from 'node:path';
16
+ /** Cached repo root -- resolved once and reused. */
17
+ let cachedRepoRoot;
18
+ /** Cached hoist library version -- resolved once and reused. */
19
+ let cachedHoistVersion;
20
+ /** hoist-react's, unless {@link setLibraryOptions} names another library. */
21
+ let libraryOptions = { importPrefix: '@xh/hoist', excludeDirs: [] };
22
+ /** Set the options for the library being indexed, before building. */
23
+ export function setLibraryOptions(options) {
24
+ libraryOptions = options;
25
+ }
26
+ export function getLibraryOptions() {
27
+ return libraryOptions;
28
+ }
29
+ /**
30
+ * Set the hoist-react checkout to index. Must hold hoist-react's `package.json` and, for type
31
+ * resolution, its installed `node_modules`.
32
+ */
33
+ export function setRepoRoot(path) {
34
+ const repoRoot = resolve(path);
35
+ if (!existsSync(resolve(repoRoot, 'package.json'))) {
36
+ throw new Error(`Not a library checkout: no package.json at ${repoRoot}`);
37
+ }
38
+ cachedRepoRoot = repoRoot;
39
+ }
40
+ /**
41
+ * The hoist-react checkout being indexed: as set by {@link setRepoRoot}, else the
42
+ * `HOIST_REACT_ROOT` env var.
43
+ */
44
+ export function resolveRepoRoot() {
45
+ if (!cachedRepoRoot) {
46
+ const fromEnv = process.env.HOIST_REACT_ROOT;
47
+ if (!fromEnv) {
48
+ throw new Error('No library checkout given - pass --root <dir> or set HOIST_REACT_ROOT.');
49
+ }
50
+ setRepoRoot(fromEnv);
51
+ }
52
+ return cachedRepoRoot;
53
+ }
54
+ /** Convert a filesystem path to POSIX (forward-slash) form. */
55
+ export function toPosixPath(p) {
56
+ return p.replace(/\\/g, '/');
57
+ }
58
+ /**
59
+ * Repo root in POSIX (forward-slash) form, matching ts-morph's `getFilePath()`
60
+ * convention, which always reports forward slashes on every platform.
61
+ *
62
+ * On Windows, {@link resolveRepoRoot} returns a backslash path (e.g.
63
+ * `D:\hoist-react`) while ts-morph reports `D:/hoist-react/...`; comparing or
64
+ * slicing the two directly fails, silently filtering out every source file and
65
+ * yielding an empty symbol index. Use this whenever comparing against or
66
+ * slicing a ts-morph source-file path (see `data/ts-registry.ts` and
67
+ * `formatters/typescript.ts`). Filesystem access that stays within Node's
68
+ * `path` APIs should keep using {@link resolveRepoRoot}.
69
+ */
70
+ export function resolveRepoRootPosix() {
71
+ return toPosixPath(resolveRepoRoot());
72
+ }
73
+ /**
74
+ * True if a source file is library source to index. Excludes files outside the repo root and,
75
+ * within it, dependencies, build output, this MCP sub-project, specs, and the CI reporter for
76
+ * hoist-react's own tests - none of which ship. The test kit in `test-support/` ships as
77
+ * `@xh/hoist/test-support`, so it is indexed.
78
+ *
79
+ * Tests the path relative to `repoRoot`, never the absolute path. In an app, the package root
80
+ * itself sits under `node_modules`, and a folder above it may be named `build` or `docs-index`. A test
81
+ * of the absolute path then rejects every file. The symbol indexer and the import path resolver
82
+ * both filter with this function so that they always agree.
83
+ *
84
+ * @param filePath - absolute POSIX path, as ts-morph `getFilePath()` reports it.
85
+ * @param repoRoot - repo root in POSIX form - see {@link resolveRepoRootPosix}.
86
+ */
87
+ export function isLibrarySource(filePath, repoRoot) {
88
+ if (!filePath.startsWith(repoRoot + '/'))
89
+ return false;
90
+ const relPath = filePath.slice(repoRoot.length);
91
+ return (!relPath.startsWith('/node_modules/') &&
92
+ !relPath.includes('/build/') &&
93
+ !relPath.includes('/docs-index/') &&
94
+ // The stdio MCP server that releases before the move to Hoist Docs carried.
95
+ !relPath.includes('/mcp/') &&
96
+ !relPath.startsWith('/test-support/report/') &&
97
+ !relPath.endsWith('.spec.ts') &&
98
+ !libraryOptions.excludeDirs.some(dir => relPath.startsWith(`/${dir}/`)));
99
+ }
100
+ /**
101
+ * Resolve the `@xh/hoist` library version from the repo root `package.json`.
102
+ *
103
+ * Used by the connectivity-check surfaces (`hoist-ping` tool, `hoist-docs ping`
104
+ * CLI) so a sanity check also reports which hoist version is being indexed.
105
+ * The result is cached after the first call.
106
+ */
107
+ export function resolveHoistVersion() {
108
+ if (cachedHoistVersion)
109
+ return cachedHoistVersion;
110
+ const pkgPath = resolve(resolveRepoRoot(), 'package.json');
111
+ const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
112
+ cachedHoistVersion = pkg.version ?? 'unknown';
113
+ return cachedHoistVersion;
114
+ }
115
+ /** Read `<dir>/package.json`, or return null if it is missing or unreadable. */
116
+ export function readPackageJson(dir) {
117
+ const path = resolve(dir, 'package.json');
118
+ if (!existsSync(path))
119
+ return null;
120
+ try {
121
+ return JSON.parse(readFileSync(path, 'utf8'));
122
+ }
123
+ catch {
124
+ return null;
125
+ }
126
+ }
127
+ /**
128
+ * Resolve the root of the package that consumes `@xh/hoist` - the anchor for companion library
129
+ * discovery. The server cannot find companion libraries from its own location, because under
130
+ * pnpm `@xh/hoist` lives in the `.pnpm` store and does not depend on them.
131
+ *
132
+ * Checks, in order:
133
+ * 1. The `HOIST_MCP_APP_ROOT` env var, if set. Used as given.
134
+ * 2. `cwd` and `cwd/client-app` (the standard Hoist app layout), then the same pair in each
135
+ * parent folder. The first package.json that is `@xh/hoist` or depends on it wins, so a run
136
+ * from deep inside an app (e.g. `client-app/src`) still finds it.
137
+ * 3. Else the first package.json in `cwd` or `cwd/client-app`.
138
+ *
139
+ * Returns null when no package.json is found. Params exist for specs - callers pass none.
140
+ */
141
+ export function resolveAppRoot(cwd = process.cwd(), env = process.env) {
142
+ if (env.HOIST_MCP_APP_ROOT)
143
+ return resolve(cwd, env.HOIST_MCP_APP_ROOT);
144
+ const start = resolve(cwd);
145
+ let fallback = null;
146
+ for (let dir = start;; dir = dirname(dir)) {
147
+ for (const candidate of [dir, resolve(dir, 'client-app')]) {
148
+ const pkg = readPackageJson(candidate);
149
+ if (!pkg)
150
+ continue;
151
+ if (usesHoist(pkg))
152
+ return candidate;
153
+ if (dir === start)
154
+ fallback ??= candidate;
155
+ }
156
+ if (dirname(dir) === dir)
157
+ return fallback;
158
+ }
159
+ }
160
+ /** True if `pkg` is `@xh/hoist` or depends on it. */
161
+ function usesHoist(pkg) {
162
+ const deps = { ...pkg.dependencies, ...pkg.devDependencies, ...pkg.peerDependencies };
163
+ return pkg.name === '@xh/hoist' || '@xh/hoist' in deps;
164
+ }
165
+ /**
166
+ * Find an installed package by walking up from `fromDir` through each `node_modules/<name>`,
167
+ * as Node does. Returns its `node_modules` path, which under pnpm is a link into the store, or
168
+ * null if not installed. Reads the folder directly rather than `require.resolve()`, which fails
169
+ * for packages whose `exports` map hides `package.json`.
170
+ */
171
+ export function findInstalledPackage(fromDir, name) {
172
+ let dir = resolve(fromDir);
173
+ while (true) {
174
+ const candidate = resolve(dir, 'node_modules', name);
175
+ if (existsSync(resolve(candidate, 'package.json')))
176
+ return candidate;
177
+ const parent = dirname(dir);
178
+ if (parent === dir)
179
+ return null;
180
+ dir = parent;
181
+ }
182
+ }
183
+ /**
184
+ * Resolve a relative path within the repo root, with traversal protection.
185
+ *
186
+ * Rejects any `relativePath` containing `..` segments and verifies that the
187
+ * resolved absolute path falls within (or equals) `repoRoot`.
188
+ *
189
+ * @throws Error if path traversal is detected.
190
+ */
191
+ export function resolveDocPath(repoRoot, relativePath) {
192
+ if (relativePath.includes('..')) {
193
+ throw new Error(`Path traversal detected: "${relativePath}" contains '..' segments`);
194
+ }
195
+ const resolved = resolve(repoRoot, relativePath);
196
+ if (resolved !== repoRoot && !resolved.startsWith(repoRoot + sep)) {
197
+ throw new Error(`Path traversal detected: "${relativePath}" resolves outside repo root`);
198
+ }
199
+ return resolved;
200
+ }
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "@xh/hoist-docs-builder",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "description": "Builds the Hoist Docs index for a TypeScript library, to ship inside its npm package",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/xh/hoist-docs.git",
10
+ "directory": "builders/react"
11
+ },
12
+ "engines": {
13
+ "node": ">=24"
14
+ },
15
+ "bin": {
16
+ "hoist-docs-build": "bin/hoist-docs-build.mjs"
17
+ },
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/cli/build-index.d.ts",
21
+ "default": "./dist/cli/build-index.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "bin",
26
+ "dist",
27
+ "!dist/**/*.spec.*"
28
+ ],
29
+ "scripts": {
30
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
31
+ "prepack": "pnpm build"
32
+ },
33
+ "dependencies": {
34
+ "ts-morph": "^28.0.0"
35
+ }
36
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.