Skip to content

Instantly share code, notes, and snippets.

@colinappnovation
Last active April 9, 2021 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinappnovation/315f9e302b7b82c8259d70a2cce3de97 to your computer and use it in GitHub Desktop.
Save colinappnovation/315f9e302b7b82c8259d70a2cce3de97 to your computer and use it in GitHub Desktop.
Get all Content Types from Contentful for all Spaces which executer has access to
const contentful = require("contentful-management");
const fs = require("fs");
require("dotenv").config();
const { CONTENTFUL_MANAGEMENT_TOKEN } = process.env;
const client = contentful.createClient({
accessToken: CONTENTFUL_MANAGEMENT_TOKEN,
});
(async () => {
const spaces = await client.getSpaces();
async function getContentTypes(spaceId = "") {
const space = await client.getSpace(spaceId);
const env = await space.getEnvironment("master");
return await env.getContentTypes();
}
const entries = await Promise.all(
spaces.items.map(async (s) => {
const {
name,
sys: { id },
} = s;
const spaceTypes = await getContentTypes(id);
return { name, contentTypes: spaceTypes.items };
})
);
fs.writeFileSync(
"all-space-content-types.json",
JSON.stringify(entries, null, 4)
);
console.log("All done!");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment