Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
fix config and setup script
Browse files Browse the repository at this point in the history
  • Loading branch information
Benedikt Rötsch committed Jul 24, 2018
1 parent cc56bdb commit 38c369e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
13 changes: 3 additions & 10 deletions .contentful.json.sample
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
{
"development": {
"host": "preview.contentful.com",
"spaceId": "...",
"accessToken": "..."
},
"production": {
"spaceId": "...",
"accessToken": "..."
}
}
"spaceId": "...",
"accessToken": "..."
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ With Contentful and Gatsby you can connect your favorite static site generator w

* Simple content model and structure. Easy to adjust to your needs.
* Contentful integration using our [Sync API](https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/synchronization/initial-synchronization-of-entries-of-a-specific-content-type)
* Using our [Preview API](https://www.contentful.com/developers/docs/references/content-preview-api/) for development and our [Delivery API](https://www.contentful.com/developers/docs/references/content-delivery-api/) for production.
* Using our [Delivery API](https://www.contentful.com/developers/docs/references/content-delivery-api/).
* Responsive/adaptive images via [gatsby-image](https://www.gatsbyjs.org/packages/gatsby-image/)

## Contribution
Expand Down Expand Up @@ -44,7 +44,7 @@ This project comes with a Contentful setup command `npm run setup`.

![Command line dialog of the npm run setup command](https://rawgit.com/contentful-userland/gatsby-contentful-starter/master/setup.jpg "Command line dialog of the npm run setup command")

This command will ask you for a space ID, and access tokens for the Contentful Management, Preview and Delivery API and then import the needed content model into the space you define and write a config file (`./contentful.json`).
This command will ask you for a space ID, and access tokens for the Contentful Management and Delivery API and then import the needed content model into the space you define and write a config file (`./contentful.json`).

`npm run setup` automates that for you but if you want to do it yourself rename `.contentful.json.sample` to `.contentful.json` and add your configuration in this file.

Expand All @@ -54,7 +54,7 @@ This project comes with a few handy commands for linting and code fixing. The mo

### `npm run dev`

Run in the project locally using the [Contentful Preview API](https://www.contentful.com/developers/docs/references/content-preview-api/). This can perfect to preview changes before they go into production.
Run in the project locally.

### `npm run build`

Expand Down
31 changes: 15 additions & 16 deletions gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
let contentfulConfig

try {
const configFile = require('./.contentful')
contentfulConfig = process.env.NODE_ENV === 'production'
? configFile.production
: configFile.development
} catch (_) {
contentfulConfig = {
spaceId: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_DELIVERY_TOKEN,
}
} finally {
const { spaceId, accessToken } = contentfulConfig
// Load the Contentful config from the .contentful.json
contentfulConfig = require('./.contentful')
} catch (_) {}

if (!spaceId || !accessToken) {
throw new Error(
'Contentful spaceId and the delivery token need to be provided.'
)
}
// Overwrite the Contentful config with environment variables if they exist
contentfulConfig = {
spaceId: process.env.CONTENTFUL_SPACE_ID || contentfulConfig.spaceId,
accessToken: process.env.CONTENTFUL_DELIVERY_TOKEN || contentfulConfig.accessToken,
}

const { spaceId, accessToken } = contentfulConfig

if (!spaceId || !accessToken) {
throw new Error(
'Contentful spaceId and the delivery token need to be provided.'
)
}

module.exports = {
Expand Down

0 comments on commit 38c369e

Please sign in to comment.