MCPcopy
hub / github.com/goenning/google-indexing-script / index

Function index

src/index.ts:41–187  ·  view source on GitHub ↗
(input: string = process.argv[2], options: IndexOptions = {})

Source from the content-addressed store, hash-verified

39 * @param options - (Optional) Additional options for indexing.
40 */
41export const index = async (input: string = process.argv[2], options: IndexOptions = {}) => {
42 if (!input) {
43 console.error("❌ Please provide a domain or site URL as the first argument.");
44 console.error("");
45 process.exit(1);
46 }
47
48 if (!options.client_email) {
49 options.client_email = process.env.GIS_CLIENT_EMAIL;
50 }
51 if (!options.private_key) {
52 options.private_key = process.env.GIS_PRIVATE_KEY;
53 }
54 if (!options.path) {
55 options.path = process.env.GIS_PATH;
56 }
57 if (!options.urls) {
58 options.urls = process.env.GIS_URLS ? process.env.GIS_URLS.split(",") : undefined;
59 }
60 if (!options.quota) {
61 options.quota = {
62 rpmRetry: process.env.GIS_QUOTA_RPM_RETRY === "true",
63 };
64 }
65
66 const accessToken = await getAccessToken(options.client_email, options.private_key, options.path);
67 let siteUrl = convertToSiteUrl(input);
68 console.log(`🔎 Processing site: ${siteUrl}`);
69 const cachePath = path.join(".cache", `${convertToFilePath(siteUrl)}.json`);
70
71 if (!accessToken) {
72 console.error("❌ Failed to get access token, check your service account credentials.");
73 console.error("");
74 process.exit(1);
75 }
76
77 siteUrl = await checkSiteUrl(accessToken, siteUrl);
78
79 let pages = options.urls || [];
80 if (pages.length === 0) {
81 console.log(`🔎 Fetching sitemaps and pages...`);
82 const [sitemaps, pagesFromSitemaps] = await getSitemapPages(accessToken, siteUrl);
83
84 if (sitemaps.length === 0) {
85 console.error("❌ No sitemaps found, add them to Google Search Console and try again.");
86 console.error("");
87 process.exit(1);
88 }
89
90 pages = pagesFromSitemaps;
91
92 console.log(`👉 Found ${pages.length} URLs in ${sitemaps.length} sitemap`);
93 } else {
94 pages = checkCustomUrls(siteUrl, pages);
95 console.log(`👉 Found ${pages.length} URLs in the provided list`);
96 }
97
98 const statusPerUrl: Record<string, { status: Status; lastCheckedAt: string }> = existsSync(cachePath)

Callers 1

cli.tsFile · 0.85

Calls 12

getAccessTokenFunction · 0.90
convertToSiteUrlFunction · 0.90
convertToFilePathFunction · 0.90
checkSiteUrlFunction · 0.90
getSitemapPagesFunction · 0.90
checkCustomUrlsFunction · 0.90
batchFunction · 0.90
getPageIndexingStatusFunction · 0.90
getEmojiForStatusFunction · 0.90
getPublishMetadataFunction · 0.90
requestIndexingFunction · 0.90
shouldRecheckFunction · 0.85

Tested by

no test coverage detected