@jondotsoy/envs 0.0.1 → 0.0.3
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/envs.js +204 -0
- package/package.json +15 -15
- package/README.md +0 -15
- package/src/bin/envs.ts +0 -4
- package/src/envs.ts +0 -230
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jonathan Delgado <hi@jon.soy> (https://jon.soy)
|
|
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/envs.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// @bun
|
|
3
|
+
|
|
4
|
+
// src/envs.ts
|
|
5
|
+
import { mkdir } from "fs/promises";
|
|
6
|
+
import { basename, dirname, join } from "path";
|
|
7
|
+
var {$ } = globalThis.Bun;
|
|
8
|
+
var HELP = `Usage: envs <command>
|
|
9
|
+
|
|
10
|
+
Commands:
|
|
11
|
+
init Create the .envs/ directory (.gitignore, values.yml)
|
|
12
|
+
pull Read each worktree's .env and write it into .envs/values.yml
|
|
13
|
+
push Write defaults and .envs/values.yml into the .env of each worktree
|
|
14
|
+
edit Pull, open .envs/values.yml in VS Code (code -w), then push
|
|
15
|
+
help Show this help message
|
|
16
|
+
`;
|
|
17
|
+
var defaultContext = () => ({
|
|
18
|
+
cwd: process.cwd(),
|
|
19
|
+
log: (message) => console.log(message),
|
|
20
|
+
error: (message) => console.error(message),
|
|
21
|
+
openEditor: async (path) => {
|
|
22
|
+
const proc = Bun.spawn(["code", "-w", path], {
|
|
23
|
+
env: { ...process.env },
|
|
24
|
+
stdio: ["inherit", "inherit", "inherit"]
|
|
25
|
+
});
|
|
26
|
+
return await proc.exited;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
async function findRoot(cwd) {
|
|
30
|
+
const result = await $`git rev-parse --path-format=absolute --git-common-dir`.cwd(cwd).nothrow().quiet();
|
|
31
|
+
if (result.exitCode !== 0)
|
|
32
|
+
return cwd;
|
|
33
|
+
return dirname(result.stdout.toString().trim());
|
|
34
|
+
}
|
|
35
|
+
async function listWorktrees(cwd) {
|
|
36
|
+
const out = await $`git worktree list --porcelain`.cwd(cwd).text();
|
|
37
|
+
return out.trim().split(`
|
|
38
|
+
|
|
39
|
+
`).map((block) => {
|
|
40
|
+
const lines = block.split(`
|
|
41
|
+
`);
|
|
42
|
+
const path = lines.find((l) => l.startsWith("worktree ")).slice(9);
|
|
43
|
+
const branch = lines.find((l) => l.startsWith("branch "));
|
|
44
|
+
return {
|
|
45
|
+
path,
|
|
46
|
+
name: branch ? branch.slice("branch refs/heads/".length) : basename(path)
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function parseDotenv(text) {
|
|
51
|
+
const result = {};
|
|
52
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
53
|
+
const line = raw.trim();
|
|
54
|
+
if (!line || line.startsWith("#"))
|
|
55
|
+
continue;
|
|
56
|
+
const match = line.match(/^(?:export\s+)?([\w.-]+)\s*=\s*(.*)$/);
|
|
57
|
+
if (!match)
|
|
58
|
+
continue;
|
|
59
|
+
let value = match[2].trim();
|
|
60
|
+
const quote = value[0];
|
|
61
|
+
if ((quote === '"' || quote === "'") && value.lastIndexOf(quote) > 0) {
|
|
62
|
+
value = value.slice(1, value.lastIndexOf(quote));
|
|
63
|
+
} else {
|
|
64
|
+
value = value.replace(/\s+#.*$/, "");
|
|
65
|
+
}
|
|
66
|
+
result[match[1]] = value;
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
function formatDotenvValue(value) {
|
|
71
|
+
if (value !== "" && !/[\s#"'\\$]/.test(value))
|
|
72
|
+
return value;
|
|
73
|
+
return value.includes('"') ? `'${value}'` : `"${value}"`;
|
|
74
|
+
}
|
|
75
|
+
function updateDotenv(text, entries) {
|
|
76
|
+
const pending = new Map(Object.entries(entries));
|
|
77
|
+
const lines = text === "" ? [] : text.replace(/\n$/, "").split(`
|
|
78
|
+
`);
|
|
79
|
+
const updated = lines.map((line) => {
|
|
80
|
+
const key = line.trim().match(/^(?:export\s+)?([\w.-]+)\s*=/)?.[1];
|
|
81
|
+
if (key === undefined || !pending.has(key))
|
|
82
|
+
return line;
|
|
83
|
+
const value = pending.get(key);
|
|
84
|
+
pending.delete(key);
|
|
85
|
+
return `${key}=${formatDotenvValue(value)}`;
|
|
86
|
+
});
|
|
87
|
+
for (const [key, value] of pending) {
|
|
88
|
+
updated.push(`${key}=${formatDotenvValue(value)}`);
|
|
89
|
+
}
|
|
90
|
+
return updated.join(`
|
|
91
|
+
`) + `
|
|
92
|
+
`;
|
|
93
|
+
}
|
|
94
|
+
async function readValues(ctx) {
|
|
95
|
+
const path = join(await findRoot(ctx.cwd), ".envs/values.yml");
|
|
96
|
+
if (!await Bun.file(path).exists()) {
|
|
97
|
+
ctx.error(`${path} not found. Run \`envs init\` first.`);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const values = Bun.YAML.parse(await Bun.file(path).text()) ?? {};
|
|
101
|
+
return { path, values };
|
|
102
|
+
}
|
|
103
|
+
async function init(ctx) {
|
|
104
|
+
const root = await findRoot(ctx.cwd);
|
|
105
|
+
const files = { ".envs/.gitignore": `*
|
|
106
|
+
`, ".envs/values.yml": "" };
|
|
107
|
+
await mkdir(join(root, ".envs"), { recursive: true });
|
|
108
|
+
for (const [name, content] of Object.entries(files)) {
|
|
109
|
+
const path = join(root, name);
|
|
110
|
+
if (await Bun.file(path).exists()) {
|
|
111
|
+
ctx.log(`${path} already exists`);
|
|
112
|
+
} else {
|
|
113
|
+
await Bun.write(path, content);
|
|
114
|
+
ctx.log(`Created ${path}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return 0;
|
|
118
|
+
}
|
|
119
|
+
async function push(ctx) {
|
|
120
|
+
const read = await readValues(ctx);
|
|
121
|
+
if (!read)
|
|
122
|
+
return 1;
|
|
123
|
+
const { values } = read;
|
|
124
|
+
for (const { name, path } of await listWorktrees(ctx.cwd)) {
|
|
125
|
+
const entries = {};
|
|
126
|
+
for (const [key, value] of Object.entries(values.defaults ?? {})) {
|
|
127
|
+
entries[key] = String(value);
|
|
128
|
+
}
|
|
129
|
+
for (const [key, byWorktree] of Object.entries(values.envs ?? {})) {
|
|
130
|
+
if (name in byWorktree)
|
|
131
|
+
entries[key] = String(byWorktree[name]);
|
|
132
|
+
}
|
|
133
|
+
if (Object.keys(entries).length === 0)
|
|
134
|
+
continue;
|
|
135
|
+
const dotenv = Bun.file(join(path, ".env"));
|
|
136
|
+
const current = await dotenv.exists() ? await dotenv.text() : "";
|
|
137
|
+
await Bun.write(dotenv, updateDotenv(current, entries));
|
|
138
|
+
ctx.log(`Pushed ${name}`);
|
|
139
|
+
}
|
|
140
|
+
return 0;
|
|
141
|
+
}
|
|
142
|
+
async function pull(ctx) {
|
|
143
|
+
const read = await readValues(ctx);
|
|
144
|
+
if (!read)
|
|
145
|
+
return 1;
|
|
146
|
+
const { path: valuesPath, values } = read;
|
|
147
|
+
const envs = values.envs ??= {};
|
|
148
|
+
for (const { name, path } of await listWorktrees(ctx.cwd)) {
|
|
149
|
+
const dotenv = Bun.file(join(path, ".env"));
|
|
150
|
+
if (!await dotenv.exists())
|
|
151
|
+
continue;
|
|
152
|
+
const parsed = parseDotenv(await dotenv.text());
|
|
153
|
+
for (const [key, byWorktree] of Object.entries(envs)) {
|
|
154
|
+
if (!(key in parsed))
|
|
155
|
+
delete byWorktree[name];
|
|
156
|
+
}
|
|
157
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
158
|
+
(envs[key] ??= {})[name] = value;
|
|
159
|
+
}
|
|
160
|
+
ctx.log(`Pulled ${name}`);
|
|
161
|
+
}
|
|
162
|
+
for (const [key, byWorktree] of Object.entries(envs)) {
|
|
163
|
+
if (Object.keys(byWorktree).length === 0)
|
|
164
|
+
delete envs[key];
|
|
165
|
+
}
|
|
166
|
+
await Bun.write(valuesPath, Bun.YAML.stringify(values, null, 2));
|
|
167
|
+
return 0;
|
|
168
|
+
}
|
|
169
|
+
async function edit(ctx) {
|
|
170
|
+
const pulled = await pull(ctx);
|
|
171
|
+
if (pulled !== 0)
|
|
172
|
+
return pulled;
|
|
173
|
+
const valuesPath = join(await findRoot(ctx.cwd), ".envs/values.yml");
|
|
174
|
+
const code = await ctx.openEditor(valuesPath);
|
|
175
|
+
if (code !== 0)
|
|
176
|
+
return code;
|
|
177
|
+
return await push(ctx);
|
|
178
|
+
}
|
|
179
|
+
async function run(args, ctx = defaultContext()) {
|
|
180
|
+
const [command = "help"] = args;
|
|
181
|
+
switch (command) {
|
|
182
|
+
case "help":
|
|
183
|
+
case "--help":
|
|
184
|
+
case "-h":
|
|
185
|
+
ctx.log(HELP);
|
|
186
|
+
return 0;
|
|
187
|
+
case "init":
|
|
188
|
+
return await init(ctx);
|
|
189
|
+
case "push":
|
|
190
|
+
return await push(ctx);
|
|
191
|
+
case "pull":
|
|
192
|
+
return await pull(ctx);
|
|
193
|
+
case "edit":
|
|
194
|
+
return await edit(ctx);
|
|
195
|
+
default:
|
|
196
|
+
ctx.error(`Unknown command: ${command}
|
|
197
|
+
|
|
198
|
+
${HELP}`);
|
|
199
|
+
return 1;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// src/bin/envs.ts
|
|
204
|
+
process.exit(await run(Bun.argv.slice(2)));
|
package/package.json
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jondotsoy/envs",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Jonathan Delgado",
|
|
7
|
+
"email": "hi@jon.soy",
|
|
8
|
+
"url": "https://jon.soy"
|
|
8
9
|
},
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/JonDotsoy/envs.git"
|
|
12
13
|
},
|
|
13
|
-
"
|
|
14
|
-
"
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/JonDotsoy/envs/issues"
|
|
15
16
|
},
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"build": "bun scripts/build.ts"
|
|
17
|
+
"module": "envs.js",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"bin": {
|
|
20
|
+
"envs": "envs.js"
|
|
21
21
|
}
|
|
22
22
|
}
|
package/README.md
DELETED
package/src/bin/envs.ts
DELETED
package/src/envs.ts
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import { mkdir } from "node:fs/promises";
|
|
2
|
-
import { basename, dirname, join } from "node:path";
|
|
3
|
-
import { $ } from "bun";
|
|
4
|
-
|
|
5
|
-
export const HELP = `Usage: envs <command>
|
|
6
|
-
|
|
7
|
-
Commands:
|
|
8
|
-
init Create the .envs/ directory (.gitignore, values.yml)
|
|
9
|
-
pull Read each worktree's .env and write it into .envs/values.yml
|
|
10
|
-
push Write defaults and .envs/values.yml into the .env of each worktree
|
|
11
|
-
edit Pull, open .envs/values.yml in VS Code (code -w), then push
|
|
12
|
-
help Show this help message
|
|
13
|
-
`;
|
|
14
|
-
|
|
15
|
-
export interface Context {
|
|
16
|
-
/** Directory the command runs from. */
|
|
17
|
-
cwd: string;
|
|
18
|
-
log(message: string): void;
|
|
19
|
-
error(message: string): void;
|
|
20
|
-
/** Opens a file in the editor and resolves with its exit code once closed. */
|
|
21
|
-
openEditor(path: string): Promise<number>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const defaultContext = (): Context => ({
|
|
25
|
-
cwd: process.cwd(),
|
|
26
|
-
log: (message) => console.log(message),
|
|
27
|
-
error: (message) => console.error(message),
|
|
28
|
-
openEditor: async (path) => {
|
|
29
|
-
const proc = Bun.spawn(["code", "-w", path], {
|
|
30
|
-
env: { ...process.env },
|
|
31
|
-
stdio: ["inherit", "inherit", "inherit"],
|
|
32
|
-
});
|
|
33
|
-
return await proc.exited;
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
interface Values {
|
|
38
|
-
defaults?: Record<string, string>;
|
|
39
|
-
envs?: Record<string, Record<string, string>>;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface Worktree {
|
|
43
|
-
name: string;
|
|
44
|
-
path: string;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Root of the main git workspace; inside a worktree this is the parent repo. Falls back to cwd outside git. */
|
|
48
|
-
export async function findRoot(cwd: string): Promise<string> {
|
|
49
|
-
const result = await $`git rev-parse --path-format=absolute --git-common-dir`
|
|
50
|
-
.cwd(cwd)
|
|
51
|
-
.nothrow()
|
|
52
|
-
.quiet();
|
|
53
|
-
if (result.exitCode !== 0) return cwd;
|
|
54
|
-
return dirname(result.stdout.toString().trim());
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Lists the main workspace and its worktrees; `name` is the branch (or dir name when detached). */
|
|
58
|
-
export async function listWorktrees(cwd: string): Promise<Worktree[]> {
|
|
59
|
-
const out = await $`git worktree list --porcelain`.cwd(cwd).text();
|
|
60
|
-
return out
|
|
61
|
-
.trim()
|
|
62
|
-
.split("\n\n")
|
|
63
|
-
.map((block) => {
|
|
64
|
-
const lines = block.split("\n");
|
|
65
|
-
const path = lines.find((l) => l.startsWith("worktree "))!.slice(9);
|
|
66
|
-
const branch = lines.find((l) => l.startsWith("branch "));
|
|
67
|
-
return {
|
|
68
|
-
path,
|
|
69
|
-
name: branch
|
|
70
|
-
? branch.slice("branch refs/heads/".length)
|
|
71
|
-
: basename(path),
|
|
72
|
-
};
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** Minimal .env parser: KEY=VALUE lines, `#` comments, optional quotes and `export`. */
|
|
77
|
-
export function parseDotenv(text: string): Record<string, string> {
|
|
78
|
-
const result: Record<string, string> = {};
|
|
79
|
-
for (const raw of text.split(/\r?\n/)) {
|
|
80
|
-
const line = raw.trim();
|
|
81
|
-
if (!line || line.startsWith("#")) continue;
|
|
82
|
-
const match = line.match(/^(?:export\s+)?([\w.-]+)\s*=\s*(.*)$/);
|
|
83
|
-
if (!match) continue;
|
|
84
|
-
let value = match[2]!.trim();
|
|
85
|
-
const quote = value[0];
|
|
86
|
-
if ((quote === '"' || quote === "'") && value.lastIndexOf(quote) > 0) {
|
|
87
|
-
value = value.slice(1, value.lastIndexOf(quote));
|
|
88
|
-
} else {
|
|
89
|
-
value = value.replace(/\s+#.*$/, "");
|
|
90
|
-
}
|
|
91
|
-
result[match[1]!] = value;
|
|
92
|
-
}
|
|
93
|
-
return result;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function formatDotenvValue(value: string): string {
|
|
97
|
-
if (value !== "" && !/[\s#"'\\$]/.test(value)) return value;
|
|
98
|
-
return value.includes('"') ? `'${value}'` : `"${value}"`;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** Sets KEY=VALUE for each entry, updating existing lines in place and appending new ones. */
|
|
102
|
-
export function updateDotenv(
|
|
103
|
-
text: string,
|
|
104
|
-
entries: Record<string, string>,
|
|
105
|
-
): string {
|
|
106
|
-
const pending = new Map(Object.entries(entries));
|
|
107
|
-
const lines = text === "" ? [] : text.replace(/\n$/, "").split("\n");
|
|
108
|
-
const updated = lines.map((line) => {
|
|
109
|
-
const key = line.trim().match(/^(?:export\s+)?([\w.-]+)\s*=/)?.[1];
|
|
110
|
-
if (key === undefined || !pending.has(key)) return line;
|
|
111
|
-
const value = pending.get(key)!;
|
|
112
|
-
pending.delete(key);
|
|
113
|
-
return `${key}=${formatDotenvValue(value)}`;
|
|
114
|
-
});
|
|
115
|
-
for (const [key, value] of pending) {
|
|
116
|
-
updated.push(`${key}=${formatDotenvValue(value)}`);
|
|
117
|
-
}
|
|
118
|
-
return updated.join("\n") + "\n";
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** Reads values.yml; returns undefined (after reporting) when `envs init` was not run. */
|
|
122
|
-
async function readValues(
|
|
123
|
-
ctx: Context,
|
|
124
|
-
): Promise<{ path: string; values: Values } | undefined> {
|
|
125
|
-
const path = join(await findRoot(ctx.cwd), ".envs/values.yml");
|
|
126
|
-
if (!(await Bun.file(path).exists())) {
|
|
127
|
-
ctx.error(`${path} not found. Run \`envs init\` first.`);
|
|
128
|
-
return undefined;
|
|
129
|
-
}
|
|
130
|
-
const values = (Bun.YAML.parse(await Bun.file(path).text()) ?? {}) as Values;
|
|
131
|
-
return { path, values };
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export async function init(ctx: Context): Promise<number> {
|
|
135
|
-
const root = await findRoot(ctx.cwd);
|
|
136
|
-
const files = { ".envs/.gitignore": "*\n", ".envs/values.yml": "" };
|
|
137
|
-
await mkdir(join(root, ".envs"), { recursive: true });
|
|
138
|
-
for (const [name, content] of Object.entries(files)) {
|
|
139
|
-
const path = join(root, name);
|
|
140
|
-
if (await Bun.file(path).exists()) {
|
|
141
|
-
ctx.log(`${path} already exists`);
|
|
142
|
-
} else {
|
|
143
|
-
await Bun.write(path, content);
|
|
144
|
-
ctx.log(`Created ${path}`);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return 0;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export async function push(ctx: Context): Promise<number> {
|
|
151
|
-
const read = await readValues(ctx);
|
|
152
|
-
if (!read) return 1;
|
|
153
|
-
const { values } = read;
|
|
154
|
-
for (const { name, path } of await listWorktrees(ctx.cwd)) {
|
|
155
|
-
// Defaults apply to every worktree; per-worktree values override them.
|
|
156
|
-
const entries: Record<string, string> = {};
|
|
157
|
-
for (const [key, value] of Object.entries(values.defaults ?? {})) {
|
|
158
|
-
entries[key] = String(value);
|
|
159
|
-
}
|
|
160
|
-
for (const [key, byWorktree] of Object.entries(values.envs ?? {})) {
|
|
161
|
-
if (name in byWorktree) entries[key] = String(byWorktree[name]);
|
|
162
|
-
}
|
|
163
|
-
if (Object.keys(entries).length === 0) continue;
|
|
164
|
-
const dotenv = Bun.file(join(path, ".env"));
|
|
165
|
-
const current = (await dotenv.exists()) ? await dotenv.text() : "";
|
|
166
|
-
await Bun.write(dotenv, updateDotenv(current, entries));
|
|
167
|
-
ctx.log(`Pushed ${name}`);
|
|
168
|
-
}
|
|
169
|
-
return 0;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export async function pull(ctx: Context): Promise<number> {
|
|
173
|
-
const read = await readValues(ctx);
|
|
174
|
-
if (!read) return 1;
|
|
175
|
-
const { path: valuesPath, values } = read;
|
|
176
|
-
const envs = (values.envs ??= {});
|
|
177
|
-
for (const { name, path } of await listWorktrees(ctx.cwd)) {
|
|
178
|
-
const dotenv = Bun.file(join(path, ".env"));
|
|
179
|
-
if (!(await dotenv.exists())) continue;
|
|
180
|
-
const parsed = parseDotenv(await dotenv.text());
|
|
181
|
-
// Drop keys that are no longer in this worktree's .env.
|
|
182
|
-
for (const [key, byWorktree] of Object.entries(envs)) {
|
|
183
|
-
if (!(key in parsed)) delete byWorktree[name];
|
|
184
|
-
}
|
|
185
|
-
for (const [key, value] of Object.entries(parsed)) {
|
|
186
|
-
(envs[key] ??= {})[name] = value;
|
|
187
|
-
}
|
|
188
|
-
ctx.log(`Pulled ${name}`);
|
|
189
|
-
}
|
|
190
|
-
for (const [key, byWorktree] of Object.entries(envs)) {
|
|
191
|
-
if (Object.keys(byWorktree).length === 0) delete envs[key];
|
|
192
|
-
}
|
|
193
|
-
await Bun.write(valuesPath, Bun.YAML.stringify(values, null, 2));
|
|
194
|
-
return 0;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export async function edit(ctx: Context): Promise<number> {
|
|
198
|
-
const pulled = await pull(ctx);
|
|
199
|
-
if (pulled !== 0) return pulled;
|
|
200
|
-
const valuesPath = join(await findRoot(ctx.cwd), ".envs/values.yml");
|
|
201
|
-
const code = await ctx.openEditor(valuesPath);
|
|
202
|
-
if (code !== 0) return code;
|
|
203
|
-
return await push(ctx);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/** Runs a command; resolves with the process exit code. */
|
|
207
|
-
export async function run(
|
|
208
|
-
args: string[],
|
|
209
|
-
ctx: Context = defaultContext(),
|
|
210
|
-
): Promise<number> {
|
|
211
|
-
const [command = "help"] = args;
|
|
212
|
-
switch (command) {
|
|
213
|
-
case "help":
|
|
214
|
-
case "--help":
|
|
215
|
-
case "-h":
|
|
216
|
-
ctx.log(HELP);
|
|
217
|
-
return 0;
|
|
218
|
-
case "init":
|
|
219
|
-
return await init(ctx);
|
|
220
|
-
case "push":
|
|
221
|
-
return await push(ctx);
|
|
222
|
-
case "pull":
|
|
223
|
-
return await pull(ctx);
|
|
224
|
-
case "edit":
|
|
225
|
-
return await edit(ctx);
|
|
226
|
-
default:
|
|
227
|
-
ctx.error(`Unknown command: ${command}\n\n${HELP}`);
|
|
228
|
-
return 1;
|
|
229
|
-
}
|
|
230
|
-
}
|