Created
March 26, 2018 08:52
-
-
Save HoverBaum/ad1801c41d8c4f4556e4508d0ca01621 to your computer and use it in GitHub Desktop.
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
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