UNPKG

2.96 kBPlain TextView Raw
1import { performance } from 'perf_hooks';
2import { getPackageManifest } from 'query-registry';
3import { RegistryPackageInfo } from '../types/registry-package-info';
4import { log } from '../utils/log';
5import { tryGetPackageAPI } from './try-get-package-api';
6
7/**
8 * `analyzeRegistryPackage` analyzes a package hosted on a registry and
9 * tries to extract its public API.
10 *
11 * @example
12 * Analyze the latest version of package `query-registry` from the npm registry:
13 *
14 * ```typescript
15 * import { analyzeRegistryPackage } from '@jsdocs-io/extractor';
16 *
17 * (async () => {
18 * const info = await analyzeRegistryPackage({ name: 'query-registry' });
19 *
20 * // Output: 'query-registry'
21 * console.log(info.manifest.name);
22 *
23 * // Output: 'string'
24 * console.log(typeof info.api?.overview);
25 * })();
26 * ```
27 *
28 * @param name - package name
29 * @param version - package version (default: `latest`)
30 * @param registry - registry URL (default: npm registry)
31 * @param mirrors - URLs of registry mirrors (default: npm registry mirrors)
32 * @param ignoreLicense - if `true`, extract API from unlicensed or proprietary packages (default: `false`)
33 * @param ignoreFilePatternOptimizations - if `true`, ignore file pattern optimizations for known npm packages (default: `false`)
34 * @param skipAPIExtraction - if `true`, do not extract the API from the package (default: `false`)
35 *
36 * @see {@link RegistryPackageInfo}
37 */
38export async function analyzeRegistryPackage({
39 name,
40 version,
41 registry,
42 mirrors,
43 ignoreLicense,
44 ignoreFilePatternOptimizations,
45 skipAPIExtraction = false,
46}: {
47 name: string;
48 version?: string;
49 registry?: string;
50 mirrors?: string[];
51 ignoreLicense?: boolean;
52 ignoreFilePatternOptimizations?: boolean;
53 skipAPIExtraction?: boolean;
54}): Promise<RegistryPackageInfo> {
55 const start = performance.now();
56 log('analyzeRegistryPackage: analyzing package: %O', { name, version });
57
58 const manifest = await getPackageManifest({
59 name,
60 version,
61 registry,
62 mirrors,
63 });
64 const { id } = manifest;
65 log('analyzeRegistryPackage: got manifest: %O', { id, manifest });
66
67 if (skipAPIExtraction) {
68 log('analyzeRegistryPackage: skipping API extraction: %O', { id });
69 return {
70 id,
71 manifest,
72 api: undefined,
73 elapsed: Math.round(performance.now() - start),
74 createdAt: new Date().toISOString(),
75 };
76 }
77
78 const api = await tryGetPackageAPI({
79 manifest,
80 ignoreLicense,
81 ignoreFilePatternOptimizations,
82 });
83 log('analyzeRegistryPackage: extracted API: %O', { id, api });
84
85 const elapsed = Math.round(performance.now() - start);
86 log('analyzeRegistryPackage: performance (ms): %O', { id, elapsed });
87
88 return {
89 id,
90 manifest,
91 api,
92 elapsed,
93 createdAt: new Date().toISOString(),
94 };
95}