generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from FranciscoKloganB/master
feat(ensureExists): allow action to succeed when file not found
- Loading branch information
Showing
6 changed files
with
116 additions
and
53 deletions.
There are no files selected for viewing
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
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,44 @@ | ||
const fs = require("fs"); | ||
const core = require("@actions/core"); | ||
|
||
let dotenv_action = function (dotenvFile, keysCase) { | ||
if (!fs.existsSync(dotenvFile)) { | ||
throw new Error("file does not exist"); | ||
const fs = require("node:fs"); | ||
|
||
/** | ||
* @param {object} kwargs | ||
* @param {boolean} kwargs.ensureExists ensure that the file exists | ||
* @param {'bypass' | 'lower' | 'upper'} kwargs.keysCase transform keys case or preserve them as they are | ||
* @param {string} kwargs.path a relative path from the repository root | ||
*/ | ||
const dotenvAction = ({ path, keysCase, ensureExists }) => { | ||
const exists = fs.existsSync(path); | ||
|
||
if (!exists && ensureExists) { | ||
core.info( | ||
"Action will fail as it was unable to load the environment file from the provided path. If you wish to bypass this check, set the 'ensure-exists' option to 'false'.", | ||
); | ||
|
||
throw new Error(`Environment file not found at path: '${path}'`); | ||
} | ||
|
||
const dotenv = require("dotenv").config({ path: dotenvFile }); | ||
const dotenv = require("dotenv").config({ path: path }); | ||
const dotenv_expand = require("dotenv-expand").expand(dotenv); | ||
console.log("loading .env file from " + dotenvFile); | ||
|
||
core.info(`Loading environment file from path: '${path}'`); | ||
|
||
const returnedMap = {}; | ||
|
||
for (const key in dotenv_expand.parsed) { | ||
const value = dotenv_expand.parsed[key]; | ||
|
||
if (keysCase == "bypass") { | ||
if (keysCase === "bypass") { | ||
returnedMap[key] = value; | ||
} else if (keysCase == "upper") { | ||
} else if (keysCase === "upper") { | ||
returnedMap[key.toLocaleUpperCase()] = value; | ||
} else { | ||
returnedMap[key.toLocaleLowerCase()] = value; | ||
} | ||
} | ||
|
||
return returnedMap; | ||
}; | ||
|
||
module.exports = dotenv_action; | ||
module.exports = dotenvAction; |
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
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