Skip to content

Commit

Permalink
feat: add and return PackageApi
Browse files Browse the repository at this point in the history
  • Loading branch information
velut committed Feb 7, 2024
1 parent 5fc9a53 commit 0ec8791
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 47 deletions.
101 changes: 54 additions & 47 deletions src/extract-package-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ResultAsync, ok, okAsync } from "neverthrow";
import { join } from "pathe";
import { createProject } from "./create-project";
import type { ExtractorError } from "./errors";
import type { ExtractedDeclaration } from "./extract-declarations";
import { installPackage } from "./install-package";
import { packageDeclarations } from "./package-declarations";
import { packageJson } from "./package-json";
Expand All @@ -17,11 +18,52 @@ export type ExtractPackageApiOptions = {
maxDepth?: number;
};

/**
PackageApi` contains all the information extracted from a package.
*/
export type PackageApi = {
/** Package name (e.g., `foo`). */
name: string;

/** Package version number (e.g., `1.0.0`). */
version: string;

/**
Package subpath selected when extracting the API (e.g., `.`, `custom`).
@see {@link https://nodejs.org/api/packages.html#subpath-exports | Node.js docs}
*/
subpath: string;

/**
Type declarations file, resolved from the selected subpath,
that acts as the entrypoint for the package (e.g., `index.d.ts`).
*/
types: string;

/**
Package description extracted from the `types` file if a
JSDoc comment with the `@packageDocumentation` tag is found.
*/
overview: string | undefined;

/** Declarations exported (or re-exported) by the package. */
declarations: ExtractedDeclaration[];

/**
All packages resolved and installed when installing the package (included).
@example
```ts
["[email protected]", "[email protected]", "[email protected]"]
```
*/
packages: string[];
};

export const extractPackageApi = ({
pkg,
pkgSubpath = ".",
maxDepth = 5,
}: ExtractPackageApiOptions): ResultAsync<unknown, ExtractorError> =>
}: ExtractPackageApiOptions): ResultAsync<PackageApi, ExtractorError> =>
okAsync({ pkg, pkgSubpath, maxDepth })
.andThen((ctx) =>
packageName(ctx.pkg).map((pkgName) => ({
Expand Down Expand Up @@ -94,54 +136,19 @@ export const extractPackageApi = ({
pkgDeclarations,
})),
)
.andThen((ctx) => {
// Debug
const sourceFiles = ctx.sourceFiles
.map((sf) => sf.getFilePath().replace(ctx.nodeModulesDir, ""))
.sort();
const indexFile = ctx.indexFile;
const referencedFiles = indexFile
.getReferencedSourceFiles()
.map((sf) => sf.getFilePath().replace(ctx.nodeModulesDir, ""))
.sort();
console.log(
JSON.stringify(
{
installedPackages: ctx.installedPackages,
indexFile: indexFile.getFilePath().replace(ctx.nodeModulesDir, ""),
sourceFiles,
referencedFiles,
pkgOverview: ctx.pkgOverview,
},
null,
2,
),
);
for (const [name, declarations] of indexFile.getExportedDeclarations()) {
console.log(
`${name}: ${declarations
.map(
(d) =>
`https://unpkg.com/browse/${d
.getSourceFile()
.getFilePath()
.replace(ctx.nodeModulesDir, "")
.replace("/", "")}#L${d.getStartLineNumber()}`,
)
.join(", ")}`,
);
}
return okAsync(ctx);
})
.andThen((ctx) =>
changeDir(ctx.startDir).map(() => ({
...ctx,
})),
)
.andThen((ctx) =>
ok({
name: ctx.pkgJson.name,
version: ctx.pkgJson.version,
subpath: ctx.pkgSubpath,
types: ctx.pkgTypes,
overview: ctx.pkgOverview,
declarations: ctx.pkgDeclarations,
packages: ctx.installedPackages,
}),
);

// await extractApiFromPackage({ pkg: "query-registry" });
// await extractApiFromPackage({ pkg: "preact", pkgSubpath: "hooks" });
// await extractApiFromPackage({ pkg: "exome", pkgSubpath: "vue" });
// await extractApiFromPackage({ pkg: "highlight-words" });
// await extractApiFromPackage({ pkg: "short-time-ago" });
// await extractApiFromPackage({ pkg: "short-time-ago", pkgSubpath: "foo" }); // Expected Error
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export { type ExtractedNamespace } from "./extract-namespace";
export {
extractPackageApi,
type ExtractPackageApiOptions,
type PackageApi,
} from "./extract-package-api";
export { type ExtractedTypeAlias } from "./extract-type-alias";
export { type ExtractedVariable } from "./extract-variable";
Expand Down

0 comments on commit 0ec8791

Please sign in to comment.