Skip to content

Instantly share code, notes, and snippets.

@martiuh
Last active December 22, 2021 16:34
Show Gist options
  • Save martiuh/17b44fa4bfe61e18b277a899308c541b to your computer and use it in GitHub Desktop.
Save martiuh/17b44fa4bfe61e18b277a899308c541b to your computer and use it in GitHub Desktop.
nextjs-using-staging-deploy
/**
* 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