Skip to content

Commit

Permalink
write an env file and source it
Browse files Browse the repository at this point in the history
  • Loading branch information
cball committed Apr 12, 2020
1 parent ceed1fd commit 1801ed6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

## [v0.0.5](https://github.com/cball/netlify-plugin-env/compare/v0.0.4...v0.0.5) - 2020-04-12

### Commits

- write an env file and source it [`def421b`](https://github.com/cball/netlify-plugin-env/commit/def421bcf22d836bb71ad04cb49adcc1d5dbe9ef)

## [v0.0.4](https://github.com/cball/netlify-plugin-env/compare/v0.0.3...v0.0.4) - 2020-04-12

### Commits
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "netlify-plugin-env",
"version": "0.0.4",
"version": "0.0.5",
"description": "A Netlify plugin to override ENV vars based on a branch or context",
"main": "src/index",
"repository": "https://github.com/cball/netlify-plugin-env",
Expand Down
25 changes: 21 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const { exec } = require("child_process");
const fs = require("fs");

/**
* Overrides an ENV var with a value if it exists
* @param {*} key the key to overwrite if found
Expand All @@ -6,26 +9,27 @@
*/
function setEnvWithValue(key, contextOrBranch, mode) {
let foundOne = false;
let found;

if (mode === "prefix") {
const prefixedEnvVar = `${contextOrBranch}_${key}`;

if (process.env[prefixedEnvVar]) {
foundOne = true;
console.log(`Setting ${key} to the value from ${prefixedEnvVar}.`);
process.env[key] = process.env[prefixedEnvVar];
found = `${key}=${process.env[prefixedEnvVar]}`;
}
} else {
const suffixedEnvVar = `${key}_${contextOrBranch}`;

if (process.env[suffixedEnvVar]) {
foundOne = true;
console.log(`Setting ${key} to the value from ${suffixedEnvVar}.`);
process.env[key] = process.env[suffixedEnvVar];
found = `${key}=${process.env[suffixedEnvVar]}`;
}
}

return foundOne;
return found;
}

module.exports = {
Expand All @@ -38,10 +42,23 @@ module.exports = {
const foundContext = setEnvWithValue(key, context, inputs.mode);
const foundBranch = setEnvWithValue(key, branch, inputs.mode);

if (foundContext || foundBranch) replaced.push(key);
if (foundContext) replaced.push(foundContext);
if (foundBranch) replaced.push(foundBranch);
});

if (replaced.length) {
// Write a file
const file = fs.createWriteStream(".env");
replaced.forEach(function (v) {
file.write(`${v}\n`);
});
file.end();

// Call the file
exec("source .env");

// ALternatively, write .env?

console.log(`Replaced ${replaced.length} ENVs`);
} else {
console.log(`Nothing found... keeping default ENVs`);
Expand Down

0 comments on commit 1801ed6

Please sign in to comment.