Created
February 26, 2025 15:15
-
-
Save cachrisman/f1b1c158ea3e1c7486536f8813846052 to your computer and use it in GitHub Desktop.
script using contentful.js and async-parallel to identify entries of specified content types with no incoming links (aka orphaned entries)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => { | |
const contentful = require("contentful"); | |
const parallel = require("async-parallel"); | |
const space_id = "SPACE_ID"; | |
const environment_id = "ENV_ID"; | |
const access_token = "ACCESS_TOKEN"; | |
let content_type_ids = [ | |
"content_type_id1", | |
"content_type_id2", | |
"content_type_id3", | |
]; | |
// do not edit | |
let content_types; | |
const cpa_client = contentful.createClient({ | |
space: space_id, | |
environment: environment_id, | |
accessToken: access_token, | |
host: "preview.contentful.com", | |
}); | |
const getContentTypes = async () => { | |
const cts = await cpa_client.getContentTypes({ | |
"sys.id[in]": content_type_ids.join(","), | |
}); | |
return cts.items; | |
}; | |
const getEntries = async () => { | |
const response = await cpa_client.getEntries({ | |
"sys.contentType.sys.id[in]": content_type_ids.join(","), | |
}); | |
return response.items; | |
}; | |
const calculateUsage = async (entry) => { | |
const response = await cpa_client.getEntries({ | |
links_to_entry: entry.sys.id, | |
}); | |
if (response.items.length > 0) return; | |
const content_type = content_types.filter( | |
(ct) => ct.sys.id == entry.sys.contentType.sys.id | |
)[0]; | |
return { | |
entry_name: entry.fields[content_type.displayField], | |
content_type: content_type.name, | |
// incoming_links: response.items.length, | |
link: `https://app.contentful.com/spaces/${space_id}/environments/${environment_id}/entries/${entry.sys.id}`, | |
}; | |
}; | |
try { | |
content_types = await getContentTypes(); | |
const entries = await getEntries(); | |
parallel.setConcurrency(3); | |
let usage = await parallel.map(entries, calculateUsage); | |
usage = usage.filter((item) => item !== undefined); | |
// usage.sort((a,b) => b.incoming_links - a.incoming_links) | |
console.log( | |
"\n================================================= Usage =================================================\n" | |
); | |
console.table(usage); | |
} catch (error) { | |
console.log(error); | |
return {}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment