@myrialabs/clopen 0.4.38 → 0.4.40
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/backend/auth/permissions.ts +48 -0
- package/backend/chat/stream-manager.ts +1 -1
- package/backend/database/crypto/secret-columns.ts +9 -1
- package/backend/database/migrations/075_create_deploy_bindings.ts +54 -0
- package/backend/database/migrations/076_remove_memory_code_graph.ts +229 -0
- package/backend/database/migrations/077_create_integration_db_links.ts +150 -0
- package/backend/database/migrations/078_create_worktree_branch_tables.ts +158 -0
- package/backend/database/migrations/index.ts +28 -0
- package/backend/database/queries/db-client-connection-queries.ts +19 -0
- package/backend/database/queries/deploy-queries.ts +124 -0
- package/backend/database/queries/graph-queries.ts +291 -247
- package/backend/database/queries/index.ts +10 -0
- package/backend/database/queries/integration-db-link-queries.ts +168 -0
- package/backend/database/queries/integration-queries.ts +34 -6
- 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 +44 -0
- package/backend/db-client/integrations/links.ts +564 -0
- package/backend/db-client/integrations/projector.test.ts +280 -0
- package/backend/db-client/integrations/projector.ts +256 -0
- package/backend/db-client/integrations/supabase-service.ts +266 -0
- 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/registry.ts +27 -0
- package/backend/db-client/providers/supabase/adapter.ts +523 -0
- package/backend/db-client/providers/supabase/api-types.ts +92 -0
- package/backend/db-client/providers/supabase/apply-migration.ts +104 -0
- package/backend/db-client/providers/supabase/client.ts +259 -0
- package/backend/db-client/providers/supabase/features.ts +502 -0
- package/backend/db-client/providers/supabase/local.test.ts +78 -0
- package/backend/db-client/providers/supabase/local.ts +137 -0
- package/backend/db-client/providers/types.ts +194 -0
- package/backend/deployments/bindings.test.ts +43 -0
- package/backend/deployments/bindings.ts +216 -0
- package/backend/deployments/index.ts +53 -0
- package/backend/deployments/log-streams.ts +214 -0
- package/backend/deployments/prompts.ts +70 -0
- package/backend/deployments/providers/vercel/adapter.ts +757 -0
- package/backend/deployments/providers/vercel/build-logs.test.ts +80 -0
- package/backend/deployments/providers/vercel/build-logs.ts +211 -0
- package/backend/deployments/providers/vercel/client.ts +203 -0
- package/backend/deployments/providers/vercel/list.test.ts +624 -0
- package/backend/deployments/providers/vercel/normalise.test.ts +115 -0
- package/backend/deployments/providers/vercel/normalise.ts +113 -0
- package/backend/deployments/providers/vercel/types.ts +104 -0
- package/backend/deployments/registry.ts +27 -0
- package/backend/deployments/service.ts +458 -0
- package/backend/deployments/types.ts +278 -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/index.ts +4 -0
- package/backend/integrations/health.ts +18 -4
- package/backend/integrations/projections/index.ts +0 -0
- package/backend/integrations/projections/mcp-projector.ts +7 -3
- package/backend/integrations/projections/types.ts +28 -9
- package/backend/integrations/registry/index.ts +8 -1
- 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 +62 -0
- package/backend/integrations/registry/providers/vercel.ts +47 -0
- package/backend/integrations/registry/types.ts +2 -0
- package/backend/mcp/internal/servers/memory-graph/description.ts +3 -3
- package/backend/mcp/internal/servers/memory-graph/runner.ts +4 -9
- package/backend/mcp/internal/servers/memory-graph/schema.ts +0 -4
- package/backend/memory/README.md +57 -34
- package/backend/memory/compose.test.ts +3 -3
- package/backend/memory/compose.ts +6 -9
- package/backend/memory/config.ts +0 -5
- package/backend/memory/consolidate.ts +1 -2
- package/backend/memory/context.test.ts +8 -54
- package/backend/memory/context.ts +19 -25
- package/backend/memory/extract/episodic.ts +10 -12
- package/backend/memory/extract/index.ts +23 -52
- package/backend/memory/extract/scheduler.ts +3 -6
- package/backend/memory/index.ts +6 -5
- package/backend/memory/invalidate.ts +36 -48
- package/backend/memory/layout.ts +319 -130
- package/backend/memory/retention.ts +12 -42
- package/backend/memory/retrieval.eval.test.ts +2 -2
- package/backend/memory/retrieval.test.ts +228 -183
- package/backend/memory/retrieval.ts +22 -21
- package/backend/memory/revise.test.ts +62 -116
- package/backend/memory/revise.ts +0 -1
- package/backend/memory/view.ts +8 -5
- package/backend/snapshot/helpers.ts +35 -41
- package/backend/snapshot/snapshot-service.ts +90 -0
- package/backend/snapshot/turn-changes.test.ts +163 -0
- package/backend/snapshot/turn-changes.ts +131 -0
- 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/db-client/accounts.ts +225 -0
- package/backend/ws/db-client/connections.ts +34 -3
- package/backend/ws/db-client/context.ts +37 -0
- package/backend/ws/db-client/env.ts +142 -0
- package/backend/ws/db-client/index.ts +22 -1
- package/backend/ws/db-client/supabase.ts +188 -0
- package/backend/ws/deployments/actions.ts +360 -0
- package/backend/ws/deployments/context.ts +43 -0
- package/backend/ws/deployments/crud.ts +152 -0
- package/backend/ws/deployments/index.ts +15 -0
- package/backend/ws/deployments/schemas.ts +183 -0
- package/backend/ws/index.ts +6 -2
- package/backend/ws/memory/crud.ts +4 -16
- 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/snapshot/changes.ts +279 -0
- package/backend/ws/snapshot/index.ts +4 -3
- package/backend/ws/system/device-info.ts +307 -76
- 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-DSHIANSy.js → index-D4igjF6e.js} +1 -1
- package/dist/assets/{index-CyH7fSbR.js → index-DONNwiXm.js} +1 -1
- package/dist/assets/main-BrfXH1bL.js +1205 -0
- package/dist/assets/main-CT1PygsW.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/constants/tool-icons.ts +13 -0
- package/shared/types/db-client/connection.ts +6 -0
- package/shared/types/db-client/env.ts +192 -0
- package/shared/types/db-client/index.ts +3 -0
- package/shared/types/db-client/integrations.ts +204 -0
- package/shared/types/db-client/supabase.ts +131 -0
- package/shared/types/deployments.ts +382 -0
- package/shared/types/integrations.ts +9 -0
- package/shared/types/memory.ts +34 -51
- package/shared/types/ui/components.ts +1 -1
- package/shared/types/worktree-branching.ts +315 -0
- package/shared/utils/diff-calculator.test.ts +89 -0
- package/shared/utils/diff-calculator.ts +30 -65
- package/shared/utils/logger.ts +2 -0
- package/backend/memory/extract/languages.ts +0 -176
- package/backend/memory/extract/structural.test.ts +0 -215
- package/backend/memory/extract/structural.ts +0 -364
- package/backend/ws/snapshot/content.ts +0 -85
- package/dist/assets/main-D2IOjftb.css +0 -1
- package/dist/assets/main-DM-tHKbW.js +0 -1152
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the shared dotenv module.
|
|
3
|
+
*
|
|
4
|
+
* The cases here are the ones that destroy something when they are wrong:
|
|
5
|
+
*
|
|
6
|
+
* - A value replaced IN PLACE must stay where it was, and the old one must
|
|
7
|
+
* survive as a comment. This is someone's configuration file, and the only
|
|
8
|
+
* reason it is safe to edit is that they can get the previous value back.
|
|
9
|
+
* - Backups must not stack, or a file written weekly grows a dead line a week.
|
|
10
|
+
* - A variable the file already has must be replaced WHERE IT IS. Appending a
|
|
11
|
+
* second copy at the end works — readers take the last assignment — and
|
|
12
|
+
* reads as if it does not, because the project's own value is still sitting
|
|
13
|
+
* at the top of the file.
|
|
14
|
+
* - Everything around either write must survive untouched.
|
|
15
|
+
* - A tracked file must be REFUSED unless the caller explicitly allows it:
|
|
16
|
+
* writing one commits a live database password, and for a worktree it also
|
|
17
|
+
* carries a disposable connection string into the main project.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
|
|
21
|
+
import fs from 'fs/promises';
|
|
22
|
+
import os from 'os';
|
|
23
|
+
import path from 'path';
|
|
24
|
+
import { execGit } from '$backend/git/git-executor';
|
|
25
|
+
import { formatEnvLine, isSafeEnvFileName, isValidEnvKey } from './keys';
|
|
26
|
+
import { effectiveAssignment, parseDotenv } from './parse';
|
|
27
|
+
import { PREVIOUS_VALUE_PREFIX, removeEnvKeys, upsertEnvKeys } from './edit';
|
|
28
|
+
import { listDotenvFiles, listEnvFileEntries } from './discovery';
|
|
29
|
+
import { clearWorktreeEnv, planEnvWrite, writeEnvVars, writeWorktreeEnv } from './write';
|
|
30
|
+
|
|
31
|
+
const URI = 'postgresql://owner:pw@ep-x.eu-central-1.aws.neon.tech/neondb?sslmode=require';
|
|
32
|
+
|
|
33
|
+
let root: string;
|
|
34
|
+
|
|
35
|
+
beforeEach(async () => {
|
|
36
|
+
root = await fs.mkdtemp(path.join(os.tmpdir(), 'clopen-envfile-'));
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
afterEach(async () => {
|
|
40
|
+
await fs.rm(root, { recursive: true, force: true });
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
async function initRepo(): Promise<void> {
|
|
44
|
+
await execGit(['init'], root);
|
|
45
|
+
await execGit(['config', 'user.email', 'test@example.com'], root);
|
|
46
|
+
await execGit(['config', 'user.name', 'Test'], root);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
describe('formatEnvLine', () => {
|
|
50
|
+
it('leaves a connection URI unquoted', () => {
|
|
51
|
+
// A URI is percent-encoded, so the common case needs no quoting — and an
|
|
52
|
+
// unquoted value is what a human expects to read.
|
|
53
|
+
expect(formatEnvLine('DATABASE_URL', URI)).toBe(`DATABASE_URL=${URI}`);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('quotes a value that would otherwise become a comment', () => {
|
|
57
|
+
expect(formatEnvLine('K', 'a#b')).toBe('K="a#b"');
|
|
58
|
+
expect(formatEnvLine('K', 'has space')).toBe('K="has space"');
|
|
59
|
+
expect(formatEnvLine('K', '')).toBe('K=""');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('escapes what the quotes would otherwise end', () => {
|
|
63
|
+
expect(formatEnvLine('K', 'say "hi"')).toBe('K="say \\"hi\\""');
|
|
64
|
+
expect(formatEnvLine('K', 'back\\slash')).toBe('K="back\\\\slash"');
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('isValidEnvKey', () => {
|
|
69
|
+
it('accepts shell-legal names and refuses the rest', () => {
|
|
70
|
+
expect(isValidEnvKey('DATABASE_URL')).toBe(true);
|
|
71
|
+
expect(isValidEnvKey('_x1')).toBe(true);
|
|
72
|
+
expect(isValidEnvKey('1DB')).toBe(false);
|
|
73
|
+
expect(isValidEnvKey('DB-URL')).toBe(false);
|
|
74
|
+
expect(isValidEnvKey('')).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('parseDotenv', () => {
|
|
79
|
+
it('records where each assignment lives, and how far it runs', () => {
|
|
80
|
+
const content = ['A=1', 'export B="two"', 'C=3'].join('\n');
|
|
81
|
+
const parsed = parseDotenv(content);
|
|
82
|
+
|
|
83
|
+
expect(parsed.map((entry) => entry.key)).toEqual(['A', 'B', 'C']);
|
|
84
|
+
expect(parsed[1]).toMatchObject({ value: 'two', line: 1, lineCount: 1, exported: true });
|
|
85
|
+
expect(parsed[2].line).toBe(2);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('reads a quoted value that runs past its first line', () => {
|
|
89
|
+
// A multi-line certificate or private key is the real case, and a parser
|
|
90
|
+
// that stopped at the newline would report the next line as a new key.
|
|
91
|
+
const content = 'KEY="line one\nline two"\nAFTER=1';
|
|
92
|
+
const parsed = parseDotenv(content);
|
|
93
|
+
|
|
94
|
+
expect(parsed[0]).toMatchObject({ key: 'KEY', value: 'line one\nline two', lineCount: 2 });
|
|
95
|
+
expect(parsed[1]).toMatchObject({ key: 'AFTER', line: 2 });
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('strips an inline comment from an unquoted value but not a quoted one', () => {
|
|
99
|
+
expect(parseDotenv('A=1 # why')[0].value).toBe('1');
|
|
100
|
+
expect(parseDotenv('A="1 # why"')[0].value).toBe('1 # why');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('marks a commented assignment instead of ignoring it', () => {
|
|
104
|
+
// `.env.example` documents optional settings as `# REDIS_URL=`, and that
|
|
105
|
+
// file is often the only record of the names a project expects.
|
|
106
|
+
const parsed = parseDotenv('# REDIS_URL=redis://localhost\nA=1');
|
|
107
|
+
expect(parsed[0]).toMatchObject({ key: 'REDIS_URL', commented: true });
|
|
108
|
+
expect(parsed[1]).toMatchObject({ key: 'A', commented: false });
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('does not read prose as an assignment', () => {
|
|
112
|
+
expect(parseDotenv('# the value = whatever you like')).toEqual([]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('takes the LAST uncommented assignment as the effective one', () => {
|
|
116
|
+
// Which is what every reader in common use does, and therefore what
|
|
117
|
+
// decides whether writing a variable has any effect at all.
|
|
118
|
+
const parsed = parseDotenv('A=first\n# A=commented\nA=second');
|
|
119
|
+
expect(effectiveAssignment(parsed, 'A')?.value).toBe('second');
|
|
120
|
+
expect(effectiveAssignment(parsed, 'MISSING')).toBe(null);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('upsertEnvKeys — in place', () => {
|
|
125
|
+
it('replaces the value where it stands and keeps the old one as a comment', () => {
|
|
126
|
+
const existing = ['# my config', 'DATABASE_URL=postgres://old', 'OTHER=1'].join('\n');
|
|
127
|
+
const result = upsertEnvKeys(existing, { DATABASE_URL: URI });
|
|
128
|
+
|
|
129
|
+
expect(result.updated).toEqual(['DATABASE_URL']);
|
|
130
|
+
expect(result.appended).toEqual([]);
|
|
131
|
+
// Still on the same line, still between the comment and OTHER.
|
|
132
|
+
const lines = result.text.split('\n');
|
|
133
|
+
expect(lines[0]).toBe('# my config');
|
|
134
|
+
expect(lines[1]).toBe(`${PREVIOUS_VALUE_PREFIX}DATABASE_URL=postgres://old`);
|
|
135
|
+
expect(lines[2]).toBe(`DATABASE_URL=${URI}`);
|
|
136
|
+
expect(result.text).toContain('OTHER=1');
|
|
137
|
+
// Nothing was appended, so the file did not grow a second section.
|
|
138
|
+
expect(result.text.split('\n')).toHaveLength(4);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('does not stack a second backup on the next write', () => {
|
|
142
|
+
const once = upsertEnvKeys('DATABASE_URL=one', { DATABASE_URL: 'two' });
|
|
143
|
+
const twice = upsertEnvKeys(once.text, { DATABASE_URL: 'three' });
|
|
144
|
+
|
|
145
|
+
expect(twice.text.match(new RegExp(PREVIOUS_VALUE_PREFIX, 'g'))).toHaveLength(1);
|
|
146
|
+
expect(twice.text).toContain('DATABASE_URL=three');
|
|
147
|
+
expect(twice.text).toContain(`${PREVIOUS_VALUE_PREFIX}DATABASE_URL=two`);
|
|
148
|
+
expect(twice.text).not.toContain('DATABASE_URL=one');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('leaves a backup belonging to a key it is not writing', () => {
|
|
152
|
+
const existing = [
|
|
153
|
+
`${PREVIOUS_VALUE_PREFIX}REDIS_URL=redis://old`,
|
|
154
|
+
'REDIS_URL=redis://new',
|
|
155
|
+
'DATABASE_URL=one'
|
|
156
|
+
].join('\n');
|
|
157
|
+
const result = upsertEnvKeys(existing, { DATABASE_URL: 'two' });
|
|
158
|
+
|
|
159
|
+
expect(result.text).toContain(`${PREVIOUS_VALUE_PREFIX}REDIS_URL=redis://old`);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('reports a key that already holds the value and touches nothing', () => {
|
|
163
|
+
const existing = 'DATABASE_URL=same\n';
|
|
164
|
+
const result = upsertEnvKeys(existing, { DATABASE_URL: 'same' });
|
|
165
|
+
|
|
166
|
+
expect(result.unchanged).toEqual(['DATABASE_URL']);
|
|
167
|
+
expect(result.text).toBe(existing);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('updates the LAST assignment, which is the one that wins', () => {
|
|
171
|
+
const result = upsertEnvKeys('DB=first\nDB=second', { DB: 'third' });
|
|
172
|
+
|
|
173
|
+
expect(result.text).toContain('DB=first');
|
|
174
|
+
expect(result.text).toContain('DB=third');
|
|
175
|
+
expect(result.text.indexOf('DB=third')).toBeGreaterThan(result.text.indexOf('DB=first'));
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('ignores a commented assignment and appends instead', () => {
|
|
179
|
+
// `# DATABASE_URL=` is documentation. Uncommenting it would turn a note
|
|
180
|
+
// into configuration the user never asked to switch on.
|
|
181
|
+
const result = upsertEnvKeys('# DATABASE_URL=postgres://example', { DATABASE_URL: URI });
|
|
182
|
+
|
|
183
|
+
expect(result.appended).toEqual(['DATABASE_URL']);
|
|
184
|
+
expect(result.text).toContain('# DATABASE_URL=postgres://example');
|
|
185
|
+
expect(result.text).toContain(`DATABASE_URL=${URI}`);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('replaces a multi-line quoted value without eating the line after it', () => {
|
|
189
|
+
const existing = 'CERT="a\nb"\nDATABASE_URL=old\nKEEP=1';
|
|
190
|
+
const result = upsertEnvKeys(existing, { DATABASE_URL: 'new' });
|
|
191
|
+
|
|
192
|
+
expect(result.text).toContain('CERT="a\nb"');
|
|
193
|
+
expect(result.text).toContain('DATABASE_URL=new');
|
|
194
|
+
expect(result.text).toContain('KEEP=1');
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('splits a mixed write between replacing and appending', () => {
|
|
198
|
+
const result = upsertEnvKeys(
|
|
199
|
+
'DB_HOST=localhost',
|
|
200
|
+
{ DB_HOST: 'db.example.com', DB_PASSWORD: 'pw' },
|
|
201
|
+
{ note: 'from DB Client' }
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
expect(result.updated).toEqual(['DB_HOST']);
|
|
205
|
+
expect(result.appended).toEqual(['DB_PASSWORD']);
|
|
206
|
+
expect(result.text).toContain('DB_HOST=db.example.com');
|
|
207
|
+
expect(result.text).toContain('# from DB Client');
|
|
208
|
+
expect(result.text).toContain('DB_PASSWORD=pw');
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe('Windows line endings', () => {
|
|
213
|
+
it('parses a CRLF file at all', () => {
|
|
214
|
+
// `.` does not match `\r` in JavaScript, so a pattern ending in `.*$` matched
|
|
215
|
+
// NOTHING on a file written on Windows — detection found no variables and
|
|
216
|
+
// every write appended a duplicate of a key that was already there.
|
|
217
|
+
const parsed = parseDotenv('APP=1\r\nDATABASE_URL=postgres://old\r\n');
|
|
218
|
+
expect(parsed.map((entry) => [entry.key, entry.value])).toEqual([
|
|
219
|
+
['APP', '1'],
|
|
220
|
+
['DATABASE_URL', 'postgres://old']
|
|
221
|
+
]);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('keeps CRLF through a write and an undo', () => {
|
|
225
|
+
const original = 'APP=1\r\nDATABASE_URL=postgres://old\r\n';
|
|
226
|
+
const written = upsertEnvKeys(original, { DATABASE_URL: URI });
|
|
227
|
+
|
|
228
|
+
expect(written.updated).toEqual(['DATABASE_URL']);
|
|
229
|
+
expect(written.text).not.toMatch(/[^\r]\n/);
|
|
230
|
+
expect(removeEnvKeys(written.text, ['DATABASE_URL'])).toBe(original);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
describe('upsertEnvKeys — appending', () => {
|
|
235
|
+
it('appends plain lines, with nothing announcing itself', () => {
|
|
236
|
+
// What a dotenv file should look like after a variable is added to it is a
|
|
237
|
+
// dotenv file with the variable in it. The markers exist to tell Clopen's
|
|
238
|
+
// lines from the user's, and removal keeps that guarantee without them.
|
|
239
|
+
const result = upsertEnvKeys('APP=1\n', { DATABASE_URL: URI });
|
|
240
|
+
|
|
241
|
+
expect(result.appended).toEqual(['DATABASE_URL']);
|
|
242
|
+
expect(result.text).toBe(`APP=1\n\nDATABASE_URL=${URI}\n`);
|
|
243
|
+
expect(result.text).not.toContain('>>>');
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('writes into an empty file without a leading blank line', () => {
|
|
247
|
+
expect(upsertEnvKeys('', { A: '1' }).text).toBe('A=1\n');
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('still replaces in place, so nothing is duplicated', () => {
|
|
251
|
+
const result = upsertEnvKeys('DATABASE_URL=old\nAPP=1\n', { DATABASE_URL: 'new' });
|
|
252
|
+
|
|
253
|
+
expect(result.updated).toEqual(['DATABASE_URL']);
|
|
254
|
+
expect(result.text).toContain('DATABASE_URL=new');
|
|
255
|
+
expect(result.text.match(/^DATABASE_URL=/gm)).toHaveLength(1);
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
describe('removeEnvKeys', () => {
|
|
260
|
+
it('removes what it appended and restores what it replaced', () => {
|
|
261
|
+
const original = 'APP=1\nDATABASE_URL=postgres://mine\n';
|
|
262
|
+
const written = upsertEnvKeys(original, {
|
|
263
|
+
DATABASE_URL: URI,
|
|
264
|
+
REDIS_URL: 'redis://x'
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
const restored = removeEnvKeys(written.text, ['DATABASE_URL', 'REDIS_URL'], {
|
|
268
|
+
values: { DATABASE_URL: URI, REDIS_URL: 'redis://x' }
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
expect(restored).toContain('DATABASE_URL=postgres://mine');
|
|
272
|
+
expect(restored).not.toContain('REDIS_URL');
|
|
273
|
+
expect(restored).not.toContain(URI);
|
|
274
|
+
expect(restored).toContain('APP=1');
|
|
275
|
+
expect(restored).not.toContain(PREVIOUS_VALUE_PREFIX);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('LEAVES a value the user has edited since', () => {
|
|
279
|
+
// Without the block there is no marker saying the line is ours, so the
|
|
280
|
+
// value is the evidence. One the user has changed is theirs now, and
|
|
281
|
+
// deleting it would be this taking away a variable it did not put there.
|
|
282
|
+
const written = upsertEnvKeys('APP=1\n', { REDIS_URL: 'redis://ours' });
|
|
283
|
+
const edited = written.text.replace('redis://ours', 'redis://theirs');
|
|
284
|
+
|
|
285
|
+
const result = removeEnvKeys(edited, ['REDIS_URL'], {
|
|
286
|
+
values: { REDIS_URL: 'redis://ours' }
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
expect(result).toContain('REDIS_URL=redis://theirs');
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('does not touch a key it was never given a value for', () => {
|
|
293
|
+
const content = 'DATABASE_URL=postgres://theirs\n';
|
|
294
|
+
expect(removeEnvKeys(content, ['DATABASE_URL'])).toBe(content);
|
|
295
|
+
});
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
describe('listEnvFileEntries / listDotenvFiles', () => {
|
|
299
|
+
it('puts the highest-precedence file first', async () => {
|
|
300
|
+
await fs.writeFile(path.join(root, '.env'), 'A=1');
|
|
301
|
+
await fs.writeFile(path.join(root, '.env.local'), 'A=2');
|
|
302
|
+
|
|
303
|
+
expect(await listDotenvFiles(root)).toEqual(['.env.local', '.env']);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('leaves templates out of the writable list but reports them separately', async () => {
|
|
307
|
+
await fs.writeFile(path.join(root, '.env'), 'A=1');
|
|
308
|
+
await fs.writeFile(path.join(root, '.env.example'), 'A=');
|
|
309
|
+
|
|
310
|
+
expect(await listDotenvFiles(root)).toEqual(['.env']);
|
|
311
|
+
expect(await listEnvFileEntries(root)).toEqual([
|
|
312
|
+
{ name: '.env', isTemplate: false },
|
|
313
|
+
{ name: '.env.example', isTemplate: true }
|
|
314
|
+
]);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it('answers empty for a directory that does not exist', async () => {
|
|
318
|
+
expect(await listDotenvFiles(path.join(root, 'nope'))).toEqual([]);
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
describe('writeEnvVars', () => {
|
|
323
|
+
it('writes, and reports the file as unchanged on a second identical write', async () => {
|
|
324
|
+
await initRepo();
|
|
325
|
+
await fs.writeFile(path.join(root, '.gitignore'), '.env\n');
|
|
326
|
+
|
|
327
|
+
const first = await writeEnvVars({
|
|
328
|
+
root,
|
|
329
|
+
fileName: '.env',
|
|
330
|
+
vars: { DATABASE_URL: URI },
|
|
331
|
+
note: 'DB Client'
|
|
332
|
+
});
|
|
333
|
+
expect(first.status).toBe('written');
|
|
334
|
+
expect(first.appended).toEqual(['DATABASE_URL']);
|
|
335
|
+
|
|
336
|
+
const second = await writeEnvVars({
|
|
337
|
+
root,
|
|
338
|
+
fileName: '.env',
|
|
339
|
+
vars: { DATABASE_URL: URI },
|
|
340
|
+
note: 'DB Client'
|
|
341
|
+
});
|
|
342
|
+
expect(second.status).toBe('unchanged');
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('REFUSES a file git tracks, and says why', async () => {
|
|
346
|
+
await initRepo();
|
|
347
|
+
await fs.writeFile(path.join(root, '.env'), 'KEEP=me\n');
|
|
348
|
+
await execGit(['add', '.env'], root);
|
|
349
|
+
await execGit(['commit', '-m', 'add env'], root);
|
|
350
|
+
|
|
351
|
+
const result = await writeEnvVars({
|
|
352
|
+
root,
|
|
353
|
+
fileName: '.env',
|
|
354
|
+
vars: { DATABASE_URL: URI },
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
expect(result.status).toBe('skipped-tracked');
|
|
358
|
+
expect(result.detail).toContain('.gitignore');
|
|
359
|
+
// Untouched — the refusal is the point.
|
|
360
|
+
expect(await fs.readFile(path.join(root, '.env'), 'utf-8')).toBe('KEEP=me\n');
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it('writes a tracked file when the caller explicitly allows it', async () => {
|
|
364
|
+
await initRepo();
|
|
365
|
+
await fs.writeFile(path.join(root, '.env'), 'KEEP=me\n');
|
|
366
|
+
await execGit(['add', '.env'], root);
|
|
367
|
+
await execGit(['commit', '-m', 'add env'], root);
|
|
368
|
+
|
|
369
|
+
const result = await writeEnvVars({
|
|
370
|
+
root,
|
|
371
|
+
fileName: '.env',
|
|
372
|
+
vars: { DATABASE_URL: URI },
|
|
373
|
+
allowTracked: true
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
expect(result.status).toBe('written');
|
|
377
|
+
expect(await fs.readFile(path.join(root, '.env'), 'utf-8')).toContain(URI);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it('refuses an unsafe file name without touching the disk', async () => {
|
|
381
|
+
const result = await writeEnvVars({
|
|
382
|
+
root,
|
|
383
|
+
fileName: '../escape',
|
|
384
|
+
vars: { K: 'V' },
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
expect(result.status).toBe('failed');
|
|
388
|
+
await expect(fs.access(path.join(root, '..', 'escape'))).rejects.toThrow();
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
describe('planEnvWrite', () => {
|
|
393
|
+
it('describes the write without performing it', async () => {
|
|
394
|
+
await fs.writeFile(path.join(root, '.env'), 'DATABASE_URL=old\n');
|
|
395
|
+
|
|
396
|
+
const plan = await planEnvWrite({
|
|
397
|
+
root,
|
|
398
|
+
fileName: '.env',
|
|
399
|
+
vars: { DATABASE_URL: URI },
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
expect(plan.exists).toBe(true);
|
|
403
|
+
expect(plan.updated).toEqual(['DATABASE_URL']);
|
|
404
|
+
expect(plan.next).toContain(URI);
|
|
405
|
+
// The file itself is still as it was.
|
|
406
|
+
expect(await fs.readFile(path.join(root, '.env'), 'utf-8')).toBe('DATABASE_URL=old\n');
|
|
407
|
+
});
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
describe('writeWorktreeEnv', () => {
|
|
411
|
+
it('writes the block and preserves the rest of the file', async () => {
|
|
412
|
+
await initRepo();
|
|
413
|
+
await fs.writeFile(path.join(root, '.gitignore'), '.env\n');
|
|
414
|
+
await fs.writeFile(path.join(root, '.env'), 'KEEP=me\n');
|
|
415
|
+
|
|
416
|
+
const result = await writeWorktreeEnv({
|
|
417
|
+
worktreeRoot: root,
|
|
418
|
+
fileName: '.env',
|
|
419
|
+
vars: { DATABASE_URL: URI },
|
|
420
|
+
note: 'Neon branch "clopen/acme/x"'
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
expect(result.status).toBe('written');
|
|
424
|
+
const contents = await fs.readFile(path.join(root, '.env'), 'utf-8');
|
|
425
|
+
expect(contents).toContain('KEEP=me');
|
|
426
|
+
expect(contents).toContain(`DATABASE_URL=${URI}`);
|
|
427
|
+
expect(contents).toContain('Neon branch');
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it('REPLACES the inherited copy rather than appending past it', async () => {
|
|
431
|
+
// A worktree is a FILE COPY of the project and carries the project's own
|
|
432
|
+
// `DATABASE_URL`. An earlier version appended a block at the end and let
|
|
433
|
+
// the last-assignment rule win: it worked, and it read as if it did not,
|
|
434
|
+
// because the project's own database was still the first thing in the
|
|
435
|
+
// file. The value is replaced where it stands and the old one survives as
|
|
436
|
+
// the comment above it.
|
|
437
|
+
await initRepo();
|
|
438
|
+
await fs.writeFile(path.join(root, '.gitignore'), '.env\n');
|
|
439
|
+
await fs.writeFile(
|
|
440
|
+
path.join(root, '.env'),
|
|
441
|
+
'# Database\nDATABASE_URL=postgres://main\nPORT=3000\n'
|
|
442
|
+
);
|
|
443
|
+
|
|
444
|
+
await writeWorktreeEnv({
|
|
445
|
+
worktreeRoot: root,
|
|
446
|
+
fileName: '.env',
|
|
447
|
+
vars: { DATABASE_URL: URI }
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
const contents = await fs.readFile(path.join(root, '.env'), 'utf-8');
|
|
451
|
+
expect(contents).toBe(
|
|
452
|
+
`# Database\n${PREVIOUS_VALUE_PREFIX}DATABASE_URL=postgres://main\nDATABASE_URL=${URI}\nPORT=3000\n`
|
|
453
|
+
);
|
|
454
|
+
// Nothing announcing itself, and nothing appended past the end.
|
|
455
|
+
expect(contents).not.toContain('>>>');
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
it('REFUSES a file git tracks, with the reason branching cares about', async () => {
|
|
459
|
+
await initRepo();
|
|
460
|
+
await fs.writeFile(path.join(root, '.env'), 'KEEP=me\n');
|
|
461
|
+
await execGit(['add', '.env'], root);
|
|
462
|
+
await execGit(['commit', '-m', 'add env'], root);
|
|
463
|
+
|
|
464
|
+
const result = await writeWorktreeEnv({
|
|
465
|
+
worktreeRoot: root,
|
|
466
|
+
fileName: '.env',
|
|
467
|
+
vars: { DATABASE_URL: URI }
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
expect(result.status).toBe('skipped-tracked');
|
|
471
|
+
expect(result.detail).toContain('main project');
|
|
472
|
+
expect(await fs.readFile(path.join(root, '.env'), 'utf-8')).toBe('KEEP=me\n');
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
it('reports an identical rewrite as written, not as something to retry', async () => {
|
|
476
|
+
await initRepo();
|
|
477
|
+
await fs.writeFile(path.join(root, '.gitignore'), '.env\n');
|
|
478
|
+
|
|
479
|
+
const vars = { DATABASE_URL: URI };
|
|
480
|
+
await writeWorktreeEnv({ worktreeRoot: root, fileName: '.env', vars });
|
|
481
|
+
const again = await writeWorktreeEnv({ worktreeRoot: root, fileName: '.env', vars });
|
|
482
|
+
|
|
483
|
+
expect(again.status).toBe('written');
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
it('refuses an unsafe file name without touching the disk', async () => {
|
|
487
|
+
const result = await writeWorktreeEnv({
|
|
488
|
+
worktreeRoot: root,
|
|
489
|
+
fileName: '../escape',
|
|
490
|
+
vars: { K: 'V' }
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
expect(result.status).toBe('failed');
|
|
494
|
+
await expect(fs.access(path.join(root, '..', 'escape'))).rejects.toThrow();
|
|
495
|
+
});
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
describe('clearWorktreeEnv', () => {
|
|
499
|
+
it('puts the project’s own value back', async () => {
|
|
500
|
+
await initRepo();
|
|
501
|
+
await fs.writeFile(path.join(root, '.gitignore'), '.env\n');
|
|
502
|
+
const target = path.join(root, '.env');
|
|
503
|
+
await fs.writeFile(target, 'KEEP=me\nDATABASE_URL=postgres://main\n');
|
|
504
|
+
await writeWorktreeEnv({ worktreeRoot: root, fileName: '.env', vars: { DATABASE_URL: URI } });
|
|
505
|
+
|
|
506
|
+
await clearWorktreeEnv(root, '.env', { DATABASE_URL: URI });
|
|
507
|
+
|
|
508
|
+
expect(await fs.readFile(target, 'utf-8')).toBe('KEEP=me\nDATABASE_URL=postgres://main\n');
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
it('does nothing when there is no file', async () => {
|
|
512
|
+
await clearWorktreeEnv(root, '.env');
|
|
513
|
+
await expect(fs.access(path.join(root, '.env'))).rejects.toThrow();
|
|
514
|
+
});
|
|
515
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether git tracks a dotenv file — the one question that can refuse a write.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { execGit } from '$backend/git/git-executor';
|
|
6
|
+
import { debug } from '$shared/utils/logger';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* `ls-files --error-unmatch` exits non-zero for an untracked path, which is the
|
|
10
|
+
* answer we want rather than an error. A directory that is not a repository at
|
|
11
|
+
* all also lands here, and "not tracked" is the correct reading: there is no
|
|
12
|
+
* commit that could carry the file anywhere.
|
|
13
|
+
*/
|
|
14
|
+
export async function isTrackedInGit(root: string, fileName: string): Promise<boolean> {
|
|
15
|
+
try {
|
|
16
|
+
const result = await execGit(['ls-files', '--error-unmatch', '--', fileName], root, {
|
|
17
|
+
okExitCodes: [1, 128]
|
|
18
|
+
});
|
|
19
|
+
return result.exitCode === 0;
|
|
20
|
+
} catch (error) {
|
|
21
|
+
// git missing, or a path git refuses to read. Treating an unknown answer
|
|
22
|
+
// as "tracked" is the safe direction: the cost is a variable the user has
|
|
23
|
+
// to paste once, against the cost of committing a live password.
|
|
24
|
+
debug.warn('env-files', `Could not check whether ${fileName} is tracked: ${error}`);
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dotenv files — the shared primitive.
|
|
3
|
+
*
|
|
4
|
+
* Two features write a connection string into a project's environment, and
|
|
5
|
+
* both go through here:
|
|
6
|
+
*
|
|
7
|
+
* keys.ts safe file names, valid variable names, one rendered line
|
|
8
|
+
* parse.ts a reader that remembers where every assignment lives
|
|
9
|
+
* edit.ts in-place replacement, plain appends, and the undo
|
|
10
|
+
* discovery.ts which dotenv files a project has, most-precedent first
|
|
11
|
+
* git.ts the one question that can refuse a write
|
|
12
|
+
* write.ts the write itself, and the plan the panel previews
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
envFilePrecedence,
|
|
17
|
+
formatEnvLine,
|
|
18
|
+
isDotenvFileName,
|
|
19
|
+
isSafeEnvFileName,
|
|
20
|
+
isTemplateEnvFile,
|
|
21
|
+
isValidEnvKey,
|
|
22
|
+
TEMPLATE_SUFFIXES
|
|
23
|
+
} from './keys';
|
|
24
|
+
|
|
25
|
+
export type { DotenvAssignment } from './parse';
|
|
26
|
+
export { effectiveAssignment, parseDotenv } from './parse';
|
|
27
|
+
|
|
28
|
+
export type { EnvUpsertOptions, EnvUpsertResult } from './edit';
|
|
29
|
+
export { PREVIOUS_VALUE_PREFIX, removeEnvKeys, upsertEnvKeys } from './edit';
|
|
30
|
+
|
|
31
|
+
export type { DotenvFileEntry } from './discovery';
|
|
32
|
+
export { listDotenvFiles, listEnvFileEntries } from './discovery';
|
|
33
|
+
|
|
34
|
+
export { isTrackedInGit } from './git';
|
|
35
|
+
|
|
36
|
+
export type { EnvWriteInput, EnvWritePlan, EnvWriteResult, EnvWriteStatus } from './write';
|
|
37
|
+
export {
|
|
38
|
+
clearEnvVars,
|
|
39
|
+
clearWorktreeEnv,
|
|
40
|
+
planEnvWrite,
|
|
41
|
+
readEnvFile,
|
|
42
|
+
writeEnvVars,
|
|
43
|
+
writeWorktreeEnv
|
|
44
|
+
} from './write';
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Names and lines — the two things a dotenv writer has to get exactly right.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/** Dotenv files that are templates rather than configuration. */
|
|
6
|
+
export const TEMPLATE_SUFFIXES = ['.example', '.sample', '.template', '.dist'];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Reject anything that is not a plain file name in the project root.
|
|
10
|
+
*
|
|
11
|
+
* The value reaches here from a stored binding or a form field, so it is
|
|
12
|
+
* untrusted input joined onto a path. A name containing a separator or a `..`
|
|
13
|
+
* is refused outright rather than normalised: there is no legitimate dotenv
|
|
14
|
+
* file outside the root a project loads by default, so accepting one would only
|
|
15
|
+
* ever be accepting an accident or an attack.
|
|
16
|
+
*/
|
|
17
|
+
export function isSafeEnvFileName(name: string): boolean {
|
|
18
|
+
const trimmed = name.trim();
|
|
19
|
+
if (!trimmed || trimmed.length > 64) return false;
|
|
20
|
+
if (trimmed.includes('/') || trimmed.includes('\\')) return false;
|
|
21
|
+
if (trimmed === '.' || trimmed === '..') return false;
|
|
22
|
+
return /^\.?[A-Za-z0-9._-]+$/.test(trimmed);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Whether a name is one a shell would accept as a variable. */
|
|
26
|
+
export function isValidEnvKey(key: string): boolean {
|
|
27
|
+
return /^[A-Za-z_][A-Za-z0-9_]*$/.test(key.trim());
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** True for `.env.example` and friends — read for their shape, never written. */
|
|
31
|
+
export function isTemplateEnvFile(name: string): boolean {
|
|
32
|
+
return TEMPLATE_SUFFIXES.some((suffix) => name.endsWith(suffix));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** True for any file a project would load as a dotenv file. */
|
|
36
|
+
export function isDotenvFileName(name: string): boolean {
|
|
37
|
+
return /^\.env(\..+)?$/.test(name);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Render one `KEY=value` line.
|
|
42
|
+
*
|
|
43
|
+
* Quoted only when it has to be. Every dotenv reader in common use strips
|
|
44
|
+
* surrounding double quotes, but an unquoted value is what a human expects to
|
|
45
|
+
* see, and a percent-encoded connection URI needs nothing. The quoting exists
|
|
46
|
+
* for the case that is not: a password carrying a `#` would otherwise turn the
|
|
47
|
+
* rest of the line into a comment.
|
|
48
|
+
*/
|
|
49
|
+
export function formatEnvLine(key: string, value: string): string {
|
|
50
|
+
const needsQuotes = /[\s#'"$`\\]/.test(value) || value === '';
|
|
51
|
+
if (!needsQuotes) return `${key}=${value}`;
|
|
52
|
+
const escaped = value.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n');
|
|
53
|
+
return `${key}="${escaped}"`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* How `.env.local` beats `.env`, as a sortable number.
|
|
58
|
+
*
|
|
59
|
+
* The order is the reason this exists at all: Next.js and Vite read
|
|
60
|
+
* `.env.local` IN PREFERENCE to `.env`, so a project with both has exactly one
|
|
61
|
+
* right answer and picking the other produces the worst outcome available — the
|
|
62
|
+
* variable is written and nothing reads it.
|
|
63
|
+
*/
|
|
64
|
+
export function envFilePrecedence(name: string): number {
|
|
65
|
+
if (name === '.env.development.local' || name === '.env.local') return 0;
|
|
66
|
+
if (name.endsWith('.local')) return 1;
|
|
67
|
+
if (name === '.env') return 2;
|
|
68
|
+
return 3;
|
|
69
|
+
}
|