Skip to content

Commit de3eb1c

Browse files
committed
Revert "Update scripts/search/ci-update-search-index.ts for TypeScript (#14055)"
This reverts commit 180bf4a.
1 parent 6f67d72 commit de3eb1c

File tree

5 files changed

+107
-20
lines changed

5 files changed

+107
-20
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"lint-markdown": "tsx ./scripts/lint/lint-markdown.ts",
99
"build-search-index": "tsx ./scripts/search/main.ts",
1010
"check-urls": "tsx ./scripts/search/check-urls.ts",
11-
"update-search-index": "tsx ./scripts/search/update-search-index.ts",
1211
"prepare": "husky"
1312
},
1413
"type": "module",

scripts/ci-update-search-index.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
# This script downloads the search indexes for registry and docs from pulumi.com then combines
44
# the two indexes and pushes them to Algolia.
5-
yarn run update-search-index "$1"
5+
node ./scripts/search/update-search-index.cjs "$1"

scripts/search/registry.ts

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const page = require("./page");
4+
5+
module.exports = {
6+
7+
// Fetch a set of objects representing all Registry resources.
8+
getObjects(pathToRegistryJSON, hugoPageItems) {
9+
const registryPackageNames = fs.readdirSync(pathToRegistryJSON);
10+
const registryItems = [];
11+
12+
registryPackageNames
13+
.map(providerJSON => {
14+
const providerName = providerJSON.replace(".json", "");
15+
const providerResults = [];
16+
17+
// API docs JSON doesn't contain proper provider/package names, so we obtain those
18+
// from the Hugo-generated set.
19+
const providerTitle = hugoPageItems.find(o => {
20+
return o.href === `/registry/packages/${providerName}/`;
21+
}).title;
22+
23+
// Read the API docs JSON file.
24+
const providerItems = JSON.parse(
25+
fs.readFileSync(path.join(pathToRegistryJSON, providerJSON), "utf8").toString(),
26+
);
27+
28+
// For every resource in that file, generate an individual search record, including
29+
// tokens as keywords to make them easier to find by copy/paste.
30+
function getMember(itemPath, item) {
31+
32+
// Some of the items in our registry nav files contain trailing slashes, which
33+
// don't play nicely with the string concatenation we do to build up our search
34+
// URLs. So we strip them out to avoid ending up with doubles (and thus 404s).
35+
item.link = item.link.replace(/\/+$/, "");
36+
37+
providerResults.push({
38+
title: `${item.name} (${itemPath.concat(item.name).join(".")})`,
39+
description: `API documentation for the ${item.name} ${item.type} of the ${providerTitle} provider, including examples, input and output properties, lookup functions, and supporting types.`,
40+
type: item.type,
41+
href: `/registry/packages/${providerName}/api-docs/${itemPath.concat(item.link).slice(1).join("/").toLowerCase()}/`,
42+
keywords: [
43+
item.name,
44+
itemPath.concat(item.name).slice(1).join(" ").toLowerCase(),
45+
itemPath.concat(item.name).join("."),
46+
itemPath.concat(item.name).join(":"),
47+
itemPath.concat(item.name).join(" "),
48+
`${providerTitle} ${item.name}`
49+
],
50+
ancestors: [ "Registry", providerTitle, "API Docs" ],
51+
});
52+
53+
if (item.children) {
54+
item.children.map(child => {
55+
getMember([...itemPath, item.name], child);
56+
});
57+
}
58+
}
59+
60+
providerItems.forEach(item => {
61+
getMember([ providerName ], item);
62+
});
63+
64+
return {
65+
name: providerName,
66+
items: providerResults,
67+
};
68+
})
69+
.forEach(member => {
70+
registryItems.push(...member.items);
71+
});
72+
73+
return registryItems.map(item => {
74+
return {
75+
objectID: page.getObjectID(item),
76+
title: item.title,
77+
description: item.description,
78+
section: "Registry",
79+
href: item.href,
80+
rank: page.getRank(item),
81+
keywords: item.keywords,
82+
ancestors: item.ancestors,
83+
};
84+
});
85+
}
86+
};

scripts/search/settings.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
import * as page from "./page";
1+
const page = require("./page");
22

