#!/usr/bin/env node // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial /** * SessionStart hook that lists each workspace and its export subpaths. * * The listing omits signatures to stay small. * `mwdev_symbol` answers the details. * * The hook swallows every error so a failure never blocks a session from starting. */ import { readPackageJSON } from "@mailwoman/core/module/resolve-from" import { repoRootPath } from "@mailwoman/core/paths" import { readWorkspaceDirectories } from "@mailwoman/core/repo/workspaces" import { stringifyJSON } from "@mailwoman/core/values/json" import { type PathBuilderLike, resolvePath } from "path-ts" import { Globerator } from "spliterator/node/fs" /** * The number of subpaths listed per workspace. * The listing counts the rest. */ const SUBPATH_LIMIT = 11 const LIB_PATTERN_TARGET = "./lib/*.ts" const LISTED_MODULE = /^(?!.*\.(?:test|d)\.ts$).+\.ts$/u /** * Returns the subpaths in a manifest's `exports` map, excluding `./package.json`. * * A `"./*"` pattern over `lib/` is expanded into the modules it resolves, * shallowest first, after the explicit keys. * Any other pattern is left out because its matches cannot be listed from the key. */ async function exportedSubpaths(packageDirectory: PathBuilderLike, exports: unknown): Promise { if (typeof exports !== "object" || !exports) return [] const map = exports as Record const explicit = Object.keys(map).filter((subpath) => subpath !== "./package.json" && !subpath.includes("*")) const pattern = map["./*"] const target = typeof pattern === "object" && pattern ? (pattern as { node?: unknown }).node : pattern if (target !== LIB_PATTERN_TARGET) return explicit const modules = ( await Globerator.from("**/*.ts", { cwd: resolvePath(packageDirectory, "lib"), onlyFiles: true }).toArray() ) .map((file) => file.toString()) .filter((file) => LISTED_MODULE.test(file) && file !== "index.ts") .map((file) => `./${file.replace(/\.ts$/u, "")}`) .toSorted((a, b) => a.split("/").length - b.split("/").length || a.localeCompare(b)) return [...new Set([...explicit, ...modules])] } /** * Builds the listing, with one line per workspace giving its package name and export subpaths. * * Returns an empty string when no workspace manifest has a `name` field. */ export async function orientationListing(repoRoot: PathBuilderLike): Promise { const lines: string[] = [] for (const directory of await readWorkspaceDirectories(repoRoot)) { const manifest = await readPackageJSON(resolvePath(repoRoot, directory, "package.json")).catch(() => null) if (!manifest?.name) continue const subpaths = await exportedSubpaths(resolvePath(repoRoot, directory), manifest.exports) const shown = subpaths.slice(0, SUBPATH_LIMIT) const rest = subpaths.length - shown.length const scope = manifest.private ? " (private)" : "" lines.push(`${manifest.name}${scope}: ${shown.length ? shown.join(" ") : "—"}${rest > 0 ? ` +${rest} more` : ""}`) } if (!lines.length) return "" return [ `The ${lines.length} workspaces in this repository and the subpaths each one exports. This says where to look, ` + "not what is there: ask `mwdev_symbol` with `query` for a name fragment, or with `describes` for what a thing " + "does, before writing a helper.", "", ...lines, ].join("\n") } async function main(): Promise { const additionalContext = await orientationListing(repoRootPath()) if (!additionalContext) return process.stdout.write(stringifyJSON({ hookSpecificOutput: { hookEventName: "SessionStart", additionalContext } })) } try { await main() } catch { // A session without orientation is better than one that fails to start. }