make-json-to-env 1.0.0
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/index.js +46 -0
- package/package.json +12 -0
package/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
function convertToEnv(json, prefix) {
|
|
17
|
+
return Object.keys(json)
|
|
18
|
+
.map(key => `${prefix}${key.toUpperCase()}=`)
|
|
19
|
+
.join('\n');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function run() {
|
|
23
|
+
const inputPath = path.resolve(process.cwd(), inputFile);
|
|
24
|
+
|
|
25
|
+
// Generate output name: example.json -> example.env
|
|
26
|
+
const parsedPath = path.parse(inputPath);
|
|
27
|
+
const outputPath = path.join(parsedPath.dir, `${parsedPath.name}.env`);
|
|
28
|
+
|
|
29
|
+
if (!fs.existsSync(inputPath)) {
|
|
30
|
+
console.error(`❌ Error: File not found: ${inputFile}`);
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
const fileContent = fs.readFileSync(inputPath, 'utf8');
|
|
36
|
+
const json = JSON.parse(fileContent);
|
|
37
|
+
const envContent = convertToEnv(json, prefix);
|
|
38
|
+
|
|
39
|
+
fs.writeFileSync(outputPath, envContent);
|
|
40
|
+
console.log(`✅ ${path.basename(outputPath)} created.`);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error('❌ Error:', error.message);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
run();
|
package/package.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "make-json-to-env",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Generates a template .env.example from a config.json file",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"make-json-to-env": "./index.js"
|
|
8
|
+
},
|
|
9
|
+
"keywords": ["env", "json", "converter", "template"],
|
|
10
|
+
"author": "Your Name",
|
|
11
|
+
"license": "MIT"
|
|
12
|
+
}
|