shelving
Version:
Toolkit for using data in JavaScript.
44 lines (43 loc) • 2.37 kB
TypeScript
import { type DirectoryElement } from "../util/element.js";
import { type AbsolutePath, type Path } from "../util/path.js";
import { Extractor } from "./Extractor.js";
import { ModuleExtractor } from "./ModuleExtractor.js";
/** Options for a `PackageExtractor`. */
export interface PackageExtractorOptions {
/**
* Pre-extracted source tree the `package.json` exports resolve against — typically the result of
* `IndexFileExtractor(MergingExtractor(DirectoryExtractor()))` over the source root.
*/
readonly tree: DirectoryElement;
/**
* Source-file extensions tried when resolving an export's last segment to a source file (e.g. `./util/string` → `string.ts`, `string.tsx`, …).
* - Checked in declaration order; first match wins.
* - Defaults to `["ts", "tsx", "js", "jsx"]`.
*/
readonly extensions?: readonly string[];
/** `ModuleExtractor` used to build each module element. Defaults to a fresh `new ModuleExtractor()`. */
readonly module?: ModuleExtractor;
/** Absolute base path used to resolve a relative `package.json` path passed to `extract()`. */
readonly base?: AbsolutePath;
}
/**
* Extractor that reads a `package.json` and produces a flat tree of modules — one `kind: "module"`
* `DocumentationElement` per export entry, in declaration order.
* - Static export keys (e.g. `"./api"`, `"./firestore/client"`) become one module each.
* - Wildcard export keys (e.g. `"./util/*"`) expand against the source tree — one module per matching child file (with
* an extension in `extensions`) or subdirectory.
* - The `"."` root export is skipped — its content is the root tree element itself.
* - Throws if a static export key has no matching source element in the tree.
*/
export declare class PackageExtractor extends Extractor<Path, DirectoryElement> {
private readonly _tree;
private readonly _extensions;
private readonly _module;
private readonly _base;
constructor({ tree, extensions, module, base }: PackageExtractorOptions);
extract(packageJson: Path): Promise<DirectoryElement>;
/** Resolve a static export subpath (e.g. `"firestore/client"`) to a file or directory element in the tree. */
private _resolve;
/** Expand a wildcard export subpath (e.g. `"util/*"`) into one module per matching child. */
private _expandWildcard;
}