33
// Algolia index settings.
44
//
55
// Changes to this file should be made with extreme care.
6+
module.exports = {
67

78
// Attributes for faceting are the properties to use for top-level filtering of results -- for
89
// example, whether a given record is returned for a "Docs" query, "Registry" query, and so on.
910
// https://www.algolia.com/doc/api-reference/api-parameters/attributesForFaceting/
10-
export function getAttributesForFaceting() {
11+
getAttributesForFaceting() {
1112
return [
1213
"searchable(section)",
1314
];
14-
}
15+
},
1516

1617
// Synonyms allow us to surface content that may not directly match users' queries.
1718
// https://www.algolia.com/doc/api-reference/api-methods/save-synonyms/#save-synonyms
18-
export function getSynonyms() {
19+
getSynonyms() {
1920
return [
2021
[".NET", "dotnet"],
2122
["aws", "aws classic"],
@@ -47,7 +48,7 @@ import * as page from "./page";
4748
synonyms,
4849
};
4950
});
50-
}
51+
},
5152

5253
// Searchable attributes control the fields that Algolia uses for query matching, as well as how
5354
// relevant those fields are in relation to one another.
@@ -57,7 +58,7 @@ import * as page from "./page";
5758
// the change on an index that isn't production.
5859
//
5960
// https://www.algolia.com/doc/guides/sending-and-managing-data/prepare-your-data/how-to/setting-searchable-attributes/#set-searchable-attributes-with-the-api
60-
export function getSearchableAttributes() {
61+
getSearchableAttributes() {
6162
return [
6263
"title,h1", // Title and H1 are considered of equal relevance, so they're listed on the same level.
6364
"keywords", // Keywords are treated as ordered. Those higher up in the list are considered more relevant.
@@ -68,25 +69,25 @@ import * as page from "./page";
6869
"section", // Section is the primary "facet" of a record -- Docs, Registry, Blog, Examples, etc.
6970
"unordered(authors)", // Blog-post authors are occasionally useful for searching, but their order is considered irrelevant.
7071
];
71-
}
72+
},
7273

7374
// Attributes to highlight contains the list of searchable attributes that might be used to highlight query matches in the UI.
7475
// https://www.algolia.com/doc/api-reference/api-parameters/attributesToHighlight/
75-
export function getAttributesToHighlight() {
76+
getAttributesToHighlight() {
7677
return [
7778
"title",
7879
"h1",
7980
"description",
8081
];
81-
}
82+
},
8283

8384
// Custom rankings are the attributes that Algolia uses for determining relative ranking --
8485
// specifically for breaking ties between records that rank similarly for text matching. Similar
8586
// to the above, order here is significant as well, so any changes here should be made with
8687
// equal care.
8788
//
8889
// https://www.algolia.com/doc/guides/managing-results/must-do/custom-ranking/how-to/configure-custom-ranking
89-
export function getCustomRanking() {
90+
getCustomRanking() {
9091
return [
9192

9293
// https://www.algolia.com/doc/guides/managing-results/must-do/custom-ranking/how-to/configure-custom-ranking/#configure-custom-ranking-with-the-api
@@ -95,12 +96,12 @@ import * as page from "./page";
9596
// https://www.algolia.com/doc/guides/managing-results/must-do/custom-ranking/how-to/boost-or-penalize-some-records/#using-the-api
9697
"desc(boosted)",
9798
];
98-
}
99+
},
99100

100101
// Rules are explicit instructions that apply fine-grained control over how certain queries are handled.
101102
// https://www.algolia.com/doc/guides/managing-results/rules/rules-overview/
102103
// https://www.algolia.com/doc/api-reference/api-methods/save-rule
103-
export function getRules() {
104+
getRules() {
104105
return [
105106

106107
// When the query is for "cloud", deliver the Pulumi Cloud overview page as the top result.
@@ -207,4 +208,5 @@ import * as page from "./page";
207208
},
208209
},
209210
];
210-
}
211+
},
212+
};

scripts/search/update-search-index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as axios from "axios";
2-
import * as settings from "./settings";
3-
import * as algoliasearch from "algoliasearch";
1+
const axios = require('axios');
2+
const settings = require("./settings");
3+
const algoliasearch = require("algoliasearch");
44

55
// URL of the JSON file
66
const registrySearchIndexUrl = "https://www.pulumi.com/registry/search-index.json";
@@ -19,7 +19,7 @@ if (!config.appID || !config.searchAPIKey || !config.adminAPIKey || !config.inde
1919
}
2020

2121
// Initialize the Algolia search client.
22-
const client = algoliasearch.default(config.appID, config.adminAPIKey);
22+
const client = algoliasearch(config.appID, config.adminAPIKey);
2323
const algoliaIndex = client.initIndex(config.indexName);
2424

2525
async function publishIndex() {
@@ -100,4 +100,4 @@ async function publishIndex() {
100100
await updateIndex(allObjects);
101101
}
102102

103-
publishIndex();
103+
publishIndex();

0 commit comments

Comments
 (0)