opencode-dotenv 0.3.8 → 0.5.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.
Files changed (3) hide show
  1. package/README.md +173 -27
  2. package/dist/index.js +1024 -316
  3. package/package.json +26 -28
package/README.md CHANGED
@@ -2,9 +2,40 @@
2
2
 
3
3
  OpenCode plugin to load `.env` files at startup.
4
4
 
5
- ## Setup
5
+ ## Features
6
+
7
+ - Load multiple `.env` files in order via config file
8
+ - Load `.env` from current working directory (optional)
9
+ - Override existing environment variables (later files override earlier ones)
10
+ - Configurable logging to `/tmp/opencode-dotenv.log` (enabled by default)
11
+ - Prevents double loading with load guard
12
+ - JSONC config file format (supports comments and trailing commas)
13
+ - **Requires Bun runtime**
14
+
15
+ ## Limitations
6
16
 
7
- Add to `~/.config/opencode/opencode.jsonc`:
17
+ **Important:** This plugin loads AFTER OpenCode configuration is already parsed. Therefore:
18
+
19
+ 1. **Cannot modify existing OpenCode config** - Variables loaded by this plugin cannot be referenced in `opencode.jsonc` using `{env:VAR}` syntax. OpenCode reads its config before plugins initialize.
20
+
21
+ 2. **Only affects subsequent operations** - Loaded environment variables are only available to new chat sessions, tool calls, and operations that occur AFTER plugin initialization.
22
+
23
+ 3. **No config reload capability** - Changing `.env` files while OpenCode is running will NOT trigger config re-parsing. To apply changes, restart OpenCode.
24
+
25
+ **Recommended approach:** Set environment variables in your shell profile (`.zshrc`, `.bashrc`) before starting OpenCode.
26
+
27
+ ## Installation
28
+
29
+ Add to your `opencode.jsonc`:
30
+
31
+ ```jsonc
32
+ {
33
+ "$schema": "https://opencode.ai/config.json",
34
+ "plugin": ["file:./plugins/opencode-dotenv"]
35
+ }
36
+ ```
37
+
38
+ After publishing to npm, you can use:
8
39
 
9
40
  ```jsonc
10
41
  {
@@ -12,51 +43,166 @@ Add to `~/.config/opencode/opencode.jsonc`:
12
43
  }
13
44
  ```
14
45
 
15
- ## Config
46
+ ## Configuration
47
+
48
+ Create `opencode-dotenv.jsonc` in one of these locations:
49
+
50
+ 1. `~/.config/opencode/opencode-dotenv.jsonc` (recommended, global config)
51
+ 2. `./opencode-dotenv.jsonc` in current working directory (project-specific)
16
52
 
17
- Create `~/.config/opencode/opencode-dotenv.jsonc`:
53
+ **Note:** Config files are loaded in the order above; the first found file is used.
54
+
55
+ ### Config Schema
56
+
57
+ Config file uses **JSONC format** (JSON with Comments), which supports:
58
+ - `//` single-line comments
59
+ - `/* */` multi-line comments
60
+ - Trailing commas
61
+ - Trailing spaces
18
62
 
19
63
  ```jsonc
20
64
  {
21
- "files": ["~/.config/opencode/.env"],
65
+ "files": [
66
+ "~/.config/opencode/.env",
67
+ "~/a/.env"
68
+ ],
22
69
  "load_cwd_env": true,
23
- "prefix": "", // or "MYAPP_"
24
- "logging": { "enabled": true }
70
+ "logging": {
71
+ "enabled": true
72
+ }
73
+ }
74
+ ```
75
+
76
+ **Fields:**
77
+ - `files` (array, optional): List of `.env` file paths to load in order. Later files override earlier ones.
78
+ - `load_cwd_env` (boolean, optional): Whether to load `.env` from the directory where OpenCode is opened. Defaults to `true`.
79
+ - `logging.enabled` (boolean, optional): Enable/disable logging to `/tmp/opencode-dotenv.log`. Defaults to `true`.
80
+
81
+ **Notes:**
82
+ - Use `~` for home directory (automatically expanded)
83
+ - Paths are expanded before loading
84
+ - If no config file exists, only loads `./.env` from cwd (if present)
85
+ - Logging writes to `/tmp/opencode-dotenv.log` for debugging
86
+
87
+ ### Load Order
88
+
89
+ 1. Files listed in `config.files` array (in order, later files override earlier ones)
90
+ 2. `.env` from current working directory (if `load_cwd_env: true`)
91
+
92
+ This ensures project-specific env vars have the highest precedence.
93
+
94
+ ## Usage Examples
95
+
96
+ ### Load global and project-specific .env files
97
+
98
+ Config (`~/.config/opencode/opencode-dotenv.jsonc`):
99
+
100
+ ```jsonc
101
+ {
102
+ "files": [
103
+ "~/.config/opencode/.env"
104
+ ],
105
+ "load_cwd_env": true,
106
+ "logging": {
107
+ "enabled": true
108
+ }
109
+ }
110
+ ```
111
+
112
+ Result:
113
+ 1. Loads `~/.config/opencode/.env`
114
+ 2. Loads `./.env` from cwd (overrides any conflicts)
115
+ 3. Logs all activity to `/tmp/opencode-dotenv.log`
116
+
117
+ ### Load multiple global files without cwd .env
118
+
119
+ Config (`~/.config/opencode/opencode-dotenv.jsonc`):
120
+
121
+ ```jsonc
122
+ {
123
+ "files": [
124
+ "~/.config/opencode/.env",
125
+ "~/a/.env"
126
+ ],
127
+ "load_cwd_env": false,
128
+ "logging": {
129
+ "enabled": false
130
+ }
25
131
  }
26
132
  ```
27
133
 
28
- | Option | Default | Description |
29
- |--------|---------|-------------|
30
- | `files` | `[]` | `.env` files to load (later overrides earlier) |
31
- | `load_cwd_env` | `true` | Load `.env` from cwd |
32
- | `prefix` | `""` | Prefix for all variable names |
33
- | `logging.enabled` | `true` | Log to `/tmp/opencode-dotenv.log` |
134
+ Result:
135
+ 1. Loads `~/.config/opencode/.env`
136
+ 2. Loads `~/a/.env` (overrides conflicts from first file)
137
+ 3. Skips cwd `.env`
138
+ 4. No logging output
34
139
 
35
- ## .env Format
140
+ ### Example .env files
141
+
142
+ `~/.config/opencode/.env`:
143
+
144
+ ```bash
145
+ # OpenCode Dotenv Configuration
146
+ OPENCODE_API_KEY=your_api_key_here
147
+ OPENCODE_DEBUG=true
148
+ OPENCODE_MAX_TOKENS=100000
149
+ ```
150
+
151
+ **Note:** This plugin cannot inject these variables into OpenCode's configuration loading process. To use `OPENCODE_API_KEY` in `opencode.jsonc`, set it in your shell profile (`~/.zshrc`, `~/.bashrc`) before starting OpenCode.
36
152
 
153
+ `./.env` (project-specific):
37
154
  ```bash
38
- KEY=value
39
- export EXPORTED=value
40
- QUOTED="with spaces"
41
- SINGLE='literal'
42
- MULTILINE=first\
43
- second
44
- INLINE=value # comment stripped
45
- ESCAPES="line1\nline2\ttab"
155
+ # Project-specific overrides
156
+ OPENCODE_DEBUG=false
157
+ PROJECT_API_KEY=project_specific_key
46
158
  ```
47
159
 
48
- ## Security
160
+ Result: `OPENCODE_DEBUG` will be `false` (from cwd), `OPENCODE_API_KEY` from global, `PROJECT_API_KEY` from cwd.
49
161
 
50
- - Paths restricted to `$HOME` or cwd
51
- - Path traversal rejected
52
- - Keys validated: `^[a-zA-Z_][a-zA-Z0-9_]*$`
162
+ ### Logging
53
163
 
54
- ## Debug
164
+ View plugin activity logs:
55
165
 
56
166
  ```bash
57
167
  tail -f /tmp/opencode-dotenv.log
58
168
  ```
59
169
 
170
+ Disable logging in config:
171
+
172
+ ```jsonc
173
+ {
174
+ "files": ["~/.config/opencode/.env"],
175
+ "logging": {
176
+ "enabled": false
177
+ }
178
+ }
179
+ ```
180
+
181
+ ## Development
182
+
183
+ ### Plugin structure
184
+
185
+ ```
186
+ opencode-dotenv/
187
+ ├── package.json
188
+ ├── src/
189
+ │ └── index.ts
190
+ └── dist/
191
+ └── index.js (built)
192
+ ```
193
+
194
+ ### Build
195
+
196
+ ```bash
197
+ bun run build
198
+ ```
199
+
200
+ ### Publish
201
+
202
+ ```bash
203
+ npm publish
204
+ ```
205
+
60
206
  ## License
61
207
 
62
208
  MIT