Skip to content
This repository was archived by the owner on Feb 16, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions bin/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const questions = [
{
name: 'spaceId',
message: 'Your Space ID',
when: !argv.spaceId,
when: !argv.spaceId && !process.env.CONTENTFUL_SPACE_ID,
validate: input =>
/^[a-z0-9]{12}$/.test(input) ||
'Space ID must be 12 lowercase characters',
Expand All @@ -47,18 +47,22 @@ const questions = [
},
{
name: 'deliveryToken',
when: !argv.accessToken,
when: !argv.accessToken && !process.env.CONTENTFUL_DELIVERY_TOKEN,
message: 'Your Content Delivery API access token',
},
]

inquirer
.prompt(questions)
.then(({ spaceId, managementToken, deliveryToken }) => {
const { CONTENTFUL_SPACE_ID, CONTENTFUL_DELIVERY_TOKEN } = process.env

spaceId = spaceId || argv.spaceId
managementToken = managementToken || argv.managementToken
deliveryToken = deliveryToken || argv.deliveryToken
// env vars are given precedence followed by args provided to the setup
// followed by input given to prompts displayed by the setup script
spaceId = CONTENTFUL_SPACE_ID || argv.spaceId || spaceId
managementToken = argv.managementToken || managementToken
deliveryToken =
CONTENTFUL_DELIVERY_TOKEN || argv.deliveryToken || deliveryToken

console.log('Writing config file...')
const configFilePath = path.resolve(__dirname, '..', '.contentful.json')
Expand Down
21 changes: 19 additions & 2 deletions gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
const contentfulConfig = require('./.contentful')
let contentfulConfig

try {
contentfulConfig = require('./.contentful')
} catch (_) {
contentfulConfig = {
spaceId: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_DELIVERY_TOKEN,
}
} finally {
const { spaceId, accessToken } = contentfulConfig

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

module.exports = {
pathPrefix: '/gatsby-contentful-starter',
Expand All @@ -7,7 +24,7 @@ module.exports = {
'gatsby-plugin-react-helmet',
{
resolve: 'gatsby-source-contentful',
options: contentfulConfig
options: contentfulConfig,
},
],
}