@open-mercato/starter 0.10.1-develop.7994.1.c9f31972aa → 0.10.1-develop.8022.1.b6753de892

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.
@@ -6,6 +6,8 @@
6
6
  // `packages/starter` directory exists.
7
7
  import { spawnSync } from 'node:child_process'
8
8
  import fs from 'node:fs'
9
+ import path from 'node:path'
10
+ import { parseEnv } from 'node:util'
9
11
  import net from 'node:net'
10
12
  import readline from 'node:readline'
11
13
 
@@ -243,7 +245,7 @@ export async function requestExistingServicesConsent(problem, {
243
245
  for (const entry of problem.ports ?? problem.services ?? []) {
244
246
  const identity = colors.service(entry.containerName || entry.service)
245
247
  const state = diagnosticText(entry.state || 'unknown')
246
- log(` ⚠️ ${identity}${entry.port ? ` on port ${colors.port(entry.port)}` : ''}: ${state}${entry.health ? ` (${diagnosticText(entry.health)})` : ''}`)
248
+ log(` ❗ ${identity}${entry.port ? ` on port ${colors.port(entry.port)}` : ''}: ${state}${entry.health ? ` (${diagnosticText(entry.health)})` : ''}`)
247
249
  log(` ${listenerSummary(entry, colors)}`)
248
250
  if (entry.containerName && (state === 'restarting' || entry.health === 'unhealthy')) log(` Inspect the restart/health failure: ${colors.command(`docker logs ${diagnosticText(entry.containerName)}`)}`)
249
251
  }
@@ -265,7 +267,7 @@ export async function requestExistingServicesConsent(problem, {
265
267
  const answer = await (question ? question(prompt) : new Promise((resolve) => reader.question(prompt, resolve)))
266
268
  if (!/^(y|yes)$/i.test(String(answer).trim())) return false
267
269
  log('')
268
- log(' ⚠️ Continuing with the services configured in .env.')
270
+ log(' ❗ Continuing with the services configured in .env.')
269
271
  if (configuredServices) {
270
272
  log(' Configured services will not be started, stopped or reconfigured.')
271
273
  } else {
@@ -321,6 +323,12 @@ function remediationFor(problem, platform, { wsl = false, databaseUrlFile = '.en
321
323
  return [
322
324
  'Your user may not talk to the Docker engine. Add it to the docker group (sudo usermod -aG docker "$USER"), log out and back in, then re-run.',
323
325
  ]
326
+ case 'unsafe-binding':
327
+ return [
328
+ problem.detail,
329
+ 'Set INFRA_BIND_HOST=127.0.0.1, or set a nonempty REDIS_PASSWORD before exposing infrastructure beyond this machine.',
330
+ 'Keep the Redis password synchronized with REDIS_URL and every *_REDIS_URL used by the app.',
331
+ ]
324
332
  case 'port-conflict':
325
333
  return [
326
334
  ...problem.ports.flatMap((entry) => [
@@ -365,6 +373,7 @@ const PROBLEM_TITLES = {
365
373
  'daemon-unresponsive': 'The Docker engine is not responding.',
366
374
  'daemon-permission': 'No permission to use the Docker engine.',
367
375
  'port-conflict': 'Infrastructure ports are already in use.',
376
+ 'unsafe-binding': 'Refusing an unsafe infrastructure host binding.',
368
377
  unhealthy: 'Infrastructure services did not become healthy.',
369
378
  'compose-failed': 'Starting the infrastructure containers failed.',
370
379
  }
@@ -422,6 +431,7 @@ export async function startInfraServices({
422
431
  services,
423
432
  upArgs = [],
424
433
  cwd,
434
+ env = process.env,
425
435
  beforeUp,
426
436
  detect = detectDockerRuntime,
427
437
  spawnSyncImpl = spawnSync,
@@ -432,6 +442,22 @@ export async function startInfraServices({
432
442
  protectExistingServices = false,
433
443
  log = console.log,
434
444
  }) {
445
+ if (services.some((entry) => entry.service === 'redis')) {
446
+ let settings
447
+ try {
448
+ let dotEnv = {}
449
+ try { dotEnv = parseEnv(fs.readFileSync(path.join(cwd ?? process.cwd(), '.env'), 'utf8')) }
450
+ catch (error) { if (error.code !== 'ENOENT') throw error }
451
+ settings = { ...dotEnv, ...env }
452
+ } catch {
453
+ return { ok: false, problem: { code: 'unsafe-binding', detail: 'Cannot read infrastructure configuration before checking Redis exposure.' } }
454
+ }
455
+ const host = String(settings.INFRA_BIND_HOST ?? '').trim().toLowerCase().replace(/^\[|\]$/g, '') || '127.0.0.1'
456
+ const loopback = host === 'localhost' || host === '::1' || (net.isIP(host) === 4 && host.split('.')[0] === '127')
457
+ if (!loopback && !String(settings.REDIS_PASSWORD ?? '').trim()) {
458
+ return { ok: false, problem: { code: 'unsafe-binding', detail: 'Redis would be reachable beyond loopback without a configured password. No Docker command was run.' } }
459
+ }
460
+ }
435
461
  const runtime = detect()
436
462
  if (!runtime.ok) return { ok: false, problem: { code: runtime.code, detail: runtime.detail } }
437
463
 
@@ -484,3 +510,89 @@ export async function startInfraServices({
484
510
  }
485
511
  return { ok: false, problem: { code: 'compose-failed' } }
486
512
  }
513
+
514
+ const LOOPBACK_DATABASE_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '[::1]'])
515
+
516
+ export function listComposeContainerNames({ composeArgs = (extra) => ['compose', ...extra], cwd, spawnSyncImpl = spawnSync } = {}) {
517
+ const ps = spawnSyncImpl('docker', composeArgs(['ps', '--all', '--format', 'json']), { cwd, encoding: 'utf8', timeout: INFRA_PROBE_TIMEOUT_MS, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] })
518
+ if (ps?.error || (ps?.status ?? 1) !== 0) return null
519
+ return new Set(parseComposePsOutput(ps.stdout).map((entry) => String(entry?.Name ?? '').replace(/^\//, '')).filter(Boolean))
520
+ }
521
+
522
+ function databaseEndpoint(raw) {
523
+ if (!String(raw ?? '').trim()) return null
524
+ try {
525
+ const url = new URL(raw)
526
+ const port = Number.parseInt(url.port || '5432', 10)
527
+ return { host: url.hostname.toLowerCase(), port }
528
+ } catch {
529
+ return null
530
+ }
531
+ }
532
+
533
+ // Default service choice when `yarn setup` gets no service flag:
534
+ // configured services answer + verify (and are not another project's
535
+ // containers) -> reuse (same as --no-infra)
536
+ // nothing answers, Docker usable -> compose (same as --infra)
537
+ // ports held by something unusable -> random (same as --random-ports)
538
+ // Docker unusable, nothing usable -> mode null + problem (stop)
539
+ // Never writes anything; `verifyConnections` is injected because it needs
540
+ // the project's database drivers.
541
+ export async function selectDefaultServiceMode({
542
+ databaseUrl,
543
+ services,
544
+ verifyConnections,
545
+ composeArgs,
546
+ cwd,
547
+ detect = detectDockerRuntime,
548
+ portProbe = probeTcpPort,
549
+ containerProbe = findContainerPortOwners,
550
+ listOwnContainers = () => listComposeContainerNames({ composeArgs, cwd }),
551
+ } = {}) {
552
+ const database = databaseEndpoint(databaseUrl)
553
+ if (database && !LOOPBACK_DATABASE_HOSTS.has(database.host)) {
554
+ return { mode: 'reuse', reason: `DATABASE_URL points at ${database.host}; using the services configured in .env.` }
555
+ }
556
+ const postgresPort = database?.port ?? services.find((entry) => entry.service === 'postgres')?.port ?? 5432
557
+ const candidates = [{ service: 'postgres', port: postgresPort }, ...services.filter((entry) => !(entry.service === 'postgres' && entry.port === postgresPort))]
558
+ const answering = []
559
+ for (const entry of candidates) {
560
+ if (await portProbe(entry.port)) answering.push(entry)
561
+ }
562
+ const runtime = detect()
563
+ const dockerProblem = runtime.ok ? null : { code: runtime.code, detail: runtime.detail }
564
+ const portList = candidates.map((entry) => `${entry.service} :${entry.port}`).join(', ')
565
+ if (answering.length === 0) {
566
+ if (runtime.ok) return { mode: 'compose', reason: `Nothing answers on the configured ports (${portList}); starting this app's Docker Compose services on them.` }
567
+ return { mode: null, problem: dockerProblem, reason: `Nothing answers on the configured service ports (${portList}) and Docker is not available.` }
568
+ }
569
+ let ownOnly = false
570
+ if (runtime.ok) {
571
+ const own = listOwnContainers()
572
+ const owners = containerProbe(answering)
573
+ const foreign = owners.filter((owner) => !own?.has(owner.containerName))
574
+ if (foreign.length > 0) {
575
+ const [first] = foreign
576
+ return { mode: 'random', reason: `Port ${first.port} (${first.service}) belongs to container ${first.containerName || 'unknown'} of another project; starting an isolated stack on free ports so its data is never touched.` }
577
+ }
578
+ ownOnly = Boolean(own) && answering.every((entry) => owners.some((owner) => owner.port === entry.port && own.has(owner.containerName)))
579
+ }
580
+ let failure = null
581
+ if (answering.some((entry) => entry.port === postgresPort)) {
582
+ try {
583
+ await verifyConnections()
584
+ return { mode: 'reuse', reason: runtime.ok
585
+ ? 'The services configured in .env answer and their credentials verified; reusing them.'
586
+ : `Docker is not available (${runtime.detail ?? runtime.code}), but the services configured in .env answer and their credentials verified; using them (same as --no-infra).` }
587
+ } catch (error) {
588
+ if (error?.kind === 'dependency') return { mode: 'reuse', deferredVerification: true, reason: 'The services configured in .env answer; their credentials are verified once dependencies are installed.' }
589
+ failure = error
590
+ }
591
+ }
592
+ const why = failure
593
+ ? `the service on the configured ports did not accept the configured settings (${String(failure.message ?? failure).replace(/^\[setup\]\s*/, '')})`
594
+ : `port ${answering[0].port} (${answering[0].service}) is in use while PostgreSQL does not answer at :${postgresPort}`
595
+ if (!runtime.ok) return { mode: null, problem: dockerProblem, reason: `Configured services are not usable: ${why}.` }
596
+ if (ownOnly) return { mode: 'compose', reason: `This app's Docker Compose services hold the configured ports but are not usable yet; re-running them on the configured ports.` }
597
+ return { mode: 'random', reason: `Configured ports are occupied: ${why}. Starting an isolated stack on free ports instead.` }
598
+ }
@@ -0,0 +1,63 @@
1
+ // Folded setup output hides `mercato init`; this pulls back the part the
2
+ // operator must see — the credentials panel (or the legacy ╔ box) on a first
3
+ // run, or the "already initialized" notice on a re-run. Mirrors
4
+ // extractInitSummaryLines in scripts/dev-orchestration-log-policy.mjs; the
5
+ // starter is published build-free, so it cannot import repo scripts.
6
+
7
+ const ANSI_PATTERN = /\u001b\[[0-9;?]*[A-Za-z]/g
8
+ const BOX_CHARS = /[╔║╠╚]/
9
+
10
+ const stripAnsi = (value) => String(value ?? '').replace(ANSI_PATTERN, '')
11
+
12
+ function sliceVisibleColumns(line, column) {
13
+ let offset = 0
14
+ let visible = 0
15
+ while (visible < column && offset < line.length) {
16
+ const escape = line.slice(offset).match(/^\u001b\[[0-9;]*m/)
17
+ if (escape) offset += escape[0].length
18
+ else {
19
+ offset += 1
20
+ visible += 1
21
+ }
22
+ }
23
+ return line.slice(offset).replace(/^(?:\u001b\[0m)+/, '')
24
+ }
25
+
26
+ function extractPanel(lines) {
27
+ const plain = lines.map(stripAnsi)
28
+ for (let heading = plain.length - 1; heading > 1; heading -= 1) {
29
+ if (!plain[heading].includes(' 🔑 ')) continue
30
+ const start = heading - 2
31
+ const column = plain[start].indexOf('━')
32
+ if (column < 0 || !/^━+\s*$/.test(plain[start].slice(column))) continue
33
+ const rule = plain[start].slice(column).trim()
34
+ const end = plain.findIndex((line, index) => index > heading && line.slice(column).trim() === rule)
35
+ if (end === -1) return []
36
+ return lines.slice(start, end + 1).map((line) => sliceVisibleColumns(line, column))
37
+ }
38
+ return []
39
+ }
40
+
41
+ function extractBox(lines) {
42
+ const plain = lines.map((line) => stripAnsi(line).replace(/\s+$/, ''))
43
+ const start = plain.findLastIndex((line) => line.includes('╔'))
44
+ if (start === -1) return []
45
+ const box = []
46
+ for (const line of plain.slice(start)) {
47
+ const column = line.search(BOX_CHARS)
48
+ if (column === -1) return []
49
+ box.push(line.slice(column))
50
+ if (line.includes('╚')) return box
51
+ }
52
+ return []
53
+ }
54
+
55
+ export function extractInitSummaryLines(lines) {
56
+ if (!Array.isArray(lines)) return []
57
+ const panel = extractPanel(lines)
58
+ if (panel.length > 0) return panel
59
+ const box = extractBox(lines)
60
+ if (box.length > 0) return box
61
+ const kept = lines.map((line) => stripAnsi(line).trim()).find((line) => line.includes('Database already initialized'))
62
+ return kept ? [kept] : []
63
+ }
@@ -0,0 +1,358 @@
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
+ import net from 'node:net'
4
+ import crypto from 'node:crypto'
5
+ import { createRequire } from 'node:module'
6
+ import { createInterface } from 'node:readline/promises'
7
+ import { parseEnv } from 'node:util'
8
+
9
+ const MODE_FLAGS = new Map([['--no-infra', 'reuse'], ['--reuse-services', 'reuse'], ['--ephemeral', 'ephemeral'], ['--random-ports', 'random'], ['--infra', 'compose']])
10
+ export const DEFAULT_SERVICES = [
11
+ { service: 'postgres', envKey: 'POSTGRES_PORT', defaultPort: 5432 },
12
+ { service: 'redis', envKey: 'REDIS_PORT', defaultPort: 6379 },
13
+ { service: 'meilisearch', envKey: 'MEILISEARCH_PORT', defaultPort: 7700 },
14
+ ]
15
+
16
+ export function resolveServiceMode(argv) {
17
+ const modes = [...new Set(argv.filter((arg) => MODE_FLAGS.has(arg)).map((arg) => MODE_FLAGS.get(arg)))]
18
+ if (modes.length > 1) throw new Error('Choose only one service option: --reuse-services, --ephemeral, --random-ports, or --infra.')
19
+ return modes[0] ?? 'reuse'
20
+ }
21
+
22
+ const MENU = [['reuse', 'reuse configured services'], ['ephemeral', 'ephemeral Testcontainers'], ['random', 'Docker Compose on random ports'], ['compose', 'Docker Compose on configured ports']]
23
+
24
+ export class ServiceSelectionError extends Error {
25
+ constructor(selection) {
26
+ super(selection.reason)
27
+ this.name = 'ServiceSelectionError'
28
+ this.problem = selection.problem
29
+ }
30
+ }
31
+
32
+ // Explicit flags keep their exact meaning. Without one, `selectDefault`
33
+ // (selectDefaultServiceMode from the infra preflight) picks the mode; a TTY
34
+ // may still choose, with that pick as the default. Non-interactive runs never
35
+ // prompt.
36
+ export async function chooseServiceMode({ argv = [], input = process.stdin, output = process.stdout, selectDefault, log = console.log } = {}) {
37
+ const mode = resolveServiceMode(argv)
38
+ if (argv.some((arg) => MODE_FLAGS.has(arg))) return mode
39
+ const selection = selectDefault ? await selectDefault() : { mode: 'reuse' }
40
+ if (!selection.mode) throw new ServiceSelectionError(selection)
41
+ if (selection.reason) log(`\n🔎 ${selection.reason}\n`)
42
+ if (argv.includes('--non-interactive') || !input.isTTY || !output.isTTY || process.env.CI) return selection.mode
43
+ const prompt = createInterface({ input, output })
44
+ try {
45
+ const choices = MENU.map(([value, label], index) => ` [${index + 1}] ${label}${value === selection.mode ? ' (default)' : ''}`).join('\n')
46
+ const answer = (await prompt.question(`\n🧩 Choose your services\n\n${choices}\n\n Enter a number [1–4] or press Enter for the default: `)).trim()
47
+ if (!answer) return selection.mode
48
+ const picked = MENU[Number(answer) - 1]
49
+ if (!/^[1-4]$/.test(answer) || !picked) throw new Error('Invalid service choice. Re-run setup and choose 1, 2, 3, or 4.')
50
+ return picked[0]
51
+ } finally {
52
+ prompt.close()
53
+ }
54
+ }
55
+
56
+ export async function availablePort() {
57
+ return new Promise((resolve, reject) => {
58
+ const server = net.createServer()
59
+ server.once('error', reject)
60
+ server.listen(0, '127.0.0.1', () => {
61
+ const address = server.address()
62
+ const port = address.port
63
+ server.close((error) => error ? reject(error) : resolve(port))
64
+ })
65
+ })
66
+ }
67
+
68
+ function envText(file) {
69
+ try { return fs.readFileSync(file, 'utf8') } catch (error) {
70
+ if (error.code === 'ENOENT') return ''
71
+ throw error
72
+ }
73
+ }
74
+
75
+ function assignmentPattern(key) {
76
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) throw new Error('Invalid service configuration key')
77
+ return new RegExp(String.raw`^(?:export[ \t]+)?${key}[ \t]*=[ \t]*(?:"[^"]*"|'[^']*'|[^\r\n]*)(?:[^\r\n]*)(?:\r?\n|$)`, 'gm')
78
+ }
79
+
80
+ function replaceValues(text, values) {
81
+ let result = text
82
+ for (const [key, value] of Object.entries(values)) {
83
+ const pattern = assignmentPattern(key)
84
+ if (value === undefined) result = result.replace(pattern, '')
85
+ else {
86
+ const raw = String(value)
87
+ if (/[\r\n']/.test(raw)) throw new Error(`Cannot safely serialize ${key}; remove line breaks and single quotes before selecting isolated services.`)
88
+ const serialized = /^[A-Za-z0-9_./:@%?=&,+-]*$/.test(raw) ? raw : `'${raw}'`
89
+ const line = `${key}=${serialized}\n`
90
+ result = pattern.test(result) ? result.replace(pattern, () => line) : `${result}${result && !result.endsWith('\n') ? '\n' : ''}${line}`
91
+ }
92
+ }
93
+ return result
94
+ }
95
+
96
+ export const SETUP_SERVICE_IMAGES = {
97
+ postgres: 'pgvector/pgvector:pg17-trixie@sha256:7a7e9f22015b67edb4bef5c59daeebcd7e74bfa570df6ce60ae01237c8648a84',
98
+ redis: 'redis:7-alpine@sha256:892022f9f18c26c92c30e15194d132f90044af87b94a491a75429bbc3982eab1',
99
+ meilisearch: 'getmeili/meilisearch:v1.11@sha256:6f8f1fc29d1ff67b4d8d503bb4e0a69b55c8d42600decafde951344218193a27',
100
+ ryuk: 'testcontainers/ryuk:0.14.0@sha256:7c1a8a9a47c780ed0f983770a662f80deb115d95cce3e2daa3d12115b8cd28f0',
101
+ }
102
+
103
+ function localCredentials(mode, projectDir, stored) {
104
+ const identity = `mercato-local-v2-${crypto.createHash('sha256').update(path.resolve(projectDir)).digest('hex').slice(0, 10)}`
105
+ const keys = ['OM_SETUP_LOCAL_POSTGRES_PASSWORD', 'OM_SETUP_LOCAL_REDIS_PASSWORD', 'OM_SETUP_LOCAL_MEILISEARCH_KEY']
106
+ if (mode === 'random' && stored.OM_SETUP_LOCAL_STACK === identity) {
107
+ if (!keys.every((key) => /^[a-f0-9]{48}$/.test(stored[key] ?? ''))) throw new Error('Owned local service credentials are incomplete. Restore the OM_SETUP_LOCAL_* values before reusing this stack; no services were changed.')
108
+ let database = 'open_mercato_local'
109
+ try {
110
+ const previous = new URL(stored.DATABASE_URL)
111
+ const selected = decodeURIComponent(previous.pathname.slice(1))
112
+ if (previous.hostname === '127.0.0.1' && previous.username === 'mercato' && previous.password === stored[keys[0]] && /^[A-Za-z_][A-Za-z0-9_-]{0,62}$/.test(selected)) database = selected
113
+ } catch {}
114
+ return { identity, postgres: stored[keys[0]], redis: stored[keys[1]], meilisearch: stored[keys[2]], database }
115
+ }
116
+ return { identity, postgres: crypto.randomBytes(24).toString('hex'), redis: crypto.randomBytes(24).toString('hex'), meilisearch: crypto.randomBytes(24).toString('hex') }
117
+ }
118
+
119
+ function localUrl(raw, fallback, host, port) {
120
+ const url = new URL(raw || fallback)
121
+ url.hostname = host.includes(':') && !host.startsWith('[') ? `[${host}]` : host
122
+ url.port = String(port)
123
+ return url.toString()
124
+ }
125
+
126
+ async function importTestcontainers(projectDir) {
127
+ const candidates = [path.join(projectDir, 'package.json'), path.join(projectDir, 'node_modules', '@open-mercato', 'cli', 'package.json'), path.join(projectDir, '..', '..', 'packages', 'cli', 'package.json')]
128
+ for (const candidate of candidates) {
129
+ try {
130
+ const require = createRequire(candidate)
131
+ return require('testcontainers')
132
+ } catch (error) {
133
+ if (error.code !== 'MODULE_NOT_FOUND') throw error
134
+ }
135
+ }
136
+ throw new Error('Testcontainers is unavailable. Run yarn install, then retry yarn setup --ephemeral.')
137
+ }
138
+
139
+ export async function configureServiceMode({ mode, projectDir, env = process.env, envFiles = [], services = DEFAULT_SERVICES, loadTestcontainers = () => importTestcontainers(projectDir), getPort = availablePort, log = console.log } = {}) {
140
+ if (!['reuse', 'compose', 'random', 'ephemeral'].includes(mode)) throw new Error('Unknown service mode.')
141
+ if (mode === 'reuse' || mode === 'compose') return { ok: true, mode, env, noInfra: mode === 'reuse' }
142
+ const files = [...new Set([path.join(projectDir, '.env'), ...envFiles])]
143
+ const before = new Map(files.map((file) => [file, parseEnv(envText(file))]))
144
+ const settings = { ...Object.assign({}, ...files.slice(1).map((file) => before.get(file))), ...before.get(files[0]), ...env }
145
+ const changes = {}
146
+ const containers = []
147
+ const written = new Map()
148
+ const originalText = new Map(files.map((file) => [file, fs.existsSync(file) ? envText(file) : undefined]))
149
+ let committed = false
150
+ let interrupted = false
151
+ const onSignal = () => { interrupted = true }
152
+ if (mode === 'ephemeral') {
153
+ process.on('SIGINT', onSignal)
154
+ process.on('SIGTERM', onSignal)
155
+ }
156
+ let restored = false
157
+ const restoreEnv = {}
158
+ const previousRyuk = process.env.RYUK_CONTAINER_IMAGE
159
+ let containersStopped = false
160
+ const cleanup = async () => {
161
+ if (restored) return
162
+ const failures = []
163
+ if (mode === 'ephemeral' || !committed) {
164
+ for (const [file, content] of written) {
165
+ try {
166
+ if (envText(file) === content) {
167
+ if (originalText.get(file) === undefined) fs.unlinkSync(file)
168
+ else fs.writeFileSync(file, originalText.get(file), { mode: 0o600 })
169
+ } else {
170
+ const current = parseEnv(envText(file))
171
+ const keys = Object.keys(changes).filter((key) => current[key] === String(changes[key]))
172
+ let persistedDatabaseAssignment
173
+ if (current.DATABASE_URL && changes.DATABASE_URL && current.DATABASE_URL !== changes.DATABASE_URL) {
174
+ try {
175
+ const active = new URL(current.DATABASE_URL)
176
+ const owned = new URL(changes.DATABASE_URL)
177
+ if (active.protocol === owned.protocol && active.hostname === owned.hostname && active.port === owned.port) {
178
+ keys.push('DATABASE_URL')
179
+ const previous = before.get(file).DATABASE_URL
180
+ persistedDatabaseAssignment = ''
181
+ if (previous) {
182
+ const restoredUrl = new URL(previous)
183
+ restoredUrl.pathname = active.pathname
184
+ const raw = (originalText.get(file) ?? '').match(assignmentPattern('DATABASE_URL'))?.join('') ?? ''
185
+ persistedDatabaseAssignment = raw.includes(previous) ? raw.replace(previous, () => restoredUrl.toString()) : raw
186
+ }
187
+ }
188
+ } catch {}
189
+ }
190
+ let next = envText(file)
191
+ for (const key of keys) {
192
+ next = next.replace(assignmentPattern(key), '')
193
+ const raw = key === 'DATABASE_URL' && persistedDatabaseAssignment !== undefined
194
+ ? persistedDatabaseAssignment
195
+ : (originalText.get(file) ?? '').match(assignmentPattern(key))?.join('') ?? ''
196
+ if (raw) next += `${next && !next.endsWith('\n') ? '\n' : ''}${raw}${raw.endsWith('\n') ? '' : '\n'}`
197
+ }
198
+ fs.writeFileSync(file, next, { mode: 0o600 })
199
+ }
200
+ written.delete(file)
201
+ } catch (error) { failures.push(error) }
202
+ }
203
+ for (const [key, value] of Object.entries(restoreEnv)) {
204
+ if (env[key] !== String(changes[key])) continue
205
+ if (value === undefined) delete env[key]
206
+ else env[key] = value
207
+ }
208
+ }
209
+ if (!containersStopped) {
210
+ const results = await Promise.allSettled(containers.map((container) => container.stop()))
211
+ containersStopped = results.every((result) => result.status === 'fulfilled')
212
+ if (!containersStopped) failures.push(new Error('Some ephemeral services could not be stopped; inspect Docker before retrying cleanup.'))
213
+ }
214
+ if (mode === 'ephemeral') {
215
+ if (previousRyuk === undefined) delete process.env.RYUK_CONTAINER_IMAGE
216
+ else process.env.RYUK_CONTAINER_IMAGE = previousRyuk
217
+ }
218
+ restored = failures.length === 0
219
+ if (failures.length) throw new AggregateError(failures, 'Could not fully restore service configuration or stop ephemeral services; retry cleanup after resolving the filesystem or Docker error.')
220
+ }
221
+
222
+ try {
223
+ const credentials = localCredentials(mode, projectDir, before.get(files[0]))
224
+ const dbUser = 'mercato'
225
+ const dbPassword = credentials.postgres
226
+ const dbName = credentials.database ?? 'open_mercato_local'
227
+ const database = new URL(`postgres://${dbUser}:${dbPassword}@127.0.0.1:5432/${dbName}`)
228
+ changes.POSTGRES_USER = dbUser
229
+ changes.POSTGRES_PASSWORD = dbPassword
230
+ changes.POSTGRES_DB = dbName
231
+ changes.REDIS_PASSWORD = credentials.redis
232
+ changes.MEILISEARCH_API_KEY = credentials.meilisearch
233
+ changes.MEILISEARCH_MASTER_KEY = credentials.meilisearch
234
+ changes.INFRA_BIND_HOST = '127.0.0.1'
235
+ if (mode === 'random') {
236
+ changes.OM_SETUP_LOCAL_STACK = credentials.identity
237
+ changes.OM_SETUP_LOCAL_POSTGRES_PASSWORD = credentials.postgres
238
+ changes.OM_SETUP_LOCAL_REDIS_PASSWORD = credentials.redis
239
+ changes.OM_SETUP_LOCAL_MEILISEARCH_KEY = credentials.meilisearch
240
+ }
241
+ const meiliKey = credentials.meilisearch
242
+ if (mode === 'ephemeral') {
243
+ if (previousRyuk && !/@sha256:[a-f0-9]{64}$/.test(previousRyuk)) throw new Error('RYUK_CONTAINER_IMAGE must use an immutable sha256 digest for ephemeral setup.')
244
+ process.env.RYUK_CONTAINER_IMAGE = previousRyuk || SETUP_SERVICE_IMAGES.ryuk
245
+ }
246
+ const { GenericContainer, Wait } = mode === 'ephemeral' ? await loadTestcontainers() : {}
247
+ for (const definition of services) {
248
+ const { service, envKey, defaultPort } = definition
249
+ let host = '127.0.0.1'
250
+ let port
251
+ if (mode === 'random') {
252
+ do { port = await getPort() } while (Object.values(changes).includes(String(port)))
253
+ } else {
254
+ const definitions = {
255
+ postgres: [SETUP_SERVICE_IMAGES.postgres, { POSTGRES_USER: dbUser, POSTGRES_PASSWORD: dbPassword, POSTGRES_DB: dbName }, ['pg_isready', '-U', dbUser, '-d', dbName]],
256
+ redis: [SETUP_SERVICE_IMAGES.redis, { REDISCLI_AUTH: credentials.redis }, ['redis-cli', 'ping']],
257
+ meilisearch: [SETUP_SERVICE_IMAGES.meilisearch, { MEILI_ENV: 'development', MEILI_MASTER_KEY: meiliKey, MEILI_NO_ANALYTICS: 'true' }, ['curl', '-fsS', 'http://127.0.0.1:7700/health']],
258
+ }
259
+ if (!definitions[service]) continue
260
+ const [image, environment, healthCommand] = definitions[service]
261
+ class LoopbackContainer extends GenericContainer {
262
+ async beforeContainerCreated() {
263
+ for (const bindings of Object.values(this.hostConfig?.PortBindings ?? {})) {
264
+ for (const binding of bindings) binding.HostIp = '127.0.0.1'
265
+ }
266
+ }
267
+ }
268
+ const builder = new LoopbackContainer(image).withEnvironment(environment).withExposedPorts(defaultPort)
269
+ if (service === 'redis') builder.withCommand(['redis-server', '--requirepass', credentials.redis])
270
+ const container = await builder.withHealthCheck({ test: ['CMD', ...healthCommand], interval: 1000, timeout: 3000, retries: 60, startPeriod: 1000 }).withWaitStrategy(Wait.forHealthCheck()).start()
271
+ containers.push(container)
272
+ if (interrupted) throw new Error('Ephemeral service startup interrupted; started services were cleaned up.')
273
+ host = container.getHost()
274
+ port = container.getMappedPort(defaultPort)
275
+ }
276
+ changes[envKey] = String(port)
277
+ if (service === 'postgres') changes.DATABASE_URL = localUrl(database.toString(), '', host, port)
278
+ if (service === 'redis') {
279
+ const redisKeys = new Set(['REDIS_URL', 'CACHE_REDIS_URL', 'QUEUE_REDIS_URL', 'EVENTS_REDIS_URL', 'DOCUMENTS_COLLAB_REDIS_URL', ...Object.keys(settings).filter((key) => key.endsWith('_REDIS_URL'))])
280
+ for (const key of redisKeys) {
281
+ const endpoint = new URL(localUrl(settings[key], 'redis://localhost:6379', host, port))
282
+ endpoint.protocol = 'redis:'
283
+ endpoint.search = ''
284
+ endpoint.hash = ''
285
+ endpoint.username = ''
286
+ endpoint.password = credentials.redis
287
+ changes[key] = endpoint.toString()
288
+ }
289
+ }
290
+ if (service === 'meilisearch') {
291
+ const endpoint = new URL(localUrl(settings.MEILISEARCH_HOST, 'http://localhost:7700', host, port))
292
+ endpoint.protocol = 'http:'
293
+ endpoint.pathname = '/'
294
+ endpoint.search = ''
295
+ endpoint.hash = ''
296
+ endpoint.username = ''
297
+ endpoint.password = ''
298
+ changes.MEILISEARCH_HOST = endpoint.toString()
299
+ }
300
+ }
301
+ if (mode === 'random') {
302
+ changes.MERCATO_STACK = credentials.identity
303
+ changes.COMPOSE_PROJECT_NAME = credentials.identity
304
+ }
305
+ for (const file of files) {
306
+ fs.mkdirSync(path.dirname(file), { recursive: true })
307
+ const content = replaceValues(envText(file), changes)
308
+ const temporary = `${file}.setup-${crypto.randomUUID()}.tmp`
309
+ try {
310
+ fs.writeFileSync(temporary, content, { mode: 0o600, flag: 'wx' })
311
+ fs.renameSync(temporary, file)
312
+ written.set(file, content)
313
+ } finally {
314
+ fs.rmSync(temporary, { force: true })
315
+ }
316
+ }
317
+ for (const [key, value] of Object.entries(changes)) {
318
+ restoreEnv[key] = env[key]
319
+ env[key] = String(value)
320
+ }
321
+ committed = true
322
+ log(`\n✅ ${mode === 'ephemeral' ? 'Ephemeral Testcontainers services are ready; configuration will be restored when setup/dev exits' : 'Random service ports and matching URLs saved to .env'}.`)
323
+ return { ok: true, mode, env, noInfra: mode === 'ephemeral', cleanup: mode === 'ephemeral' ? cleanup : undefined }
324
+ } catch (error) {
325
+ await cleanup()
326
+ throw error
327
+ } finally {
328
+ process.off('SIGINT', onSignal)
329
+ process.off('SIGTERM', onSignal)
330
+ }
331
+ }
332
+
333
+ export function printManualSetup({ log = console.error, monorepo = false } = {}) {
334
+ log('')
335
+ log('🔨 Setup did not complete. Manual setup:')
336
+ log('')
337
+ log(' 1. Configure PostgreSQL, Redis and Meilisearch in your .env file:')
338
+ log(monorepo ? ' apps/mercato/.env' : ' .env')
339
+ log('')
340
+ log(' 2. Run these commands in order:')
341
+ log('')
342
+ log(' yarn install')
343
+ if (monorepo) log(' yarn build:packages')
344
+ log(' yarn generate')
345
+ if (monorepo) log(' yarn build:packages')
346
+ log(' yarn db:migrate')
347
+ log(monorepo ? ' yarn initialize -- --skip-if-initialized' : ' yarn initialize --skip-if-initialized')
348
+ log(' yarn dev --no-infra')
349
+ log('')
350
+ log('💡 Or retry setup with an isolated or Docker Compose stack:')
351
+ log('')
352
+ log(' yarn setup --random-ports Docker Compose on free ports')
353
+ log(' yarn setup --ephemeral Temporary Testcontainers services')
354
+ log(' yarn setup --infra Docker Compose on configured ports')
355
+ log('')
356
+ log('🔒 Run database commands only against the intended database; reuse preserves your configured URLs.')
357
+ log('')
358
+ }
@@ -54,7 +54,7 @@ export function createSetupLogs({
54
54
  if (fileFailed) return
55
55
  fileFailed = true
56
56
  activity?.pause()
57
- write(' ⚠️ Could not save setup logs; terminal output is still available.\n')
57
+ write(' ❗ Could not save setup logs; terminal output is still available.\n')
58
58
  if (active && !visible) activity?.resume()
59
59
  }
60
60