This repository has been archived by the owner on Jun 11, 2020. It is now read-only.
/
create-search-index.ts
60 lines (54 loc) · 2.37 KB
/
create-search-index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as yargs from "yargs";
import { getDefinitelyTyped } from "./get-definitely-typed";
import { Options, writeDataFile } from "./lib/common";
import { UncachedNpmInfoClient } from "./lib/npm-client";
import { AllPackages, TypingsData } from "./lib/packages";
import { loggerWithErrors } from "./util/logging";
import { logUncaughtErrors } from "./util/util";
if (!module.parent) {
const log = loggerWithErrors()[0];
const single = yargs.argv.single as string | undefined;
if (single) {
logUncaughtErrors(doSingle(single, new UncachedNpmInfoClient()));
} else {
logUncaughtErrors(
async () => createSearchIndex(await AllPackages.read(await getDefinitelyTyped(Options.defaults, log)), new UncachedNpmInfoClient()));
}
}
export interface SearchRecord {
// types package name
readonly t: string;
// globals
readonly g: ReadonlyArray<string>;
// modules
readonly m: ReadonlyArray<string>;
// project name
readonly p: string;
// library name
readonly l: string;
// downloads in the last month from NPM
readonly d: number;
}
export default async function createSearchIndex(packages: AllPackages, client: UncachedNpmInfoClient): Promise<void> {
console.log("Generating search index...");
const records = await createSearchRecords(packages.allLatestTypings(), client);
console.log("Done generating search index. Writing out data files...");
await writeDataFile("search-index-min.json", records, false);
}
async function doSingle(name: string, client: UncachedNpmInfoClient): Promise<void> {
const pkg = await AllPackages.readSingle(name);
const record = (await createSearchRecords([pkg], client))[0];
console.log(record);
}
async function createSearchRecords(packages: ReadonlyArray<TypingsData>, client: UncachedNpmInfoClient): Promise<ReadonlyArray<SearchRecord>> {
// TODO: Would like to just use pkg.unescapedName unconditionally, but npm doesn't allow scoped packages.
const dl = await client.getDownloads(packages.map((pkg, i) => pkg.name === pkg.unescapedName ? pkg.name : `dummy${i}`));
return packages.map((pkg, i): SearchRecord => ({
p: pkg.projectName,
l: pkg.libraryName,
g: pkg.globals,
t: pkg.name,
m: pkg.declaredModules,
d: dl[i],
})).sort((a, b) => b.d - a.d);
}