Skip to content

Commit 13c2271

Browse files
authored
Merge pull request #2 from meese-os/feat/comment-support
2 parents 59f903e + a10e8af commit 13c2271

File tree

6 files changed

+52
-5
lines changed

6 files changed

+52
-5
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
npm install complex-dotenv-json
1010
```
1111

12-
This is a drop-in replacement for the [dotenv-json](https://www.npmjs.com/package/dotenv-json) package, with support for complex JSON objects.
12+
This is a drop-in replacement for the [dotenv-json](https://www.npmjs.com/package/dotenv-json) package, with support for complex JSON objects. It ships with zero runtime dependencies for a lightweight install.
1313

1414
## Usage
1515

@@ -28,6 +28,15 @@ Define your environment variables in `.env.json` in the root of your project (or
2828
}
2929
```
3030

31+
Lines beginning with `//` are ignored during parsing, so you can annotate your configuration while keeping it valid:
32+
33+
```json
34+
// Credentials for local development only
35+
{
36+
"secret_api_key": "s@Mpl3_d@Ta_SECRET"
37+
}
38+
```
39+
3140
Note that in the example above, the `database` section is a nested object. This is not able to be stored in a tree structure in environmental variables, so instead it is stored as a JSON string. It can easily be accessed by the following code:
3241

3342
```js
@@ -55,3 +64,11 @@ You can customize the location of your `.env.json` file by passing a `path` opti
5564
const dotenvJSON = require("complex-dotenv-json");
5665
dotenvJSON({ path: "./config/example.json" });
5766
```
67+
68+
## Development
69+
70+
Run the tests locally before committing code to ensure everything is working as expected:
71+
72+
```bash
73+
npm test
74+
```

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ module.exports = function dotenvJSON(options) {
99
});
1010

1111
try {
12-
const envConfig = JSON.parse(jsonString);
12+
const cleanString = jsonString
13+
.split(/\r?\n/)
14+
.filter(line => !line.trim().startsWith("//"))
15+
.join("\n");
16+
17+
const envConfig = JSON.parse(cleanString);
1318

1419
for (const key in envConfig) {
1520
if (process.env.hasOwnProperty(key)) {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "complex-dotenv-json",
3-
"version": "1.1.3",
3+
"version": "1.2.0",
44
"description": "Load environment variables via a JSON file",
55
"main": "index.js",
66
"scripts": {

tests/.env.comments.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Sample comment at top
2+
{
3+
// secret key for tests
4+
"secret_api_key": "s@Mpl3_d@Ta_SECRET",
5+
"website_url": "https://example.com/login",
6+
"feature_flags": {
7+
// enable beta
8+
"beta": true
9+
}
10+
}

tests/index.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,24 @@ dotenvJSON({ path: "tests/.env.json" });
1010
// test simple properties
1111
assert.ok(process.env.hasOwnProperty("secret_api_key"));
1212
assert.equal(process.env.secret_api_key, "s@Mpl3_d@Ta_SECRET");
13+
console.log("✔ basic environment variables loaded");
1314

1415
// test nested properties
1516
assert.ok(process.env.hasOwnProperty("database"));
1617
const database = JSON.parse(process.env.database);
1718
assert.ok(database.hasOwnProperty("password"));
1819
assert.equal(database.password, "postgres");
20+
console.log("✔ nested environment variables stored as JSON");
21+
22+
// loads JSON file that includes comment lines
23+
delete process.env.feature_flags;
24+
delete process.env.website_url;
25+
dotenvJSON({ path: "tests/.env.comments.json" });
26+
27+
assert.ok(process.env.hasOwnProperty("feature_flags"));
28+
const featureFlags = JSON.parse(process.env.feature_flags);
29+
assert.equal(featureFlags.beta, true);
30+
assert.equal(process.env.website_url, "https://example.com/login");
31+
console.log("✔ comment-stripping logic applied correctly");
32+
33+
console.log("All tests passed");

0 commit comments

Comments
 (0)