Skip to content

Commit

Permalink
feat: add multiline string support
Browse files Browse the repository at this point in the history
Co-authored-by: Max Rosoff <[email protected]>
Signed-off-by: Saurav Sharma <[email protected]>
  • Loading branch information
iamsauravsharma and mrrosoff committed Oct 23, 2024
1 parent c48a69f commit ee0a9b6
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 24 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ jobs:
run: |
if [[ -n $(git diff --exit-code) ]]
then
echo "Please run `pnpm run build && pnpm run package` to update dist folder"
exit
echo "Please run pnpm run build && pnpm run package to update dist folder"
exit 1
fi
test:
Expand Down Expand Up @@ -67,6 +67,12 @@ jobs:
ENV_KEY_SECRET_KEY: secret123
ENV_KEY_ENV_KEY_MULTIPLE: test
SOME_ENV_KEY: no_value
ENV_KEY_MULTILINE: |
some multiline
string here
- name: Cat generated dot env file
run: |
cat tests/development.env
- name: Install pnpm dependencies
run: |
pnpm install
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ lib/

# Mac related
.DS_STORE
*.env
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function envContentFromMap(contentMap, outputPrefix) {
(0, core_1.info)("Converting env content map to array");
const envFileArray = [];
for (const [key, value] of contentMap) {
const envLine = `${outputPrefix}${key}=${value}`;
const envLine = `${outputPrefix}${key}="${value.replace(/"/g, '\\"')}"`;
envFileArray.push(envLine);
}
(0, core_1.info)("Converting env array into string");
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"description": "Create dotenv file for github actions",
"main": "lib/main.js",
"packageManager": "[email protected].1",
"packageManager": "[email protected].2",
"scripts": {
"build": "tsc",
"format": "prettier --write **/*.ts",
Expand Down Expand Up @@ -35,6 +35,7 @@
"@types/jest": "^29.5.13",
"@types/node": "^22.7.5",
"@vercel/ncc": "^0.38.2",
"dotenv": "^16.4.5",
"jest": "^29.7.0",
"prettier": "^3.3.3",
"ts-jest": "^29.2.5",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function envContentFromMap(

const envFileArray: string[] = [];
for (const [key, value] of contentMap) {
const envLine = `${outputPrefix}${key}=${value}`;
const envLine = `${outputPrefix}${key}="${value.replace(/"/g, '\\"')}"`;
envFileArray.push(envLine);
}

Expand Down
30 changes: 12 additions & 18 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "fs";
import * as path from "path";
import * as readline from "readline";
import dotenv from "dotenv";

test("test if env file value matched with actual value", async () => {
const envFilePath = path.resolve(__dirname, "development.env");
Expand All @@ -11,30 +12,23 @@ test("test if env file value matched with actual value", async () => {
["OUTPUT_API_KEY", "USER_API_KEY"],
["OUTPUT_SECRET_KEY", "secret123"],
["OUTPUT_ENV_KEY_MULTIPLE", "test"],
["OUTPUT_MULTILINE", "some multiline\nstring here\n"],
]);
let actualMap: Map<string, string> = new Map();
const readLineInterface = readline.createInterface({
input: fs.createReadStream(envFilePath),
crlfDelay: Infinity,
});
for await (const line of readLineInterface) {
const splitLine = line.split("=");
if (splitLine.length != 2) {
continue;
}
actualMap.set(splitLine[0], splitLine[1]);
}
if (actualMap.size != expectedMap.size) {
const processedEnv = {};
dotenv.config({ path: envFilePath, processEnv: processedEnv });
const actualMap = new Map(Object.entries(processedEnv));

if (actualMap.size !== expectedMap.size) {
console.log(`Expected: ${[...expectedMap.entries()]}`);
console.log(`Actual: ${[...expectedMap.entries()]}`);
throw new Error("Size of two map doesn't matched");
throw new Error("Size of two map is not same");
}
for (const [key, val] of expectedMap) {
const actualValue = actualMap.get(key);
if (actualValue != val) {
console.log(`Expected: ${[...expectedMap.entries()]}`);
console.log(`Actual: ${[...expectedMap.entries()]}`);
throw new Error("Value doesn't matched");
if (actualValue !== val) {
console.log(`Expected: ${val}`);
console.log(`Actual: ${actualValue}`);
throw new Error(`${key} value is different with each other`);
}
}
});

0 comments on commit ee0a9b6

Please sign in to comment.