Skip to content

Instantly share code, notes, and snippets.

@jasmo2
Last active June 30, 2020 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasmo2/7bc0a6446a5d4edf1762d9139e91af8d to your computer and use it in GitHub Desktop.
Save jasmo2/7bc0a6446a5d4edf1762d9139e91af8d to your computer and use it in GitHub Desktop.
/*
* in package.json set the next script:
* "update-refs": "node update-refs.js ENVIRONTMENT_A && node update-refs.js ENVIRONTMENT_B && ... && node update-refs.js ENVIRONTMENT_i && git add ."
*/
const fs = require('fs')
const axios = require('axios')
const saveRef = (envFile, ref) => {
const envDev = fs.readFileSync(envFile, {
encoding: 'utf8',
})
const replaced = envDev.replace(
/PRISMIC_API_REF=.*/gm,
`PRISMIC_API_REF=${ref}`
)
fs.writeFileSync(envFile, replaced)
}
const replace = envKey => result => {
const envFile = `./.env.${envKey}`
let ref = envKey === 'production' ? result[envKey] : result['staging']
if (envKey === 'staging') {
ref = result['production']
}
saveRef(envFile, ref)
}
var args = process.argv.slice(2)
const baseUrl = 'https://REPO.cdn.prismic.io/api/v2'
axios
.get(baseUrl)
.then(response => response.data)
.then(data => data.refs)
.then(refs => ({
staging: refs[1].ref,
production: refs[0].ref,
}))
.then(result => replace(args[0])(result))
.catch(error => console.log('TCL: error', error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment