dotswitch 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 +242 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +586 -0
- package/dist/index.cjs +246 -0
- package/dist/index.d.cts +69 -0
- package/dist/index.d.mts +69 -0
- package/dist/index.mjs +202 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Stefan Natter
|
|
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,242 @@
|
|
|
1
|
+
# dotswitch
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
[](https://github.com/natterstefan/dotswitch/actions/workflows/ci.yml)
|
|
6
|
+
|
|
7
|
+
Quickly switch between `.env` files. Copies `.env.<environment>` to `.env.local` (or a custom target) and tracks the active environment via a header comment. Works with Next.js, Vite, Remix, and any project that uses `.env` files.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install -g dotswitch
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Or use directly without installing:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npx dotswitch use staging
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Switch environment
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
# Switch to .env.staging → .env.local
|
|
27
|
+
dotswitch use staging
|
|
28
|
+
|
|
29
|
+
# Interactive picker (when no env specified)
|
|
30
|
+
dotswitch use
|
|
31
|
+
|
|
32
|
+
# Skip backup of existing .env.local
|
|
33
|
+
dotswitch use production --no-backup
|
|
34
|
+
|
|
35
|
+
# Force switch even if already active
|
|
36
|
+
dotswitch use staging --force
|
|
37
|
+
|
|
38
|
+
# Preview what would happen without making changes
|
|
39
|
+
dotswitch use staging --dry-run
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### List available environments
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
dotswitch ls
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
Available environments:
|
|
50
|
+
|
|
51
|
+
▸ staging (active)
|
|
52
|
+
production
|
|
53
|
+
preview
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
JSON output for scripts:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
dotswitch ls --json
|
|
60
|
+
# [{"name":".env.staging","env":"staging","active":true},{"name":".env.production","env":"production","active":false}]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Show current environment
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
dotswitch current
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
JSON output:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
dotswitch current --json
|
|
73
|
+
# {"active":"staging"}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Pipe-friendly — outputs the plain env name when not a TTY:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
ENV=$(dotswitch current)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Restore from backup
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
# Restore .env.local from .env.local.backup
|
|
86
|
+
dotswitch restore
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Compare environments
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
# Compare current .env.local against .env.production
|
|
93
|
+
dotswitch diff production
|
|
94
|
+
|
|
95
|
+
# Compare two environments directly
|
|
96
|
+
dotswitch diff staging production
|
|
97
|
+
|
|
98
|
+
# Show actual values (not just key names)
|
|
99
|
+
dotswitch diff staging production --show-values
|
|
100
|
+
|
|
101
|
+
# JSON output
|
|
102
|
+
dotswitch diff staging production --json
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Options
|
|
106
|
+
|
|
107
|
+
All commands support:
|
|
108
|
+
|
|
109
|
+
| Flag | Description |
|
|
110
|
+
|------|-------------|
|
|
111
|
+
| `-p, --path <dir>` | Project directory (defaults to cwd) |
|
|
112
|
+
| `--json` | Output as JSON (machine-readable) |
|
|
113
|
+
|
|
114
|
+
`use` also supports:
|
|
115
|
+
|
|
116
|
+
| Flag | Description |
|
|
117
|
+
|------|-------------|
|
|
118
|
+
| `-f, --force` | Switch even if already active |
|
|
119
|
+
| `--no-backup` | Skip `.env.local` backup |
|
|
120
|
+
| `-n, --dry-run` | Preview what would happen |
|
|
121
|
+
|
|
122
|
+
## Configuration
|
|
123
|
+
|
|
124
|
+
Create a `.dotswitchrc.json` in your project root to customize behavior. Everything is optional — dotswitch works out of the box without a config file.
|
|
125
|
+
|
|
126
|
+
```json
|
|
127
|
+
{
|
|
128
|
+
"target": ".env.local",
|
|
129
|
+
"exclude": [".env.test"],
|
|
130
|
+
"hooks": {
|
|
131
|
+
"main": "production",
|
|
132
|
+
"staging/*": "staging",
|
|
133
|
+
"dev*": "development"
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
| Field | Default | Description |
|
|
139
|
+
|-------|---------|-------------|
|
|
140
|
+
| `target` | `".env.local"` | File to write the active env to |
|
|
141
|
+
| `exclude` | `[]` | Additional env files to hide from `ls` |
|
|
142
|
+
| `hooks` | `{}` | Branch-to-env mappings for git hook auto-switching |
|
|
143
|
+
|
|
144
|
+
### Custom target file
|
|
145
|
+
|
|
146
|
+
By default dotswitch writes to `.env.local`, but some frameworks use `.env` directly. Set the `target` field to change this:
|
|
147
|
+
|
|
148
|
+
```json
|
|
149
|
+
{
|
|
150
|
+
"target": ".env"
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Git hook auto-switching
|
|
155
|
+
|
|
156
|
+
Automatically switch environments when you check out a branch.
|
|
157
|
+
|
|
158
|
+
### Setup
|
|
159
|
+
|
|
160
|
+
1. Add branch mappings to `.dotswitchrc.json`:
|
|
161
|
+
|
|
162
|
+
```json
|
|
163
|
+
{
|
|
164
|
+
"hooks": {
|
|
165
|
+
"main": "production",
|
|
166
|
+
"staging/*": "staging",
|
|
167
|
+
"develop": "development"
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
2. Install the git hook:
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
dotswitch hook install
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Now `git checkout staging/feat-login` will automatically run `dotswitch use staging`.
|
|
179
|
+
|
|
180
|
+
### Patterns
|
|
181
|
+
|
|
182
|
+
- `"main"` — exact branch name match
|
|
183
|
+
- `"staging/*"` — matches `staging/` prefix (e.g., `staging/feat-x`)
|
|
184
|
+
- `"dev*"` — matches any branch starting with `dev`
|
|
185
|
+
|
|
186
|
+
### Remove the hook
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
dotswitch hook remove
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Monorepo support
|
|
193
|
+
|
|
194
|
+
Switch environments across multiple packages at once using glob patterns:
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
# Switch all apps to staging
|
|
198
|
+
dotswitch use staging --path "./apps/*"
|
|
199
|
+
|
|
200
|
+
# Check status across packages
|
|
201
|
+
dotswitch ls --path "./packages/*"
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Each directory is processed independently with labeled output.
|
|
205
|
+
|
|
206
|
+
## How it works
|
|
207
|
+
|
|
208
|
+
When you run `dotswitch use staging`, it:
|
|
209
|
+
|
|
210
|
+
1. Backs up your existing `.env.local` to `.env.local.backup`
|
|
211
|
+
2. Copies `.env.staging` to `.env.local`
|
|
212
|
+
3. Prepends a `# dotswitch:staging` header to track the active environment
|
|
213
|
+
|
|
214
|
+
The header comment is how `dotswitch ls` and `dotswitch current` know which environment is active.
|
|
215
|
+
|
|
216
|
+
## Programmatic API
|
|
217
|
+
|
|
218
|
+
dotswitch exports its core functions for use in scripts:
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
import {
|
|
222
|
+
listEnvFiles,
|
|
223
|
+
switchEnv,
|
|
224
|
+
getActiveEnv,
|
|
225
|
+
restoreEnvLocal,
|
|
226
|
+
loadConfig,
|
|
227
|
+
parseEnvContent,
|
|
228
|
+
diffEnvMaps,
|
|
229
|
+
} from "dotswitch";
|
|
230
|
+
|
|
231
|
+
const files = listEnvFiles(process.cwd());
|
|
232
|
+
const active = getActiveEnv(process.cwd());
|
|
233
|
+
switchEnv(process.cwd(), "staging", { backup: true });
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Requirements
|
|
237
|
+
|
|
238
|
+
- Node.js >= 20
|
|
239
|
+
|
|
240
|
+
## License
|
|
241
|
+
|
|
242
|
+
MIT
|
package/dist/cli.d.ts
ADDED