env-creator 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/LICENSE +21 -0
- package/README.md +161 -0
- package/dist/cli.js +2 -0
- package/dist/index.js +8 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kyrylo Voronoi
|
|
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
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# env-creator
|
|
2
|
+
|
|
3
|
+
A fast, lightweight CLI tool for managing and generating environment (`.env`) files.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
You can install `env-creator` globally using npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g env-creator
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Alternatively, you can run it directly using `npx` without installing globally:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npx create-env <command> [options]
|
|
17
|
+
# or
|
|
18
|
+
npx env-creator <command> [options]
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`env-creator` (also available as `create-env`) provides several commands to help you quickly set up or split your environment files.
|
|
22
|
+
|
|
23
|
+
### 1. Create an empty or pre-filled `.env` file
|
|
24
|
+
|
|
25
|
+
Generates a `.env` file in the current working directory. You can optionally pass `KEY=VALUE` pairs to pre-fill it. If a file already exists, it will not overwrite it.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npx create-env create
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**With initial fields:**
|
|
32
|
+
```bash
|
|
33
|
+
npx create-env create PORT=3000 NODE_ENV=development
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
**Resulting `.env`:**
|
|
37
|
+
```env
|
|
38
|
+
PORT=3000
|
|
39
|
+
NODE_ENV=development
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 2. Create from JSON
|
|
43
|
+
|
|
44
|
+
Generates a `.env` target file from a provided JSON file. The keys and values in the JSON file will be converted into `KEY=VALUE` format.
|
|
45
|
+
You can optionally provide an `--env <name>` flag to create a specific `.env.<name>` file (e.g., `.env.production`).
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npx create-env create-from-json <path-to-file.json> [--env <name>]
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
**Example `config.json`:**
|
|
52
|
+
```json
|
|
53
|
+
{
|
|
54
|
+
"PORT": 3000,
|
|
55
|
+
"DB_HOST": "localhost",
|
|
56
|
+
"NODE_ENV": "development"
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**Command:**
|
|
61
|
+
```bash
|
|
62
|
+
npx create-env create-from-json config.json --env production
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
**Resulting `.env.production`:**
|
|
66
|
+
```env
|
|
67
|
+
PORT=3000
|
|
68
|
+
DB_HOST=localhost
|
|
69
|
+
NODE_ENV=development
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 3. Split `.env` for specific environments
|
|
73
|
+
|
|
74
|
+
Reads your existing `.env` file, removes all comments and values, and creates a new target file containing **only the keys** (e.g., for creating a `.env.example` or `.env.production` template).
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
npx create-env split --env <env-name>
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Example:**
|
|
81
|
+
If you have a `.env` file like this:
|
|
82
|
+
```env
|
|
83
|
+
# Database Config
|
|
84
|
+
DB_USER=admin
|
|
85
|
+
DB_PASS=secret
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Running the split command for production:
|
|
89
|
+
```bash
|
|
90
|
+
npx create-env split --env production
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Will generate a new file named `.env.production` with empty values:
|
|
94
|
+
```env
|
|
95
|
+
DB_USER=
|
|
96
|
+
DB_PASS=
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 4. Delete an `.env` file
|
|
100
|
+
|
|
101
|
+
Deletes a specific environment file. If no filename is provided, it defaults to deleting `.env`.
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
npx create-env delete [file]
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
**Examples:**
|
|
108
|
+
```bash
|
|
109
|
+
npx create-env delete # Deletes .env
|
|
110
|
+
npx create-env delete .env.production # Deletes .env.production
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### 5. Sort `.env` keys alphabetically
|
|
114
|
+
|
|
115
|
+
Reads an environment file and reorders all `KEY=VALUE` lines alphabetically. Comments and empty lines are preserved at the top of the file. Defaults to `.env` if no file is specified.
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
npx create-env sort [file]
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
**Examples:**
|
|
122
|
+
```bash
|
|
123
|
+
npx create-env sort # Sorts .env
|
|
124
|
+
npx create-env sort .env.production # Sorts .env.production
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**Before:**
|
|
128
|
+
```env
|
|
129
|
+
# App config
|
|
130
|
+
PORT=3000
|
|
131
|
+
APP_NAME=my-app
|
|
132
|
+
DB_HOST=localhost
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
**After:**
|
|
136
|
+
```env
|
|
137
|
+
# App config
|
|
138
|
+
APP_NAME=my-app
|
|
139
|
+
DB_HOST=localhost
|
|
140
|
+
PORT=3000
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Development
|
|
144
|
+
|
|
145
|
+
If you want to contribute or modify the tool, you can clone the repository and use the built-in npm scripts:
|
|
146
|
+
|
|
147
|
+
### Linting
|
|
148
|
+
To check the code for syntax and style errors using ESLint:
|
|
149
|
+
```bash
|
|
150
|
+
npm run lint
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Building
|
|
154
|
+
The project uses `esbuild` to bundle and minify the code into a single executable file in the `dist/` directory:
|
|
155
|
+
```bash
|
|
156
|
+
npm run build
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## License
|
|
160
|
+
|
|
161
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
package/dist/cli.js
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import n from"fs";import f from"path";var t=process.argv.slice(2);t.length===0&&(p(),process.exit(0));function p(){console.log("Usage: env-creator <command> [options]"),console.log("Commands:"),console.log(" create [KEY=value...] Create a .env file (optionally with values)"),console.log(" create-from-json <json> [--env <name>] Create .env or .env.<name> from JSON"),console.log(" split --env <dev|prod> Create environment-specific file from .env"),console.log(" delete [file] Delete an environment file (default: .env)"),console.log(" sort [file] Sort keys alphabetically in an env file (default: .env)"),console.log("Options:"),console.log(" -h, --help Show this help message")}var d=t[0];(d==="--help"||d==="-h"||d==="help")&&(p(),process.exit(0));switch(d){case"create":{let e=f.join(process.cwd(),".env");if(n.existsSync(e))console.log(".env already exists");else{let o=t.slice(1),s="";if(o.length>0){let i=o.filter(c=>c.includes("="));s=i.join(`
|
|
2
|
+
`)+(i.length>0?`
|
|
3
|
+
`:"")}n.writeFileSync(e,s),console.log(s?"Created .env with specified fields":"Created empty .env")}break}case"create-from-json":{let e=t[1];e||(console.error("Please provide a JSON file"),process.exit(1)),n.existsSync(e)||(console.error("JSON file not found"),process.exit(1));let o=t.indexOf("--env"),i=`.env${o!==-1&&t[o+1]?`.${t[o+1]}`:""}`;if(n.existsSync(i)){console.log(`${i} already exists`);break}let c=JSON.parse(n.readFileSync(e,"utf-8")),a="";for(let r in c)a+=`${r}=${c[r]}
|
|
4
|
+
`;n.writeFileSync(i,a),console.log(`Created ${i} from JSON`);break}case"split":{let e=t.indexOf("--env");(e===-1||!t[e+1])&&(console.error("Please specify environment with --env <dev|prod>"),process.exit(1));let o=t[e+1],s=f.join(process.cwd(),".env");n.existsSync(s)||(console.error(".env file not found"),process.exit(1));let c=n.readFileSync(s,"utf-8").split(/\r?\n/).filter(r=>r.trim()!==""&&!r.startsWith("#")).map(r=>r.split("=")[0]+"=").join(`
|
|
5
|
+
`),a=f.join(process.cwd(),`.env.${o}`);if(n.existsSync(a)){console.log(`.env.${o} already exists`);break}n.writeFileSync(a,c),console.log(`Created .env.${o} with keys only`);break}case"delete":{let e=t[1]||".env",o=f.join(process.cwd(),e);n.existsSync(o)?(n.unlinkSync(o),console.log(`Deleted ${e}`)):console.log(`File ${e} does not exist`);break}case"sort":{let e=t[1]||".env",o=f.join(process.cwd(),e);n.existsSync(o)||(console.error(`File ${e} does not exist`),process.exit(1));let s=n.readFileSync(o,"utf-8").split(/\r?\n/),i=s[s.length-1]===""?s.slice(0,-1):s,c=i.filter(l=>!l.startsWith("#")&&l.trim()!==""&&l.includes("=")).sort((l,v)=>l.localeCompare(v)),a=0,r=i.map(l=>!l.startsWith("#")&&l.trim()!==""&&l.includes("=")?c[a++]:l);n.writeFileSync(o,r.join(`
|
|
6
|
+
`)+`
|
|
7
|
+
`),console.log(`Sorted keys in ${e}`);break}default:console.error(`Unknown command: "${d}"
|
|
8
|
+
`),p(),process.exit(1)}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "env-creator",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Environment variable generator CLI",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"env-creator": "./dist/cli.js",
|
|
8
|
+
"create-env": "./dist/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=20"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"lint": "eslint .",
|
|
21
|
+
"build": "esbuild src/index.js --bundle --platform=node --format=esm --target=node20 --minify --outfile=dist/index.js && printf '#!/usr/bin/env node\nimport(\"./index.js\");\n' > dist/cli.js && chmod +x dist/cli.js",
|
|
22
|
+
"prepublishOnly": "npm run lint && npm run build",
|
|
23
|
+
"release": "npm version patch && npm publish"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"env",
|
|
27
|
+
"dotenv",
|
|
28
|
+
"cli",
|
|
29
|
+
"environment",
|
|
30
|
+
"generator"
|
|
31
|
+
],
|
|
32
|
+
"author": "Kyrylo Voronoi",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/kyrylovoronoi/env-creator.git"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/kyrylovoronoi/env-creator",
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@eslint/js": "^10.0.1",
|
|
41
|
+
"esbuild": "^0.27.3",
|
|
42
|
+
"eslint": "^10.0.3",
|
|
43
|
+
"globals": "^17.4.0"
|
|
44
|
+
}
|
|
45
|
+
}
|