opencode-dotenv 0.3.7 → 0.4.1

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 +160 -27
  2. package/dist/index.js +842 -292
  3. package/package.json +3 -27
package/README.md CHANGED
@@ -2,9 +2,28 @@
2
2
 
3
3
  OpenCode plugin to load `.env` files at startup.
4
4
 
5
- ## Setup
5
+ ## Features
6
6
 
7
- Add to `~/.config/opencode/opencode.jsonc`:
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
+ ## Installation
16
+
17
+ Add to your `opencode.jsonc`:
18
+
19
+ ```jsonc
20
+ {
21
+ "$schema": "https://opencode.ai/config.json",
22
+ "plugin": ["file:./plugins/opencode-dotenv"]
23
+ }
24
+ ```
25
+
26
+ After publishing to npm, you can use:
8
27
 
9
28
  ```jsonc
10
29
  {
@@ -12,51 +31,165 @@ Add to `~/.config/opencode/opencode.jsonc`:
12
31
  }
13
32
  ```
14
33
 
15
- ## Config
34
+ ## Configuration
16
35
 
17
- Create `~/.config/opencode/opencode-dotenv.jsonc`:
36
+ Create `opencode-dotenv.jsonc` in one of these locations:
37
+
38
+ 1. `~/.config/opencode/opencode-dotenv.jsonc` (recommended, global config)
39
+ 2. `./opencode-dotenv.jsonc` in current working directory (project-specific)
40
+
41
+ **Note:** Config files are loaded in the order above; the first found file is used.
42
+
43
+ ### Config Schema
44
+
45
+ Config file uses **JSONC format** (JSON with Comments), which supports:
46
+ - `//` single-line comments
47
+ - `/* */` multi-line comments
48
+ - Trailing commas
49
+ - Trailing spaces
18
50
 
19
51
  ```jsonc
20
52
  {
21
- "files": ["~/.config/opencode/.env"],
53
+ "files": [
54
+ "~/.config/opencode/.env",
55
+ "~/a/.env"
56
+ ],
57
+ "load_cwd_env": true,
58
+ "logging": {
59
+ "enabled": true
60
+ }
61
+ }
62
+ ```
63
+
64
+ **Fields:**
65
+ - `files` (array, optional): List of `.env` file paths to load in order. Later files override earlier ones.
66
+ - `load_cwd_env` (boolean, optional): Whether to load `.env` from the directory where OpenCode is opened. Defaults to `true`.
67
+ - `logging.enabled` (boolean, optional): Enable/disable logging to `/tmp/opencode-dotenv.log`. Defaults to `true`.
68
+
69
+ **Notes:**
70
+ - Use `~` for home directory (automatically expanded)
71
+ - Paths are expanded before loading
72
+ - If no config file exists, only loads `./.env` from cwd (if present)
73
+ - Logging writes to `/tmp/opencode-dotenv.log` for debugging
74
+
75
+ ### Load Order
76
+
77
+ 1. Files listed in `config.files` array (in order, later files override earlier ones)
78
+ 2. `.env` from current working directory (if `load_cwd_env: true`)
79
+
80
+ This ensures project-specific env vars have the highest precedence.
81
+
82
+ ## Usage Examples
83
+
84
+ ### Load global and project-specific .env files
85
+
86
+ Config (`~/.config/opencode/opencode-dotenv.jsonc`):
87
+
88
+ ```jsonc
89
+ {
90
+ "files": [
91
+ "~/.config/opencode/.env"
92
+ ],
22
93
  "load_cwd_env": true,
23
- "prefix": "", // or "MYAPP_"
24
- "logging": { "enabled": true }
94
+ "logging": {
95
+ "enabled": true
96
+ }
97
+ }
98
+ ```
99
+
100
+ Result:
101
+ 1. Loads `~/.config/opencode/.env`
102
+ 2. Loads `./.env` from cwd (overrides any conflicts)
103
+ 3. Logs all activity to `/tmp/opencode-dotenv.log`
104
+
105
+ ### Load multiple global files without cwd .env
106
+
107
+ Config (`~/.config/opencode/opencode-dotenv.jsonc`):
108
+
109
+ ```jsonc
110
+ {
111
+ "files": [
112
+ "~/.config/opencode/.env",
113
+ "~/a/.env"
114
+ ],
115
+ "load_cwd_env": false,
116
+ "logging": {
117
+ "enabled": false
118
+ }
25
119
  }
26
120
  ```
27
121
 
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` |
122
+ Result:
123
+ 1. Loads `~/.config/opencode/.env`
124
+ 2. Loads `~/a/.env` (overrides conflicts from first file)
125
+ 3. Skips cwd `.env`
126
+ 4. No logging output
127
+
128
+ ### Example .env files
34
129
 
35
- ## .env Format
130
+ `~/.config/opencode/.env`:
36
131
 
37
132
  ```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"
133
+ # OpenCode Dotenv Configuration
134
+ OPENCODE_API_KEY=your_api_key_here
135
+ OPENCODE_DEBUG=true
136
+ OPENCODE_MAX_TOKENS=100000
46
137
  ```
47
138
 
48
- ## Security
139
+ `./.env` (project-specific):
49
140
 
50
- - Paths restricted to `$HOME` or cwd
51
- - Path traversal rejected
52
- - Keys validated: `^[a-zA-Z_][a-zA-Z0-9_]*$`
141
+ ```bash
142
+ # Project-specific overrides
143
+ OPENCODE_DEBUG=false
144
+ PROJECT_API_KEY=project_specific_key
145
+ ```
53
146
 
54
- ## Debug
147
+ Result: `OPENCODE_DEBUG` will be `false` (from cwd), `OPENCODE_API_KEY` from global, `PROJECT_API_KEY` from cwd.
148
+
149
+ ### Logging
150
+
151
+ View plugin activity logs:
55
152
 
56
153
  ```bash
57
154
  tail -f /tmp/opencode-dotenv.log
58
155
  ```
59
156
 
157
+ Disable logging in config:
158
+
159
+ ```jsonc
160
+ {
161
+ "files": ["~/.config/opencode/.env"],
162
+ "logging": {
163
+ "enabled": false
164
+ }
165
+ }
166
+ ```
167
+
168
+ ## Development
169
+
170
+ ### Plugin structure
171
+
172
+ ```
173
+ opencode-dotenv/
174
+ ├── package.json
175
+ ├── src/
176
+ │ └── index.ts
177
+ └── dist/
178
+ └── index.js (built)
179
+ ```
180
+
181
+ ### Build
182
+
183
+ ```bash
184
+ bun run build
185
+ ```
186
+
187
+ ### Publish
188
+
189
+ ```bash
190
+ npm publish
191
+ ```
192
+
60
193
  ## License
61
194
 
62
195
  MIT