make-json-to-env 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +87 -32
- package/dist/index.js +118 -0
- package/package.json +31 -7
- package/index.js +0 -62
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ajay Vishwakarma
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,82 +1,137 @@
|
|
|
1
|
-
# make-json-to-env
|
|
1
|
+
# 🚀 make-json-to-env
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**The ultimate zero-dependency CLI tool to transform complex, nested JSON into clean, production-ready `.env` files.**
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 💡 Why `make-json-to-env`?
|
|
8
|
+
|
|
9
|
+
Most JSON-to-ENV converters stop at the first level or handle arrays poorly. `make-json-to-env` was built to solve the "Deep Config" problem. Whether you have a 10-level nested Firebase config or a simple flat JSON, this tool generates a standard, predictable `.env` structure that works with `dotenv`, Docker, and Shell scripts.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## 🔥 Features
|
|
14
|
+
|
|
15
|
+
* **⚡ Ultra-Lightweight** – Zero dependencies. It uses native Node.js `fs` and `path` modules.
|
|
16
|
+
* **🌳 Infinite Recursion** – No depth limits. It flattens deep objects into a `PARENT_CHILD_KEY` convention.
|
|
17
|
+
* **🔢 Intelligent Array Mapping** – Converts arrays into indexed variables (e.g., `TAGS_0`, `TAGS_1`).
|
|
18
|
+
* **🛡️ Built with TypeScript** – Fully type-safe with robust error handling for invalid JSON.
|
|
19
|
+
* **⚙️ Prefix Support** – Prepend `export `, `SET `, or `REACT_APP_` to every variable automatically.
|
|
20
|
+
* **🚫 Smart Flattening Toggle** – Use `--no-flatten` if you want nested objects to stay as JSON strings.
|
|
4
21
|
|
|
5
22
|
---
|
|
6
23
|
|
|
7
24
|
## 📦 Installation
|
|
8
25
|
|
|
9
|
-
|
|
26
|
+
### Use without installing (Recommended)
|
|
10
27
|
|
|
11
28
|
```bash
|
|
12
|
-
npx make-json-to-env
|
|
29
|
+
npx make-json-to-env config.json
|
|
13
30
|
|
|
14
31
|
```
|
|
15
32
|
|
|
16
|
-
|
|
33
|
+
### Global Installation
|
|
17
34
|
|
|
18
35
|
```bash
|
|
19
36
|
npm install -g make-json-to-env
|
|
20
37
|
|
|
21
38
|
```
|
|
22
39
|
|
|
23
|
-
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## 🛠 Usage & Commands
|
|
24
43
|
|
|
25
|
-
|
|
44
|
+
The tool provides the `make-json-to-env` command.
|
|
26
45
|
|
|
27
|
-
### Basic Conversion
|
|
46
|
+
### 1. Basic Conversion
|
|
47
|
+
|
|
48
|
+
Converts `config.json` to `config.env`.
|
|
28
49
|
|
|
29
50
|
```bash
|
|
30
|
-
|
|
51
|
+
make-json-to-env config.json
|
|
31
52
|
|
|
32
53
|
```
|
|
33
54
|
|
|
34
|
-
|
|
55
|
+
### 2. Using Custom Prefixes
|
|
56
|
+
|
|
57
|
+
Great for shell scripts or specific frameworks (like Create React App).
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
make-json-to-env config.json --prefix="export "
|
|
61
|
+
|
|
62
|
+
```
|
|
35
63
|
|
|
36
|
-
###
|
|
64
|
+
### 3. Disabling Flattening
|
|
37
65
|
|
|
38
|
-
|
|
66
|
+
Keep nested objects as serialized strings.
|
|
39
67
|
|
|
40
68
|
```bash
|
|
41
|
-
|
|
69
|
+
make-json-to-env data.json --no-flatten
|
|
42
70
|
|
|
43
71
|
```
|
|
44
72
|
|
|
45
73
|
---
|
|
46
74
|
|
|
47
|
-
## 📋 Example
|
|
75
|
+
## 📋 Deep-Flattening Example
|
|
48
76
|
|
|
49
|
-
**Input (`
|
|
77
|
+
**Input (`settings.json`):**
|
|
50
78
|
|
|
51
79
|
```json
|
|
52
80
|
{
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
81
|
+
"server": {
|
|
82
|
+
"port": 3000,
|
|
83
|
+
"auth": {
|
|
84
|
+
"methods": ["google", "github"],
|
|
85
|
+
"enabled": true
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"log_level": "info"
|
|
56
89
|
}
|
|
57
90
|
|
|
58
91
|
```
|
|
59
92
|
|
|
60
|
-
**
|
|
93
|
+
**Output (`settings.env`):**
|
|
61
94
|
|
|
62
|
-
```
|
|
63
|
-
|
|
95
|
+
```env
|
|
96
|
+
SERVER_PORT=3000
|
|
97
|
+
SERVER_AUTH_METHODS_0=google
|
|
98
|
+
SERVER_AUTH_METHODS_1=github
|
|
99
|
+
SERVER_AUTH_ENABLED=true
|
|
100
|
+
LOG_LEVEL=info
|
|
64
101
|
|
|
65
102
|
```
|
|
66
103
|
|
|
67
|
-
|
|
104
|
+
---
|
|
68
105
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
106
|
+
## 🚀 Advanced Flag Reference
|
|
107
|
+
|
|
108
|
+
| Flag | Type | Description |
|
|
109
|
+
| --- | --- | --- |
|
|
110
|
+
| `<filename>` | `string` | **Required.** The path to your JSON source file. |
|
|
111
|
+
| `--prefix` | `string` | Prepend a string to every key (e.g., `--prefix="VITE_"`). |
|
|
112
|
+
| `--no-flatten` | `boolean` | Stops recursion. Nested objects are saved as JSON strings. |
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## 🛠 Development & Contributing
|
|
117
|
+
|
|
118
|
+
Contributions are welcome! To set up the project locally:
|
|
119
|
+
|
|
120
|
+
1. **Clone the repo:** `git clone https://github.com/ajay-vish/make-json-to-env.git`
|
|
121
|
+
2. **Install types:** `npm install`
|
|
122
|
+
3. **Build from TS:** `npm run build`
|
|
123
|
+
4. **Test locally:** `node dist/index.js example.json`
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## 📄 License
|
|
128
|
+
|
|
129
|
+
Distributed under the **MIT License**. See the `LICENSE` file for full text.
|
|
74
130
|
|
|
75
131
|
---
|
|
76
132
|
|
|
77
|
-
##
|
|
133
|
+
## ⭐️ Support
|
|
134
|
+
|
|
135
|
+
If this tool saved you time, please give it a star on [GitHub](https://github.com/ajay-vish/make-json-to-env)!
|
|
78
136
|
|
|
79
|
-
|
|
80
|
-
* **Smart Naming:** Automatically generates the output filename based on your input.
|
|
81
|
-
* **Case Formatting:** Automatically converts keys to `UPPER_CASE` for standard `.env` compliance.
|
|
82
|
-
* **Security First:** Helps you create `.env.example` files so you never accidentally commit your real secrets.
|
|
137
|
+
---
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
20
|
+
var ownKeys = function(o) {
|
|
21
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
22
|
+
var ar = [];
|
|
23
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
24
|
+
return ar;
|
|
25
|
+
};
|
|
26
|
+
return ownKeys(o);
|
|
27
|
+
};
|
|
28
|
+
return function (mod) {
|
|
29
|
+
if (mod && mod.__esModule) return mod;
|
|
30
|
+
var result = {};
|
|
31
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
+
__setModuleDefault(result, mod);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
const fs = __importStar(require("fs"));
|
|
38
|
+
const path = __importStar(require("path"));
|
|
39
|
+
const args = process.argv.slice(2);
|
|
40
|
+
const inputFile = args.find((arg) => !arg.startsWith("--"));
|
|
41
|
+
const prefixArg = args.find((arg) => arg.startsWith("--prefix="));
|
|
42
|
+
const noFlatten = args.includes("--no-flatten");
|
|
43
|
+
const prefix = prefixArg
|
|
44
|
+
? prefixArg.split("=")[1].replace(/['"]+/g, "")
|
|
45
|
+
: "";
|
|
46
|
+
if (!inputFile) {
|
|
47
|
+
console.error('❌ Usage: convert-json-env <file.json> [--prefix="export "] [--no-flatten]');
|
|
48
|
+
process.exit(1);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Recursive Flattener
|
|
52
|
+
*/
|
|
53
|
+
function flattenObject(obj, parentKey = "", res = {}) {
|
|
54
|
+
for (let key in obj) {
|
|
55
|
+
const propName = parentKey
|
|
56
|
+
? `${parentKey}_${key.toUpperCase()}`
|
|
57
|
+
: key.toUpperCase();
|
|
58
|
+
if (Array.isArray(obj[key])) {
|
|
59
|
+
obj[key].forEach((val, i) => {
|
|
60
|
+
flattenObject({ [`${i}`]: val }, propName, res);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
else if (typeof obj[key] === "object" && obj[key] !== null) {
|
|
64
|
+
flattenObject(obj[key], propName, res);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
res[propName] = obj[key];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return res;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Simple Formatter (No flattening)
|
|
74
|
+
*/
|
|
75
|
+
function simpleFormat(obj) {
|
|
76
|
+
const res = {};
|
|
77
|
+
for (let key in obj) {
|
|
78
|
+
res[key.toUpperCase()] = obj[key];
|
|
79
|
+
}
|
|
80
|
+
return res;
|
|
81
|
+
}
|
|
82
|
+
function run() {
|
|
83
|
+
const inputPath = path.resolve(process.cwd(), inputFile);
|
|
84
|
+
const fileInfo = path.parse(inputPath);
|
|
85
|
+
const outputPath = path.join(fileInfo.dir, `${fileInfo.name}.env`);
|
|
86
|
+
if (!fs.existsSync(inputPath)) {
|
|
87
|
+
console.error(`❌ File not found: ${inputFile}`);
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const json = JSON.parse(fs.readFileSync(inputPath, "utf8"));
|
|
92
|
+
// Choose strategy based on flag
|
|
93
|
+
const dataToProcess = noFlatten ? simpleFormat(json) : flattenObject(json);
|
|
94
|
+
const envContent = Object.entries(dataToProcess)
|
|
95
|
+
.map(([key, value]) => {
|
|
96
|
+
let val = value;
|
|
97
|
+
// If no-flatten is on, objects stay objects, so we stringify them
|
|
98
|
+
if (typeof val === "object" && val !== null) {
|
|
99
|
+
val = JSON.stringify(val);
|
|
100
|
+
}
|
|
101
|
+
else if (val === null) {
|
|
102
|
+
val = "";
|
|
103
|
+
}
|
|
104
|
+
let strVal = String(val);
|
|
105
|
+
if (strVal.includes(" "))
|
|
106
|
+
strVal = `"${strVal}"`;
|
|
107
|
+
return `${prefix}${key}=${strVal}`;
|
|
108
|
+
})
|
|
109
|
+
.join("\n");
|
|
110
|
+
fs.writeFileSync(outputPath, envContent);
|
|
111
|
+
console.log(`🚀 Done! Mode: ${noFlatten ? "Simple" : "Recursive Flatten"}`);
|
|
112
|
+
console.log(`✅ Saved to: ${path.basename(outputPath)}`);
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
console.error("❌ Error:", e.message);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
run();
|
package/package.json
CHANGED
|
@@ -1,28 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "make-json-to-env",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
5
|
-
"main": "index.js",
|
|
3
|
+
"version": "1.0.6",
|
|
4
|
+
"description": "Infinite recursive JSON to ENV flattener written in TypeScript.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"make-json-to-env": "./index.js"
|
|
7
|
+
"make-json-to-env": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"prepare": "npm run build",
|
|
17
|
+
"start": "ts-node src/index.ts",
|
|
18
|
+
"prepublishOnly": "npm run build"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/ajay-vish/make-json-to-env.git"
|
|
23
|
+
},
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/ajay-vish/make-json-to-env/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/ajay-vish/make-json-to-env#readme",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^20.19.35",
|
|
30
|
+
"ts-node": "^10.9.2",
|
|
31
|
+
"typescript": "^5.9.3"
|
|
8
32
|
},
|
|
9
33
|
"keywords": [
|
|
10
34
|
"env",
|
|
11
35
|
"json",
|
|
12
36
|
"converter",
|
|
13
|
-
"
|
|
37
|
+
"recursive",
|
|
38
|
+
"flatten",
|
|
14
39
|
"dot-env",
|
|
15
40
|
"environment-variables",
|
|
16
41
|
"config-to-env",
|
|
17
42
|
"cli",
|
|
18
43
|
"devtools",
|
|
19
44
|
"automation",
|
|
20
|
-
"boilerplate",
|
|
21
45
|
"env-example",
|
|
22
46
|
"dotenv-generator",
|
|
23
47
|
"configuration",
|
|
24
48
|
"secrets-management"
|
|
25
49
|
],
|
|
26
|
-
"author": "
|
|
50
|
+
"author": "Ajay Vishwakarma",
|
|
27
51
|
"license": "MIT"
|
|
28
52
|
}
|
package/index.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
|
|
5
|
-
// 1. Parse CLI Arguments
|
|
6
|
-
const args = process.argv.slice(2);
|
|
7
|
-
const inputFile = args.find(arg => !arg.startsWith('--'));
|
|
8
|
-
const prefixArg = args.find(arg => arg.startsWith('--prefix='));
|
|
9
|
-
const prefix = prefixArg ? prefixArg.split('=')[1].replace(/['"]+/g, '') : '';
|
|
10
|
-
|
|
11
|
-
if (!inputFile) {
|
|
12
|
-
console.error('❌ Usage: convert-json-env <filename.json> [--prefix="your-prefix"]');
|
|
13
|
-
process.exit(1);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Converts JSON keys and values to ENV format
|
|
18
|
-
*/
|
|
19
|
-
function convertToEnv(json, prefix) {
|
|
20
|
-
return Object.entries(json)
|
|
21
|
-
.map(([key, value]) => {
|
|
22
|
-
const envKey = `${prefix}${key.toUpperCase()}`;
|
|
23
|
-
|
|
24
|
-
// Handle different value types
|
|
25
|
-
let envValue = value;
|
|
26
|
-
if (typeof value === 'object' && value !== null) {
|
|
27
|
-
envValue = JSON.stringify(value);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// Wrap in quotes if value contains spaces
|
|
31
|
-
if (typeof envValue === 'string' && envValue.includes(' ')) {
|
|
32
|
-
envValue = `"${envValue}"`;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return `${envKey}=${envValue}`;
|
|
36
|
-
})
|
|
37
|
-
.join('\n');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function run() {
|
|
41
|
-
const inputPath = path.resolve(process.cwd(), inputFile);
|
|
42
|
-
const parsedPath = path.parse(inputPath);
|
|
43
|
-
const outputPath = path.join(parsedPath.dir, `${parsedPath.name}.env`);
|
|
44
|
-
|
|
45
|
-
if (!fs.existsSync(inputPath)) {
|
|
46
|
-
console.error(`❌ Error: File not found: ${inputFile}`);
|
|
47
|
-
process.exit(1);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
try {
|
|
51
|
-
const fileContent = fs.readFileSync(inputPath, 'utf8');
|
|
52
|
-
const json = JSON.parse(fileContent);
|
|
53
|
-
const envContent = convertToEnv(json, prefix);
|
|
54
|
-
|
|
55
|
-
fs.writeFileSync(outputPath, envContent);
|
|
56
|
-
console.log(`✅ ${path.basename(outputPath)} created with values.`);
|
|
57
|
-
} catch (error) {
|
|
58
|
-
console.error('❌ Error:', error.message);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
run();
|