Created
August 8, 2016 16:45
-
-
Save ivanoats/b325178f9b5d1a425096304e7cdc6aea to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env babel-node | |
import contentful from 'contentful' | |
import fs from 'fs-extra-promise' | |
// Requires a test to see if .env file exists because it will not on CI build | |
if (fs.existsSync('.env')) require('dotenv').config() | |
// Contentful Config | |
const apiToken = process.env.CONTENTFUL_DELIVERY_API_TOKEN | |
const spaceId = process.env.CONTENTFUL_SPACE_ID | |
const client = contentful.createClient({ accessToken: apiToken, space: spaceId }) | |
async function getEntriesByType (contentType, fields) { | |
const options = { content_type: contentType, ...fields } | |
try { | |
return await client.getEntries(options) | |
} catch (error) { | |
console.log('LegacyPost error: ', error) | |
return [] | |
} | |
} | |
async function renderPost (post) { | |
try { | |
return fs.outputFile( | |
`pages/blog/${post.fields.slug}/index.json`, | |
JSON.stringify(post, null, 2) | |
) | |
} catch (error) { | |
console.log('Error creating post', error) | |
return Promise.reject('error') | |
} | |
} | |
async function renderAuthor (author) { | |
try { | |
return fs.outputFile( | |
`pages/blog/author/${author.fields.slug}/index.json`, | |
JSON.stringify(author, null, 2) | |
) | |
} catch (error) { | |
console.log('Error creating author', error) | |
return Promise.reject('error') | |
} | |
} | |
async function renderPage (page) { | |
try { | |
return fs.outputFile( | |
`pages/${page.fields.slug}/index.json`, | |
JSON.stringify(page, null, 2) | |
) | |
} catch (error) { | |
console.log('Error creating page', error) | |
return Promise.reject('error') | |
} | |
} | |
async function renderEventJson (events) { | |
// eslint-disable-next-line prefer-template | |
const data = '/* eslint-disable */\nexport default ' + JSON.stringify(events, null, 2) | |
try { | |
return fs.outputFile( | |
'components/EventCalendar/event-listings.js', | |
data | |
) | |
} catch (error) { | |
console.log('Error creating event listings', error) | |
return Promise.reject('error') | |
} | |
} | |
async function main () { | |
try { | |
const posts = await getEntriesByType('legacyPost') | |
const postPromises = posts.items.map(post => renderPost(post)) | |
await Promise.all(postPromises) | |
const pages = await getEntriesByType('legacyCustomPage', { fields: { active: true } }) | |
const pagePromises = pages.items.map(page => renderPage(page)) | |
await Promise.all(pagePromises) | |
const events = await getEntriesByType('event') // TODO date later than today | |
await renderEventJson(events) | |
const authors = await getEntriesByType('legacyAuthor') | |
const authorPromises = authors.items.map(author => renderAuthor(author)) | |
await Promise.all(authorPromises) | |
} catch (error) { console.log(error) } | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment