Skip to content

Commit

Permalink
Test: Match NextJs env lookup and load.
Browse files Browse the repository at this point in the history
  • Loading branch information
sherakama committed May 3, 2024
1 parent 4f92726 commit 9f95e0b
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ module.exports = {
// Overwrite existing secrets.
const overwrite = process.env.VAULT_OVERWRITE || false;
const isNetlify = process.env.NETLIFY || false;
const nodeEnv =
process.env.CONTEXT !== 'production' ? 'development' : 'production';

console.log(
`Overwrite existing secrets was set to: ${overwrite.toString()}`
Expand Down Expand Up @@ -80,7 +78,7 @@ module.exports = {
if (!Array.isArray(netlifyConfig.functions['*'].included_files)) {
netlifyConfig.functions['*'].included_files = [];
}
netlifyConfig.functions['*'].included_files.push(`.env.${nodeEnv}`);
netlifyConfig.functions['*'].included_files.push('.env');
}

// Contextualize the secrets.
Expand All @@ -102,16 +100,13 @@ module.exports = {

let existingSecrets = '';
const envFilePath = path.resolve(process.cwd(), '.env');
const writeEnvFilePath = path.resolve(process.cwd(), `.env.${nodeEnv}`);

console.log(`Writing to ${writeEnvFilePath}`);
const writeEnvFilePath = path.resolve(process.cwd(), '.env.vault');

// Read existing env file.
try {
existingSecrets = fs.readFileSync(envFilePath).toString();
} catch (err) {
// Don't fail when no .env file already
console.log('No existing .env file found');
}

// Write new env file.
Expand All @@ -127,4 +122,17 @@ module.exports = {
summary: `Added environment variables from vault to environment and LAMBDA`,
});
},
// Remove env file if on Netilfy.
async onPostBuild() {
const isNetlify = process.env.NETLIFY || false;
const envFilePath = path.resolve(process.cwd(), '.env');
const vaultEnvFilePath = path.resolve(process.cwd(), '.env.vault');
if (isNetlify) {
try {
fs.renameSync(vaultEnvFilePath, envFilePath);
} catch (err) {
console.error('Failed to rename .env.vault to .env', { err });
}
}
},
};

0 comments on commit 9f95e0b

Please sign in to comment.