@myrialabs/clopen 0.4.39 → 0.4.41
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/.env.example +1 -1
- package/backend/artifacts/detect.ts +15 -3
- package/backend/artifacts/generate.ts +2 -2
- package/backend/artifacts/index.ts +12 -4
- package/backend/artifacts/markers.ts +24 -1
- package/backend/artifacts/matrix.ts +47 -3
- package/backend/artifacts/sync.test.ts +3 -3
- package/backend/artifacts/sync.ts +8 -20
- package/backend/artifacts/types.ts +9 -3
- package/backend/auth/permissions.ts +29 -11
- package/backend/auth/session-cleanup.ts +10 -1
- package/backend/auth/tokens.ts +9 -1
- package/backend/chat/stream-manager.ts +42 -0
- package/backend/database/crypto/secret-columns.ts +5 -1
- package/backend/database/migrations/078_create_worktree_branch_tables.ts +158 -0
- package/backend/database/migrations/079_merge_commands_into_skills.ts +222 -0
- package/backend/database/migrations/080_create_file_shares.ts +111 -0
- package/backend/database/migrations/081_create_push_subscriptions.ts +32 -0
- package/backend/database/migrations/index.ts +28 -0
- package/backend/database/queries/file-share-queries.ts +163 -0
- package/backend/database/queries/index.ts +12 -4
- package/backend/database/queries/profile-queries.ts +5 -3
- package/backend/database/queries/push-subscription-queries.test.ts +90 -0
- package/backend/database/queries/push-subscription-queries.ts +99 -0
- package/backend/database/queries/skill-queries.ts +96 -18
- package/backend/database/queries/worktree-branch-queries.ts +331 -0
- package/backend/db-client/cloning.test.ts +161 -0
- package/backend/db-client/cloning.ts +672 -0
- package/backend/db-client/connection-url.test.ts +117 -0
- package/backend/db-client/connection-url.ts +126 -0
- package/backend/db-client/drivers/mongodb.ts +2 -14
- package/backend/db-client/drivers/mysql.ts +2 -8
- package/backend/db-client/drivers/postgres.ts +10 -13
- package/backend/db-client/drivers/redis.ts +2 -6
- package/backend/db-client/env/detect.ts +438 -0
- package/backend/db-client/env/diff.ts +94 -0
- package/backend/db-client/env/env.test.ts +574 -0
- package/backend/db-client/env/index.ts +15 -0
- package/backend/db-client/env/naming.ts +202 -0
- package/backend/db-client/env/service.ts +487 -0
- package/backend/db-client/integrations/index.ts +7 -0
- package/backend/db-client/integrations/links.ts +41 -10
- package/backend/db-client/providers/neon/adapter.ts +522 -0
- package/backend/db-client/providers/neon/api-types.ts +182 -0
- package/backend/db-client/providers/neon/client.ts +298 -0
- package/backend/db-client/providers/neon/endpoints.test.ts +201 -0
- package/backend/db-client/providers/neon/endpoints.ts +314 -0
- package/backend/db-client/providers/neon/list.test.ts +181 -0
- package/backend/db-client/providers/supabase/adapter.ts +25 -2
- package/backend/db-client/providers/types.ts +57 -0
- package/backend/engine/adapters/claude/stream.ts +2 -1
- package/backend/engine/adapters/cline/stream.ts +3 -3
- package/backend/engine/adapters/codex/stream.ts +3 -3
- package/backend/engine/adapters/copilot/stream.ts +3 -3
- package/backend/engine/adapters/cursor/stream.ts +25 -3
- package/backend/engine/adapters/opencode/stream.ts +4 -6
- package/backend/engine/adapters/pi/stream.ts +2 -2
- package/backend/engine/adapters/qwen/stream.ts +2 -2
- package/backend/engine/artifact-sync.ts +26 -20
- package/backend/engine/docs/artifacts.md +81 -22
- package/backend/engine/docs/quick-reference.md +2 -1
- package/backend/engine/resolve-model.test.ts +108 -0
- package/backend/engine/resolve-model.ts +53 -10
- package/backend/engine/structured-helpers.ts +22 -0
- package/backend/env-files/discovery.ts +48 -0
- package/backend/env-files/edit.ts +213 -0
- package/backend/env-files/env-files.test.ts +515 -0
- package/backend/env-files/git.ts +27 -0
- package/backend/env-files/index.ts +44 -0
- package/backend/env-files/keys.ts +69 -0
- package/backend/env-files/parse.ts +157 -0
- package/backend/env-files/write.ts +229 -0
- package/backend/files/file-shares.ts +369 -0
- package/backend/files/path-browsing.ts +27 -21
- package/backend/http/files-shared.test.ts +505 -0
- package/backend/http/files-shared.ts +351 -0
- package/backend/http/push.ts +150 -0
- package/backend/index.ts +8 -0
- package/backend/instructions/index.ts +1 -1
- package/backend/instructions/sync.ts +30 -6
- package/backend/integrations/health.ts +18 -4
- package/backend/integrations/projections/types.ts +9 -2
- package/backend/integrations/registry/index.ts +3 -0
- package/backend/integrations/registry/providers/context7.ts +1 -0
- package/backend/integrations/registry/providers/firecrawl.ts +1 -0
- package/backend/integrations/registry/providers/github.ts +1 -0
- package/backend/integrations/registry/providers/neon.ts +55 -0
- package/backend/integrations/registry/providers/supabase.ts +1 -0
- package/backend/integrations/registry/providers/vercel.ts +1 -0
- package/backend/integrations/registry/types.ts +2 -0
- package/backend/memory/compose.ts +2 -2
- package/backend/memory/consolidate.ts +2 -2
- package/backend/memory/extract/episodic.ts +2 -2
- package/backend/memory/judge.ts +2 -2
- package/backend/profiles/service.ts +7 -6
- package/backend/push/sender.ts +172 -0
- package/backend/push/tags.ts +25 -0
- package/backend/push/vapid-keys.ts +77 -0
- package/backend/push/webpush-crypto.test.ts +200 -0
- package/backend/push/webpush-crypto.ts +231 -0
- package/backend/skills/detect.ts +28 -0
- package/backend/skills/index.ts +22 -4
- package/backend/skills/invoke.test.ts +114 -0
- package/backend/skills/invoke.ts +222 -0
- package/backend/skills/service.ts +194 -44
- package/backend/skills/spec.ts +35 -0
- package/backend/skills/sync.ts +85 -55
- package/backend/subagents/detect.ts +2 -3
- package/backend/subagents/sync.ts +15 -8
- package/backend/work/pull-requests.ts +2 -2
- package/backend/work/start-work.ts +4 -0
- package/backend/worktrees/branching/engine.ts +557 -0
- package/backend/worktrees/branching/index.ts +56 -0
- package/backend/worktrees/branching/naming.test.ts +118 -0
- package/backend/worktrees/branching/naming.ts +97 -0
- package/backend/worktrees/branching/projector.test.ts +229 -0
- package/backend/worktrees/branching/projector.ts +139 -0
- package/backend/worktrees/branching/providers/neon.ts +220 -0
- package/backend/worktrees/branching/registry.ts +27 -0
- package/backend/worktrees/branching/service.ts +620 -0
- package/backend/worktrees/branching/suggest.test.ts +154 -0
- package/backend/worktrees/branching/suggest.ts +202 -0
- package/backend/worktrees/branching/types.ts +88 -0
- package/backend/worktrees/index.ts +7 -0
- package/backend/worktrees/service.ts +48 -1
- package/backend/ws/chat/stream.ts +100 -8
- package/backend/ws/db-client/env.ts +142 -0
- package/backend/ws/db-client/index.ts +2 -0
- package/backend/ws/files/index.ts +4 -0
- package/backend/ws/files/share.ts +95 -0
- package/backend/ws/git/branch-name.ts +2 -2
- package/backend/ws/git/commit-message.ts +2 -2
- package/backend/ws/index.ts +3 -4
- package/backend/ws/profiles/crud.ts +2 -3
- package/backend/ws/projects/index.ts +6 -1
- package/backend/ws/projects/info.ts +70 -6
- package/backend/ws/projects/overview.test.ts +108 -0
- package/backend/ws/projects/overview.ts +300 -0
- package/backend/ws/skills/catalog.ts +5 -1
- package/backend/ws/skills/crud.ts +128 -22
- package/backend/ws/skills/index.ts +4 -1
- package/backend/ws/system/device-info.ts +307 -76
- package/backend/ws/user/crud.ts +12 -3
- package/backend/ws/worktrees/branching.ts +212 -0
- package/backend/ws/worktrees/crud.ts +33 -7
- package/backend/ws/worktrees/index.ts +14 -0
- package/dist/assets/{index-C8Gi-b6U.js → index-CeNKKSDR.js} +1 -1
- package/dist/assets/{index-BrF6nqld.js → index-DW89566K.js} +1 -1
- package/dist/assets/main-Cy5DXWPT.js +1207 -0
- package/dist/assets/main-DJKj_4By.css +1 -0
- package/dist/index.html +7 -2
- package/dist/sw.js +112 -0
- package/package.json +1 -1
- package/shared/constants/engine-tools.ts +31 -6
- package/shared/constants/notification-messages.ts +142 -0
- package/shared/constants/tool-icons.ts +6 -2
- package/shared/types/db-client/env.ts +192 -0
- package/shared/types/db-client/index.ts +1 -0
- package/shared/types/db-client/integrations.ts +30 -0
- package/shared/types/integrations.ts +9 -0
- package/shared/types/unified/stream.ts +7 -0
- package/shared/types/worktree-branching.ts +315 -0
- package/shared/utils/logger.ts +1 -0
- package/backend/commands/detect.ts +0 -25
- package/backend/commands/index.ts +0 -9
- package/backend/commands/service.ts +0 -178
- package/backend/commands/store.ts +0 -45
- package/backend/commands/sync.ts +0 -94
- package/backend/database/queries/command-queries.ts +0 -82
- package/backend/ws/commands/crud.ts +0 -157
- package/backend/ws/commands/index.ts +0 -8
- package/dist/assets/main-Bd3gb79F.css +0 -1
- package/dist/assets/main-Bo1hD7mt.js +0 -1191
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A dotenv reader that remembers where everything was.
|
|
3
|
+
*
|
|
4
|
+
* `dotenv` and friends answer "what is the value of KEY", which is the wrong
|
|
5
|
+
* question for a writer: updating a variable IN PLACE means knowing which lines
|
|
6
|
+
* held it, whether it was quoted, and whether it was commented out. A parse
|
|
7
|
+
* that returns a plain object throws all of that away and leaves rewriting the
|
|
8
|
+
* file as the only option — which is how tools end up reformatting comments and
|
|
9
|
+
* ordering that the user put there on purpose.
|
|
10
|
+
*
|
|
11
|
+
* Commented assignments are returned too, marked. `.env.example` is often the
|
|
12
|
+
* only record of the variable names a project expects, and half of those files
|
|
13
|
+
* document optional settings as `# REDIS_URL=`, so a detector that ignored
|
|
14
|
+
* comments would miss the shape it exists to find.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** One `KEY=value`, and where it lives. */
|
|
18
|
+
export interface DotenvAssignment {
|
|
19
|
+
key: string;
|
|
20
|
+
value: string;
|
|
21
|
+
/** The original text, including every line the value spans. */
|
|
22
|
+
raw: string;
|
|
23
|
+
/** 0-based index of the first line. */
|
|
24
|
+
line: number;
|
|
25
|
+
/** How many lines the assignment spans — more than one for a quoted value. */
|
|
26
|
+
lineCount: number;
|
|
27
|
+
exported: boolean;
|
|
28
|
+
/** True for `# KEY=value`: documentation, not configuration. */
|
|
29
|
+
commented: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* `[^\r\n]*` rather than `.*`, and the reason is a bug this had.
|
|
34
|
+
*
|
|
35
|
+
* In JavaScript `.` does not match `\r`, so on a file written on Windows every
|
|
36
|
+
* line ends one character past what `.*$` can reach and the pattern matched
|
|
37
|
+
* NOTHING — a CRLF dotenv file parsed as empty, which would have made detection
|
|
38
|
+
* find no variables and every write append a duplicate.
|
|
39
|
+
*/
|
|
40
|
+
const ASSIGNMENT = /^(\s*)(#+\s*)?(export\s+)?([A-Za-z_][A-Za-z0-9_]*)[ \t]*=([^\r\n]*)\r?$/;
|
|
41
|
+
|
|
42
|
+
/** Decode a double-quoted value's escapes. Single quotes are literal. */
|
|
43
|
+
function unescapeDouble(raw: string): string {
|
|
44
|
+
return raw.replace(/\\([nrt"'\\$`])/g, (_match, char: string) => {
|
|
45
|
+
if (char === 'n') return '\n';
|
|
46
|
+
if (char === 'r') return '\r';
|
|
47
|
+
if (char === 't') return '\t';
|
|
48
|
+
return char;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The index of the quote that closes the value, or -1.
|
|
54
|
+
*
|
|
55
|
+
* Only a double-quoted value honours backslash escapes, which is what every
|
|
56
|
+
* reader in common use does — inside single quotes a backslash is a backslash,
|
|
57
|
+
* so skipping the next character there would read past a legitimate close.
|
|
58
|
+
*/
|
|
59
|
+
function findClosingQuote(body: string, quote: string): number {
|
|
60
|
+
for (let index = 0; index < body.length; index += 1) {
|
|
61
|
+
if (quote === '"' && body[index] === '\\') {
|
|
62
|
+
index += 1;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (body[index] === quote) return index;
|
|
66
|
+
}
|
|
67
|
+
return -1;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Read a value that may run past the end of its first line.
|
|
72
|
+
*
|
|
73
|
+
* Returns how many EXTRA lines were consumed so the caller can keep its own
|
|
74
|
+
* line index honest — the whole reason this module exists.
|
|
75
|
+
*/
|
|
76
|
+
function readValue(
|
|
77
|
+
first: string,
|
|
78
|
+
lines: string[],
|
|
79
|
+
index: number
|
|
80
|
+
): { value: string; extraLines: number } {
|
|
81
|
+
const trimmed = first.trimStart();
|
|
82
|
+
const quote = trimmed[0];
|
|
83
|
+
|
|
84
|
+
if (quote !== '"' && quote !== "'" && quote !== '`') {
|
|
85
|
+
// Unquoted. Everything from an unescaped `#` onwards is a comment, which
|
|
86
|
+
// is what every reader in common use does.
|
|
87
|
+
const withoutComment = trimmed.split('#')[0] ?? '';
|
|
88
|
+
return { value: withoutComment.trim(), extraLines: 0 };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let body = trimmed.slice(1);
|
|
92
|
+
let extraLines = 0;
|
|
93
|
+
|
|
94
|
+
for (;;) {
|
|
95
|
+
const closing = findClosingQuote(body, quote);
|
|
96
|
+
if (closing !== -1) {
|
|
97
|
+
const inner = body.slice(0, closing);
|
|
98
|
+
return { value: quote === '"' ? unescapeDouble(inner) : inner, extraLines };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const next = lines[index + extraLines + 1];
|
|
102
|
+
// Unterminated at end of file. Taking what there is beats discarding the
|
|
103
|
+
// assignment: the caller is deciding whether to rewrite this line, and a
|
|
104
|
+
// key it cannot see is a key it would append a duplicate of.
|
|
105
|
+
if (next === undefined) {
|
|
106
|
+
return { value: quote === '"' ? unescapeDouble(body) : body, extraLines };
|
|
107
|
+
}
|
|
108
|
+
body = `${body}\n${next}`;
|
|
109
|
+
extraLines += 1;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function parseDotenv(content: string): DotenvAssignment[] {
|
|
114
|
+
const lines = content.split('\n');
|
|
115
|
+
const found: DotenvAssignment[] = [];
|
|
116
|
+
|
|
117
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
118
|
+
const match = ASSIGNMENT.exec(lines[index]);
|
|
119
|
+
if (!match) continue;
|
|
120
|
+
|
|
121
|
+
const [, , hash, exported, key, rest] = match;
|
|
122
|
+
const { value, extraLines } = readValue(rest, lines, index);
|
|
123
|
+
const lineCount = extraLines + 1;
|
|
124
|
+
|
|
125
|
+
found.push({
|
|
126
|
+
key,
|
|
127
|
+
value,
|
|
128
|
+
raw: lines.slice(index, index + lineCount).join('\n'),
|
|
129
|
+
line: index,
|
|
130
|
+
lineCount,
|
|
131
|
+
exported: Boolean(exported),
|
|
132
|
+
commented: Boolean(hash)
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
index += extraLines;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return found;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The assignment a reader would actually use for `key`, or null.
|
|
143
|
+
*
|
|
144
|
+
* The LAST one wins, and commented ones do not count — the two rules that
|
|
145
|
+
* decide whether writing a variable will have any effect.
|
|
146
|
+
*/
|
|
147
|
+
export function effectiveAssignment(
|
|
148
|
+
assignments: DotenvAssignment[],
|
|
149
|
+
key: string
|
|
150
|
+
): DotenvAssignment | null {
|
|
151
|
+
let winner: DotenvAssignment | null = null;
|
|
152
|
+
for (const assignment of assignments) {
|
|
153
|
+
if (assignment.commented) continue;
|
|
154
|
+
if (assignment.key === key) winner = assignment;
|
|
155
|
+
}
|
|
156
|
+
return winner;
|
|
157
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Putting variables into a project's dotenv file.
|
|
3
|
+
*
|
|
4
|
+
* One rule is a hard refusal: **never a file git TRACKS.** For DB Client the
|
|
5
|
+
* cost is committing a live database password; for worktree branching it is
|
|
6
|
+
* worse — "Apply to Main" would carry the branch connection string into the
|
|
7
|
+
* main project, overwriting the real one with a URL pointing at a database that
|
|
8
|
+
* deleting the worktree destroys. Both are refused and reported rather than
|
|
9
|
+
* written and hoped about, and the refusal is overridable only where the caller
|
|
10
|
+
* says so explicitly.
|
|
11
|
+
*
|
|
12
|
+
* Nothing here throws for an ordinary failure. Every caller has already done
|
|
13
|
+
* the expensive, visible thing the user asked for — created a worktree, linked
|
|
14
|
+
* a database — and a variable that did not land is worth a sentence, not an
|
|
15
|
+
* exception that undoes the rest.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import fs from 'fs/promises';
|
|
19
|
+
import path from 'path';
|
|
20
|
+
import { removeEnvKeys, upsertEnvKeys } from './edit';
|
|
21
|
+
import { isSafeEnvFileName } from './keys';
|
|
22
|
+
import { isTrackedInGit } from './git';
|
|
23
|
+
import { debug } from '$shared/utils/logger';
|
|
24
|
+
|
|
25
|
+
export type EnvWriteStatus = 'written' | 'unchanged' | 'skipped-tracked' | 'failed';
|
|
26
|
+
|
|
27
|
+
export interface EnvWriteResult {
|
|
28
|
+
status: EnvWriteStatus;
|
|
29
|
+
/** One sentence, naming what happened and what to do. Null when it worked. */
|
|
30
|
+
detail: string | null;
|
|
31
|
+
fileName: string;
|
|
32
|
+
updated: string[];
|
|
33
|
+
appended: string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface EnvWriteInput {
|
|
37
|
+
root: string;
|
|
38
|
+
fileName: string;
|
|
39
|
+
vars: Record<string, string>;
|
|
40
|
+
/** A comment above the appended lines, naming where they came from. */
|
|
41
|
+
note?: string;
|
|
42
|
+
/** Write even though git tracks the file. The caller must have asked. */
|
|
43
|
+
allowTracked?: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The file as it is, or an empty string when it does not exist yet. */
|
|
47
|
+
export async function readEnvFile(
|
|
48
|
+
root: string,
|
|
49
|
+
fileName: string
|
|
50
|
+
): Promise<{ text: string; exists: boolean }> {
|
|
51
|
+
try {
|
|
52
|
+
return { text: await fs.readFile(path.join(root, fileName), 'utf-8'), exists: true };
|
|
53
|
+
} catch {
|
|
54
|
+
return { text: '', exists: false };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface EnvWritePlan {
|
|
59
|
+
fileName: string;
|
|
60
|
+
exists: boolean;
|
|
61
|
+
/** The file as it is now. */
|
|
62
|
+
current: string;
|
|
63
|
+
/** The file as it would be. */
|
|
64
|
+
next: string;
|
|
65
|
+
updated: string[];
|
|
66
|
+
appended: string[];
|
|
67
|
+
unchanged: string[];
|
|
68
|
+
/** True when git tracks the target, which refuses the write by default. */
|
|
69
|
+
tracked: boolean;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Work out the write without performing it.
|
|
74
|
+
*
|
|
75
|
+
* Exists so the panel can show a diff of someone's own configuration file
|
|
76
|
+
* before touching it, and so `writeEnvVars` and the preview can never disagree
|
|
77
|
+
* about what would happen.
|
|
78
|
+
*/
|
|
79
|
+
export async function planEnvWrite(input: EnvWriteInput): Promise<EnvWritePlan> {
|
|
80
|
+
if (!isSafeEnvFileName(input.fileName)) {
|
|
81
|
+
throw new Error(`"${input.fileName}" is not a dotenv file in the project root.`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const [{ text, exists }, tracked] = await Promise.all([
|
|
85
|
+
readEnvFile(input.root, input.fileName),
|
|
86
|
+
isTrackedInGit(input.root, input.fileName)
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
const result = upsertEnvKeys(text, input.vars, { note: input.note });
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
fileName: input.fileName,
|
|
93
|
+
exists,
|
|
94
|
+
current: text,
|
|
95
|
+
next: result.text,
|
|
96
|
+
updated: result.updated,
|
|
97
|
+
appended: result.appended,
|
|
98
|
+
unchanged: result.unchanged,
|
|
99
|
+
tracked
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export async function writeEnvVars(input: EnvWriteInput): Promise<EnvWriteResult> {
|
|
104
|
+
const empty = { fileName: input.fileName, updated: [], appended: [] };
|
|
105
|
+
|
|
106
|
+
let plan: EnvWritePlan;
|
|
107
|
+
try {
|
|
108
|
+
plan = await planEnvWrite(input);
|
|
109
|
+
} catch (error) {
|
|
110
|
+
return { status: 'failed', detail: messageOf(error), ...empty };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (plan.tracked && !input.allowTracked) {
|
|
114
|
+
return {
|
|
115
|
+
status: 'skipped-tracked',
|
|
116
|
+
detail: `git tracks ${input.fileName}, so writing a password into it would commit one — add it to .gitignore or pick a file that is ignored.`,
|
|
117
|
+
...empty
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (plan.next === plan.current && plan.exists) {
|
|
122
|
+
return {
|
|
123
|
+
status: 'unchanged',
|
|
124
|
+
detail: null,
|
|
125
|
+
fileName: input.fileName,
|
|
126
|
+
updated: [],
|
|
127
|
+
appended: []
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
await fs.writeFile(path.join(input.root, input.fileName), plan.next, 'utf-8');
|
|
133
|
+
debug.log('env-files', `Wrote ${plan.updated.length + plan.appended.length} variable(s) into ${input.fileName}`);
|
|
134
|
+
return {
|
|
135
|
+
status: 'written',
|
|
136
|
+
detail: null,
|
|
137
|
+
fileName: input.fileName,
|
|
138
|
+
updated: plan.updated,
|
|
139
|
+
appended: plan.appended
|
|
140
|
+
};
|
|
141
|
+
} catch (error) {
|
|
142
|
+
return { status: 'failed', detail: `Could not write ${input.fileName}: ${messageOf(error)}`, ...empty };
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Take the managed block and every replaced value back out.
|
|
148
|
+
*
|
|
149
|
+
* Silent about a missing file: there is nothing to undo, which is the outcome
|
|
150
|
+
* the caller wanted.
|
|
151
|
+
*/
|
|
152
|
+
export async function clearEnvVars(input: {
|
|
153
|
+
root: string;
|
|
154
|
+
fileName: string;
|
|
155
|
+
keys: string[];
|
|
156
|
+
/** Remove an appended key only while it still holds this value. */
|
|
157
|
+
values?: Record<string, string>;
|
|
158
|
+
}): Promise<{ changed: boolean }> {
|
|
159
|
+
if (!isSafeEnvFileName(input.fileName)) return { changed: false };
|
|
160
|
+
const target = path.join(input.root, input.fileName);
|
|
161
|
+
try {
|
|
162
|
+
const existing = await fs.readFile(target, 'utf-8');
|
|
163
|
+
const next = removeEnvKeys(existing, input.keys, { values: input.values });
|
|
164
|
+
if (next === existing) return { changed: false };
|
|
165
|
+
await fs.writeFile(target, next, 'utf-8');
|
|
166
|
+
return { changed: true };
|
|
167
|
+
} catch {
|
|
168
|
+
// The file is gone, or was never written. Either way there is no block.
|
|
169
|
+
return { changed: false };
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function messageOf(error: unknown): string {
|
|
174
|
+
return error instanceof Error ? error.message : String(error);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Worktree branching's write, in its own words.
|
|
179
|
+
*
|
|
180
|
+
* IN PLACE, like every other write here. A worktree is a FILE COPY of the
|
|
181
|
+
* project and therefore already carries the project's own `DATABASE_URL` — so
|
|
182
|
+
* an earlier version appended a marked block at the end and relied on readers
|
|
183
|
+
* taking the last assignment. It worked and it read as if it did not: the
|
|
184
|
+
* project's value sat untouched at the top of the file, and anyone opening it
|
|
185
|
+
* saw their own database named first. Replacing the line where it stands says
|
|
186
|
+
* what is true, and the previous value survives as the comment above it.
|
|
187
|
+
*
|
|
188
|
+
* A tracked file is refused outright with no override, because the danger there
|
|
189
|
+
* is not a committed password but "Apply to Main" overwriting the real database
|
|
190
|
+
* URL with a disposable one.
|
|
191
|
+
*/
|
|
192
|
+
export async function writeWorktreeEnv(input: {
|
|
193
|
+
worktreeRoot: string;
|
|
194
|
+
fileName: string;
|
|
195
|
+
vars: Record<string, string>;
|
|
196
|
+
note?: string;
|
|
197
|
+
}): Promise<{ status: 'written' | 'skipped-tracked' | 'failed'; detail: string | null }> {
|
|
198
|
+
const result = await writeEnvVars({
|
|
199
|
+
root: input.worktreeRoot,
|
|
200
|
+
fileName: input.fileName,
|
|
201
|
+
vars: input.vars,
|
|
202
|
+
note: input.note
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
if (result.status === 'skipped-tracked') {
|
|
206
|
+
return {
|
|
207
|
+
status: 'skipped-tracked',
|
|
208
|
+
detail: `git tracks ${input.fileName}, so writing to it would carry the branch connection string into the main project — add it to .gitignore or point branching at a file that is ignored.`
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
// "Unchanged" means the block already said exactly this, which is a write
|
|
212
|
+
// that succeeded — reporting it as anything else would light up the retry.
|
|
213
|
+
if (result.status === 'unchanged') return { status: 'written', detail: null };
|
|
214
|
+
return { status: result.status, detail: result.detail };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Put a worktree's dotenv file back the way branching found it. */
|
|
218
|
+
export async function clearWorktreeEnv(
|
|
219
|
+
worktreeRoot: string,
|
|
220
|
+
fileName: string,
|
|
221
|
+
vars: Record<string, string> = {}
|
|
222
|
+
): Promise<void> {
|
|
223
|
+
await clearEnvVars({
|
|
224
|
+
root: worktreeRoot,
|
|
225
|
+
fileName,
|
|
226
|
+
keys: Object.keys(vars),
|
|
227
|
+
values: vars
|
|
228
|
+
});
|
|
229
|
+
}
|