Skip to content

Instantly share code, notes, and snippets.

@HoverBaum
Created March 26, 2018 08:52
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 HoverBaum/ad1801c41d8c4f4556e4508d0ca01621 to your computer and use it in GitHub Desktop.
Save HoverBaum/ad1801c41d8c4f4556e4508d0ca01621 to your computer and use it in GitHub Desktop.
const createAndPublishCategories = async (categories, spaceId, managementToken, simpleLog = console.log) => {
const client = contentful.createClient({
accessToken: managementToken,
logHandler: (level, data) => simpleLog(`${level} | ${data}`)
})
const space = await client.getSpace(spaceId)
const createdCategories = await Promise.all(categories.map(category => new Promise(async resolve => {
let cmsCategory
try {
cmsCategory = await space.createEntry('blogCategory', {
fields: {
categoryName: {
'en-US': category.name
}
}
})
} catch(e) {
throw(Error(e))
}
try {
await cmsCategory.publish()
} catch(e) {
throw(Error(e))
}
// Save mapping information to contentful.
cmsCategory.wpCategory = category
resolve(cmsCategory)
})))
return createdCategories
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment