Last active
June 30, 2020 19:15
-
-
Save jasmo2/7bc0a6446a5d4edf1762d9139e91af8d 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
/* | |
* 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