convex-env-sync 0.1.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 +15 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +334 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Johannes Schießl
|
|
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,15 @@
|
|
|
1
|
+
# convex-env-sync
|
|
2
|
+
|
|
3
|
+
To install dependencies:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bun install
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
To run:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bun run index.ts
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This project was created using `bun init` in bun v1.3.5. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn } from "child_process";
|
|
3
|
+
import { watch, existsSync, readFileSync, writeFileSync } from "fs";
|
|
4
|
+
import { resolve } from "path";
|
|
5
|
+
const DELETE_MARKER = "__DELETE__";
|
|
6
|
+
function parseArgs() {
|
|
7
|
+
const args = process.argv.slice(2);
|
|
8
|
+
const options = {
|
|
9
|
+
file: ".env.convex",
|
|
10
|
+
once: false,
|
|
11
|
+
writeBack: true,
|
|
12
|
+
poll: 300,
|
|
13
|
+
};
|
|
14
|
+
for (let i = 0; i < args.length; i++) {
|
|
15
|
+
const arg = args[i];
|
|
16
|
+
const nextArg = args[i + 1];
|
|
17
|
+
if (arg === "--file" && nextArg) {
|
|
18
|
+
options.file = nextArg;
|
|
19
|
+
i++;
|
|
20
|
+
}
|
|
21
|
+
else if (arg === "--once") {
|
|
22
|
+
options.once = true;
|
|
23
|
+
}
|
|
24
|
+
else if (arg === "--no-write-back") {
|
|
25
|
+
options.writeBack = false;
|
|
26
|
+
}
|
|
27
|
+
else if (arg === "--poll" && nextArg) {
|
|
28
|
+
options.poll = parseInt(nextArg, 10);
|
|
29
|
+
i++;
|
|
30
|
+
}
|
|
31
|
+
else if (arg === "--help" || arg === "-h") {
|
|
32
|
+
console.log(`
|
|
33
|
+
convex-env-sync - Sync environment variables with Convex
|
|
34
|
+
|
|
35
|
+
Usage: convex-env-sync [options]
|
|
36
|
+
|
|
37
|
+
Options:
|
|
38
|
+
--file <path> Path to env file (default: .env.convex)
|
|
39
|
+
--once Run a single sync and exit
|
|
40
|
+
--no-write-back Prevent updating local file from remote changes
|
|
41
|
+
--poll <seconds> Polling interval for remote changes (default: 300)
|
|
42
|
+
-h, --help Show this help message
|
|
43
|
+
|
|
44
|
+
Special values:
|
|
45
|
+
__DELETE__ Set a variable to this value to delete it from Convex
|
|
46
|
+
`);
|
|
47
|
+
process.exit(0);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return options;
|
|
51
|
+
}
|
|
52
|
+
function parseEnvFile(content) {
|
|
53
|
+
const vars = {};
|
|
54
|
+
const lines = content.split("\n");
|
|
55
|
+
for (const line of lines) {
|
|
56
|
+
const trimmed = line.trim();
|
|
57
|
+
if (!trimmed || trimmed.startsWith("#"))
|
|
58
|
+
continue;
|
|
59
|
+
const eqIndex = trimmed.indexOf("=");
|
|
60
|
+
if (eqIndex === -1)
|
|
61
|
+
continue;
|
|
62
|
+
const key = trimmed.slice(0, eqIndex).trim();
|
|
63
|
+
let value = trimmed.slice(eqIndex + 1).trim();
|
|
64
|
+
// Handle quoted values
|
|
65
|
+
if ((value.startsWith('"') && value.endsWith('"')) ||
|
|
66
|
+
(value.startsWith("'") && value.endsWith("'"))) {
|
|
67
|
+
value = value.slice(1, -1);
|
|
68
|
+
}
|
|
69
|
+
if (key) {
|
|
70
|
+
vars[key] = value;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return vars;
|
|
74
|
+
}
|
|
75
|
+
function serializeEnvFile(vars) {
|
|
76
|
+
return (Object.entries(vars)
|
|
77
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
78
|
+
.map(([key, value]) => {
|
|
79
|
+
// Quote values that contain spaces or special characters
|
|
80
|
+
if (value.includes(" ") || value.includes('"') || value.includes("=")) {
|
|
81
|
+
value = `"${value.replace(/"/g, '\\"')}"`;
|
|
82
|
+
}
|
|
83
|
+
return `${key}=${value}`;
|
|
84
|
+
})
|
|
85
|
+
.join("\n") + "\n");
|
|
86
|
+
}
|
|
87
|
+
function runConvexCommand(args) {
|
|
88
|
+
return new Promise((resolve, reject) => {
|
|
89
|
+
const proc = spawn("npx", ["convex", ...args], {
|
|
90
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
91
|
+
shell: process.platform === "win32",
|
|
92
|
+
});
|
|
93
|
+
let stdout = "";
|
|
94
|
+
let stderr = "";
|
|
95
|
+
proc.stdout.on("data", (data) => {
|
|
96
|
+
stdout += data.toString();
|
|
97
|
+
});
|
|
98
|
+
proc.stderr.on("data", (data) => {
|
|
99
|
+
stderr += data.toString();
|
|
100
|
+
});
|
|
101
|
+
proc.on("close", (code) => {
|
|
102
|
+
if (code === 0) {
|
|
103
|
+
resolve(stdout);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
reject(new Error(`Convex command failed: ${stderr || stdout}`));
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
proc.on("error", (err) => {
|
|
110
|
+
reject(err);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
async function getRemoteVars() {
|
|
115
|
+
const output = await runConvexCommand(["env", "list"]);
|
|
116
|
+
const vars = {};
|
|
117
|
+
// Parse the output from `convex env list`
|
|
118
|
+
// Format is typically: VAR_NAME=value or VAR_NAME="value"
|
|
119
|
+
const lines = output.split("\n");
|
|
120
|
+
for (const line of lines) {
|
|
121
|
+
const trimmed = line.trim();
|
|
122
|
+
if (!trimmed)
|
|
123
|
+
continue;
|
|
124
|
+
const eqIndex = trimmed.indexOf("=");
|
|
125
|
+
if (eqIndex === -1)
|
|
126
|
+
continue;
|
|
127
|
+
const key = trimmed.slice(0, eqIndex).trim();
|
|
128
|
+
let value = trimmed.slice(eqIndex + 1).trim();
|
|
129
|
+
// Handle quoted values
|
|
130
|
+
if ((value.startsWith('"') && value.endsWith('"')) ||
|
|
131
|
+
(value.startsWith("'") && value.endsWith("'"))) {
|
|
132
|
+
value = value.slice(1, -1);
|
|
133
|
+
}
|
|
134
|
+
if (key) {
|
|
135
|
+
vars[key] = value;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return vars;
|
|
139
|
+
}
|
|
140
|
+
async function setRemoteVar(key, value) {
|
|
141
|
+
await runConvexCommand(["env", "set", key, value]);
|
|
142
|
+
console.log(` ✓ Set ${key}`);
|
|
143
|
+
}
|
|
144
|
+
async function deleteRemoteVar(key) {
|
|
145
|
+
await runConvexCommand(["env", "remove", key]);
|
|
146
|
+
console.log(` ✓ Deleted ${key}`);
|
|
147
|
+
}
|
|
148
|
+
function readLocalVars(filePath) {
|
|
149
|
+
if (!existsSync(filePath)) {
|
|
150
|
+
return {};
|
|
151
|
+
}
|
|
152
|
+
const content = readFileSync(filePath, "utf-8");
|
|
153
|
+
return parseEnvFile(content);
|
|
154
|
+
}
|
|
155
|
+
function writeLocalVars(filePath, vars) {
|
|
156
|
+
writeFileSync(filePath, serializeEnvFile(vars));
|
|
157
|
+
}
|
|
158
|
+
let lastKnownRemoteVars = {};
|
|
159
|
+
let lastKnownLocalVars = {};
|
|
160
|
+
let isSyncing = false;
|
|
161
|
+
async function sync(options, isInitialSync) {
|
|
162
|
+
if (isSyncing) {
|
|
163
|
+
console.log("Sync already in progress, skipping...");
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
isSyncing = true;
|
|
167
|
+
const filePath = resolve(options.file);
|
|
168
|
+
try {
|
|
169
|
+
console.log(`\n[${new Date().toLocaleTimeString()}] Starting sync...`);
|
|
170
|
+
// Get current state
|
|
171
|
+
const remoteVars = await getRemoteVars();
|
|
172
|
+
const localVars = readLocalVars(filePath);
|
|
173
|
+
const fileExists = existsSync(filePath);
|
|
174
|
+
// On initial sync with no local file, pull everything from remote
|
|
175
|
+
if (isInitialSync && !fileExists) {
|
|
176
|
+
console.log("Initial sync: pulling remote variables to local file...");
|
|
177
|
+
if (Object.keys(remoteVars).length > 0) {
|
|
178
|
+
writeLocalVars(filePath, remoteVars);
|
|
179
|
+
console.log(` ✓ Created ${options.file} with ${Object.keys(remoteVars).length} variables`);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
writeLocalVars(filePath, {});
|
|
183
|
+
console.log(` ✓ Created empty ${options.file}`);
|
|
184
|
+
}
|
|
185
|
+
lastKnownRemoteVars = { ...remoteVars };
|
|
186
|
+
lastKnownLocalVars = { ...remoteVars };
|
|
187
|
+
console.log("Sync complete.");
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
// Track what needs to change
|
|
191
|
+
const toSetRemote = {};
|
|
192
|
+
const toDeleteRemote = [];
|
|
193
|
+
let updatedLocalVars = { ...localVars };
|
|
194
|
+
let localNeedsWrite = false;
|
|
195
|
+
// Process local variables -> push to remote
|
|
196
|
+
for (const [key, value] of Object.entries(localVars)) {
|
|
197
|
+
if (value === DELETE_MARKER) {
|
|
198
|
+
// User wants to delete this variable
|
|
199
|
+
if (key in remoteVars) {
|
|
200
|
+
toDeleteRemote.push(key);
|
|
201
|
+
}
|
|
202
|
+
// Remove from local vars
|
|
203
|
+
delete updatedLocalVars[key];
|
|
204
|
+
localNeedsWrite = true;
|
|
205
|
+
}
|
|
206
|
+
else if (remoteVars[key] !== value) {
|
|
207
|
+
// Variable is new or changed locally
|
|
208
|
+
toSetRemote[key] = value;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
// Process remote variables -> sync missing vars back to local
|
|
212
|
+
if (options.writeBack) {
|
|
213
|
+
// Add remote variables that are missing locally (re-sync from remote)
|
|
214
|
+
for (const [key, value] of Object.entries(remoteVars)) {
|
|
215
|
+
if (!(key in localVars) && !(key in toSetRemote)) {
|
|
216
|
+
// Variable exists in Convex but not locally - add it back
|
|
217
|
+
updatedLocalVars[key] = value;
|
|
218
|
+
localNeedsWrite = true;
|
|
219
|
+
console.log(` ← Synced from remote: ${key}`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// Check for remote deletions (was in lastKnownRemoteVars but not in current remote)
|
|
223
|
+
for (const key of Object.keys(lastKnownRemoteVars)) {
|
|
224
|
+
if (!(key in remoteVars) && key in localVars && localVars[key] !== DELETE_MARKER) {
|
|
225
|
+
// Variable was deleted remotely
|
|
226
|
+
delete updatedLocalVars[key];
|
|
227
|
+
localNeedsWrite = true;
|
|
228
|
+
console.log(` ← Remote deletion detected: ${key}`);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
// Apply remote changes
|
|
233
|
+
for (const [key, value] of Object.entries(toSetRemote)) {
|
|
234
|
+
await setRemoteVar(key, value);
|
|
235
|
+
}
|
|
236
|
+
for (const key of toDeleteRemote) {
|
|
237
|
+
await deleteRemoteVar(key);
|
|
238
|
+
}
|
|
239
|
+
// Write back to local file if needed
|
|
240
|
+
if (localNeedsWrite && options.writeBack) {
|
|
241
|
+
writeLocalVars(filePath, updatedLocalVars);
|
|
242
|
+
console.log(` ✓ Updated ${options.file}`);
|
|
243
|
+
}
|
|
244
|
+
// Update last known state
|
|
245
|
+
lastKnownRemoteVars = await getRemoteVars(); // Re-fetch after our changes
|
|
246
|
+
lastKnownLocalVars = readLocalVars(filePath);
|
|
247
|
+
const totalChanges = Object.keys(toSetRemote).length + toDeleteRemote.length + (localNeedsWrite ? 1 : 0);
|
|
248
|
+
if (totalChanges === 0) {
|
|
249
|
+
console.log("No changes detected.");
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
console.log("Sync complete.");
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
console.error("Sync error:", error instanceof Error ? error.message : error);
|
|
257
|
+
}
|
|
258
|
+
finally {
|
|
259
|
+
isSyncing = false;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
function debounce(fn, ms) {
|
|
263
|
+
let timeoutId = null;
|
|
264
|
+
return (...args) => {
|
|
265
|
+
if (timeoutId)
|
|
266
|
+
clearTimeout(timeoutId);
|
|
267
|
+
timeoutId = setTimeout(() => fn(...args), ms);
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
async function main() {
|
|
271
|
+
const options = parseArgs();
|
|
272
|
+
const filePath = resolve(options.file);
|
|
273
|
+
console.log("Convex Environment Variable Sync");
|
|
274
|
+
console.log("================================");
|
|
275
|
+
console.log(`File: ${filePath}`);
|
|
276
|
+
console.log(`Write-back: ${options.writeBack}`);
|
|
277
|
+
console.log(`Poll interval: ${options.poll}s`);
|
|
278
|
+
console.log(`Mode: ${options.once ? "one-time sync" : "continuous watch"}`);
|
|
279
|
+
// Initial sync
|
|
280
|
+
await sync(options, true);
|
|
281
|
+
if (options.once) {
|
|
282
|
+
console.log("\nOne-time sync complete. Exiting.");
|
|
283
|
+
process.exit(0);
|
|
284
|
+
}
|
|
285
|
+
// Set up file watcher with debounce
|
|
286
|
+
const debouncedSync = debounce(() => sync(options, false), 500);
|
|
287
|
+
console.log(`\nWatching ${options.file} for changes...`);
|
|
288
|
+
console.log("Press Ctrl+C to exit.\n");
|
|
289
|
+
// Watch for file changes
|
|
290
|
+
let watcher = null;
|
|
291
|
+
const setupWatcher = () => {
|
|
292
|
+
if (watcher) {
|
|
293
|
+
watcher.close();
|
|
294
|
+
}
|
|
295
|
+
if (existsSync(filePath)) {
|
|
296
|
+
watcher = watch(filePath, (eventType) => {
|
|
297
|
+
if (eventType === "change") {
|
|
298
|
+
console.log(`\nFile change detected...`);
|
|
299
|
+
debouncedSync();
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
watcher.on("error", (error) => {
|
|
303
|
+
console.error("Watcher error:", error);
|
|
304
|
+
// Try to re-establish watcher
|
|
305
|
+
setTimeout(setupWatcher, 1000);
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
setupWatcher();
|
|
310
|
+
// Poll for remote changes
|
|
311
|
+
const pollInterval = setInterval(() => {
|
|
312
|
+
console.log(`\nPolling remote...`);
|
|
313
|
+
sync(options, false);
|
|
314
|
+
}, options.poll * 1000);
|
|
315
|
+
// Handle graceful shutdown
|
|
316
|
+
process.on("SIGINT", () => {
|
|
317
|
+
console.log("\n\nShutting down...");
|
|
318
|
+
if (watcher)
|
|
319
|
+
watcher.close();
|
|
320
|
+
clearInterval(pollInterval);
|
|
321
|
+
process.exit(0);
|
|
322
|
+
});
|
|
323
|
+
process.on("SIGTERM", () => {
|
|
324
|
+
console.log("\n\nShutting down...");
|
|
325
|
+
if (watcher)
|
|
326
|
+
watcher.close();
|
|
327
|
+
clearInterval(pollInterval);
|
|
328
|
+
process.exit(0);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
main().catch((error) => {
|
|
332
|
+
console.error("Fatal error:", error);
|
|
333
|
+
process.exit(1);
|
|
334
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "convex-env-sync",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Sync environment variables with Convex using a separate .env.convex file",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"convex-env-sync": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"prepublishOnly": "npm run build"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"convex",
|
|
19
|
+
"env",
|
|
20
|
+
"environment",
|
|
21
|
+
"variables",
|
|
22
|
+
"sync",
|
|
23
|
+
"cli"
|
|
24
|
+
],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/johannesschiessl/convex-env-sync"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=18"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/node": "^20",
|
|
35
|
+
"typescript": "^5"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"convex": "*"
|
|
39
|
+
},
|
|
40
|
+
"peerDependenciesMeta": {
|
|
41
|
+
"convex": {
|
|
42
|
+
"optional": true
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|