Last active
December 22, 2021 16:34
-
-
Save martiuh/17b44fa4bfe61e18b277a899308c541b to your computer and use it in GitHub Desktop.
nextjs-using-staging-deploy
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
/** | |
* Generate a `.env.production` file depending on the value from TARGET_ENV environment variable | |
* created with ❤️ by Tona González (@martiuh) | |
*/ | |
const fs = require('fs'); | |
/** | |
* @description copies the given `.env.base.${TARGET_ENV}` file to a `.env.production` file. | |
*/ | |
const env = { | |
production: 'production', | |
staging: 'staging', | |
}; | |
function copyEnvFile() { | |
const target = env[process.env.TARGET_ENV] || env.staging; | |
const dotenvPath = process.cwd() + `/.env.base.${target}`; | |
const fileStats = fs.statSync(dotenvPath); | |
if (!fileStats.isFile()) { | |
console.error(`[copyEnvFile] ${dotenvPath} is not a valid file`); | |
} | |
const prodDotEnv = '.env.production'; | |
try { | |
fs.copyFileSync(dotenvPath, `${process.cwd()}/${prodDotEnv}`); | |
console.log(`${prodDotEnv} successfully copied with TARGET_ENV=${target}`); | |
} catch (error) { | |
console.error( | |
`[copyEnvFile] there was an error copying ${prodDotEnv} file` | |
); | |
console.error(error); | |
} | |
return; | |
} | |
copyEnvFile(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment