Created
May 4, 2017 15:21
-
-
Save cedricdelpoux/3cf222c022a675928d7c19d9ecddad0d to your computer and use it in GitHub Desktop.
Script to fetch content from Contentfull using Phenomic
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
import contentful from "contentful" | |
import fs from "fs-extra-promise" | |
import request from "request-promise" | |
import grayMatter from "gray-matter" | |
import get from "lodash/get" | |
import pkg from "../package.json" | |
const client = contentful.createClient({ | |
accessToken: pkg.contentful["access-token"], | |
space: pkg.contentful["space-id"], | |
resolveLinks: true | |
}) | |
async function getEntriesByType (contentType, fields) { | |
const options = { content_type: contentType, fields } | |
try { | |
return await client.getEntries(options) | |
} catch (error) { | |
console.log('Get entries error: ', error) | |
return [] | |
} | |
} | |
async function getAssets() { | |
try { | |
return await client.getAssets() | |
} catch (error) { | |
console.log('Get assets error: ', error) | |
return [] | |
} | |
} | |
async function renderItem(item, config) { | |
try { | |
const {body, ...fields} = item.fields | |
return fs.outputFile( | |
`contentful/${config.folder}/${get(item, config.itemFieldId)}.md`, | |
grayMatter.stringify(body, { | |
...fields, | |
...fields.author && {author: fields.author.map(author => author.fields.name)}, | |
...fields.category && {category: fields.category.map(category => category.fields.title)}, | |
...fields.featuredImage && {featuredImage: "assets/" + fields.featuredImage.fields.file.fileName}, | |
...fields.layout && {layout: fields.layout.fields.title}, | |
}) | |
) | |
} catch (error) { | |
console.log('Error creating post', error) | |
return Promise.reject('error') | |
} | |
} | |
async function renderAsset(asset) { | |
try { | |
const {url, fileName} = asset.fields.file | |
return request(`http:${url}`).pipe(fs.createWriteStream(`contentful/assets/${fileName}`)) | |
} catch (error) { | |
console.log('Error creating asset', error) | |
return Promise.reject('error') | |
} | |
} | |
async function main (contentTypes) { | |
try { | |
contentTypes.map(contentType => { | |
getEntriesByType(contentType.id) | |
.then(contentTypeItems => { | |
const itemsPromises = contentTypeItems.items.map(item => renderItem(item, contentType)) | |
Promise.all(itemsPromises) | |
}) | |
}) | |
getAssets() | |
.then(assets => { | |
const assetsPromises = assets.items.map(asset => renderAsset(asset)) | |
Promise.all(assetsPromises) | |
}) | |
} catch (error) { console.log(error) } | |
} | |
// Your configuration | |
main([ | |
{ | |
id: "2wKn6yEnZewu2SCCkus4as", | |
folder: "posts", | |
itemFieldId: "sys.id", | |
} | |
]) |
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
{ | |
"scripts": { | |
"fetch-content": "babel-node ./scripts/fetch-content.js" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment