@skitterbyte/skitterspec 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +31 -4
- package/assets/claude-md-section.md +18 -14
- package/assets/core/env.config.json.example +5 -0
- package/assets/core/env.config.md +29 -2
- package/assets/rules/spec-planning.md +22 -17
- package/assets/skills/spec/SKILL.md +13 -10
- package/assets/skills/spec-cancel/SKILL.md +11 -4
- package/assets/skills/spec-complete/SKILL.md +16 -8
- package/assets/skills/spec-connect/SKILL.md +53 -0
- package/assets/skills/spec-go/SKILL.md +28 -10
- package/assets/skills/spec-review/SKILL.md +2 -2
- package/package.json +4 -6
- package/src/cli.js +191 -5
- package/src/env/config.js +44 -0
- package/src/env/dev.js +63 -0
- package/src/env/proxy.js +159 -0
- package/src/env/resolve.js +16 -7
- package/src/env/supervise.js +150 -0
- package/src/init.js +2 -2
- package/assets/skills/spec-env/SKILL.md +0 -63
- package/assets/skills/spec-env-down/SKILL.md +0 -64
- package/assets/skills/spec-ready/SKILL.md +0 -50
package/src/cli.js
CHANGED
|
@@ -17,11 +17,14 @@ const {
|
|
|
17
17
|
freeSlot,
|
|
18
18
|
portOffset,
|
|
19
19
|
} = require('./env/registry.js')
|
|
20
|
-
const { resolveSpec, resolveBaseBranch } = require('./env/resolve.js')
|
|
20
|
+
const { resolveSpec, resolveBaseBranch, repoInfo, expandTokens, splitPrefix } = require('./env/resolve.js')
|
|
21
21
|
const { ensureWorktreeDirTrusted } = require('./env/trust.js')
|
|
22
22
|
const { planUp } = require('./env/provision.js')
|
|
23
23
|
const { planDown } = require('./env/teardown.js')
|
|
24
24
|
const { planIntegrate } = require('./env/integrate.js')
|
|
25
|
+
const { planDev } = require('./env/dev.js')
|
|
26
|
+
const { startProcess, stopProcess, waitHealthy } = require('./env/supervise.js')
|
|
27
|
+
const { renderRoutes, portsInUse, waitListening } = require('./env/proxy.js')
|
|
25
28
|
|
|
26
29
|
const pkg = require('../package.json')
|
|
27
30
|
|
|
@@ -36,6 +39,9 @@ Usage:
|
|
|
36
39
|
specs/.core/env.config.json). Subcommands:
|
|
37
40
|
up <spec> plan a worktree + Docker stack + opener
|
|
38
41
|
down <spec> tear down (guards; --keep-volumes, --force)
|
|
42
|
+
dev up <spec> start host dev servers on the spec's ports
|
|
43
|
+
dev down <spec> stop the spec's host dev servers
|
|
44
|
+
connect <spec> expose a spec on the canonical ports (main = off)
|
|
39
45
|
integrate <spec> plan rebase + fast-forward onto the base branch
|
|
40
46
|
status list provisioned specs + port blocks
|
|
41
47
|
resolve <spec> print resolved slug/type/branch/paths
|
|
@@ -332,7 +338,19 @@ function specEnvIntegrate(dir, config, specArg) {
|
|
|
332
338
|
const commonDir = gitReader(dir)(['rev-parse', '--git-common-dir'])
|
|
333
339
|
const mainRepoPath = commonDir ? path.dirname(path.resolve(dir, commonDir)) : dir
|
|
334
340
|
|
|
335
|
-
|
|
341
|
+
// A spec authored entirely on its branch may not exist in the primary
|
|
342
|
+
// checkout's specs/** (it was never committed to base) — but its worktree
|
|
343
|
+
// does, and the worktree path is derivable from config without the folder.
|
|
344
|
+
// Offer it as a fallback search location so integrate can still find the spec.
|
|
345
|
+
const { slug } = splitPrefix(path.basename(specArg))
|
|
346
|
+
const { repo, repoSlug } = repoInfo(mainRepoPath)
|
|
347
|
+
const wtTokens = { repo, repoSlug, slug }
|
|
348
|
+
const worktreeGuess = path.resolve(
|
|
349
|
+
mainRepoPath,
|
|
350
|
+
expandTokens(config.worktree.root, wtTokens),
|
|
351
|
+
expandTokens(config.worktree.folderPattern, wtTokens),
|
|
352
|
+
)
|
|
353
|
+
const spec = resolveSpec(specArg, mainRepoPath, config, { searchDirs: [worktreeGuess] })
|
|
336
354
|
|
|
337
355
|
if (!fs.existsSync(spec.worktreePath)) {
|
|
338
356
|
process.stdout.write(
|
|
@@ -389,9 +407,171 @@ function specEnvResolve(dir, config, specArg) {
|
|
|
389
407
|
)
|
|
390
408
|
}
|
|
391
409
|
|
|
410
|
+
// Start/stop a spec's host dev servers on its reserved port block. Host dev
|
|
411
|
+
// servers (e.g. `pnpm dev`) need a block even on a worktree-only spec, so `up`
|
|
412
|
+
// allocates a slot if the spec has none (idempotent). The planner is pure
|
|
413
|
+
// (dev.js); the spawning/killing lives in supervise.js.
|
|
414
|
+
async function specEnvDev(dir, config, positional) {
|
|
415
|
+
const action = positional[0]
|
|
416
|
+
const specArg = positional[1]
|
|
417
|
+
if ((action !== 'up' && action !== 'down') || !specArg) {
|
|
418
|
+
process.stdout.write('Usage: skitterspec spec-env dev <up|down> <spec>\n')
|
|
419
|
+
return
|
|
420
|
+
}
|
|
421
|
+
const spec = resolveSpec(specArg, dir, config)
|
|
422
|
+
if (!config.dev.length) {
|
|
423
|
+
process.stdout.write(
|
|
424
|
+
'spec-env dev: no dev processes configured — set "dev": [...] in env.config.json.\n',
|
|
425
|
+
)
|
|
426
|
+
return
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const registry = readRegistry(dir, config)
|
|
430
|
+
let slot
|
|
431
|
+
if (action === 'up') {
|
|
432
|
+
// Ensure a slot (idempotent) so the port block is reserved even worktree-only.
|
|
433
|
+
const alloc = allocateSlot(registry, spec.folder)
|
|
434
|
+
slot = alloc.slot
|
|
435
|
+
writeRegistry(dir, config, alloc.registry)
|
|
436
|
+
} else {
|
|
437
|
+
// Teardown only needs the pid-file paths (keyed by folder, not slot), so the
|
|
438
|
+
// slot value is immaterial — use the existing one, or 0 as a placeholder.
|
|
439
|
+
slot = Object.prototype.hasOwnProperty.call(registry.slots, spec.folder)
|
|
440
|
+
? registry.slots[spec.folder]
|
|
441
|
+
: 0
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const plan = planDev(spec, slot, config)
|
|
445
|
+
|
|
446
|
+
if (action === 'up') {
|
|
447
|
+
const out = [`spec-env dev up: ${spec.folder} slot ${slot} (ports from ${plan.portOffset})`]
|
|
448
|
+
for (const proc of plan.procs) {
|
|
449
|
+
const res = startProcess(proc, { cwd: spec.worktreePath, rootDir: dir })
|
|
450
|
+
let health = ''
|
|
451
|
+
if (proc.health) {
|
|
452
|
+
health = (await waitHealthy(proc.health)) ? ' health: ok' : ' health: TIMEOUT'
|
|
453
|
+
}
|
|
454
|
+
out.push(
|
|
455
|
+
` ${proc.name}: port ${proc.port} pid ${res.pid} ` +
|
|
456
|
+
`${res.started ? 'started' : 'already running'}${health}`,
|
|
457
|
+
)
|
|
458
|
+
}
|
|
459
|
+
out.push('')
|
|
460
|
+
out.push(` logs: ${stateDirLabel(config)}/logs/`)
|
|
461
|
+
process.stdout.write(out.join('\n') + '\n')
|
|
462
|
+
} else {
|
|
463
|
+
const out = [`spec-env dev down: ${spec.folder}`]
|
|
464
|
+
for (const proc of plan.procs) {
|
|
465
|
+
const res = await stopProcess(proc, { rootDir: dir })
|
|
466
|
+
out.push(` ${proc.name}: ${res.stopped ? `stopped (pid ${res.pid})` : 'not running'}`)
|
|
467
|
+
}
|
|
468
|
+
process.stdout.write(out.join('\n') + '\n')
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// The `.spec-env`-style state dir label for user-facing messages.
|
|
473
|
+
function stateDirLabel(config) {
|
|
474
|
+
return path.posix.dirname(config.registry) || '.spec-env'
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// The supervised proxy process descriptor (paths relative to the checkout root).
|
|
478
|
+
function proxyProcFor(config, routesFileAbs) {
|
|
479
|
+
const sdir = stateDirLabel(config)
|
|
480
|
+
return {
|
|
481
|
+
name: 'proxy',
|
|
482
|
+
command: `node ${path.join(__dirname, 'env', 'proxy.js')} ${routesFileAbs}`,
|
|
483
|
+
env: {},
|
|
484
|
+
logFile: `${sdir}/logs/proxy.log`,
|
|
485
|
+
pidFile: `${sdir}/pids/proxy.pid`,
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// Connect the canonical origin to ONE spec (exclusive model): (re)start the
|
|
490
|
+
// bundled proxy pointing at that spec's warm dev servers. `connect main` stops
|
|
491
|
+
// the proxy so the primary checkout owns the canonical ports again.
|
|
492
|
+
async function specEnvConnect(dir, config, specArg) {
|
|
493
|
+
const sdir = stateDirLabel(config)
|
|
494
|
+
const abs = (rel) => path.resolve(dir, rel)
|
|
495
|
+
const routesFile = `${sdir}/proxy.json`
|
|
496
|
+
const connectedFile = `${sdir}/connected`
|
|
497
|
+
const proxyProc = proxyProcFor(config, abs(routesFile))
|
|
498
|
+
const target = specArg || 'main'
|
|
499
|
+
|
|
500
|
+
if (target === 'main') {
|
|
501
|
+
const res = await stopProcess(proxyProc, { rootDir: dir })
|
|
502
|
+
for (const f of [connectedFile, routesFile]) {
|
|
503
|
+
try {
|
|
504
|
+
fs.unlinkSync(abs(f))
|
|
505
|
+
} catch {
|
|
506
|
+
/* not connected */
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
process.stdout.write(
|
|
510
|
+
res.stopped
|
|
511
|
+
? 'spec-connect: disconnected — the primary checkout owns the canonical ports again.\n'
|
|
512
|
+
: 'spec-connect: nothing was connected — the primary checkout already owns the ports.\n',
|
|
513
|
+
)
|
|
514
|
+
return
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
const spec = resolveSpec(target, dir, config)
|
|
518
|
+
const registry = readRegistry(dir, config)
|
|
519
|
+
if (!Object.prototype.hasOwnProperty.call(registry.slots, spec.folder)) {
|
|
520
|
+
process.stdout.write(
|
|
521
|
+
`spec-connect: ${spec.folder} has no reserved ports yet — ` +
|
|
522
|
+
`run \`skitterspec spec-env dev up ${spec.folder}\` first.\n`,
|
|
523
|
+
)
|
|
524
|
+
return
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const plan = planDev(spec, registry.slots[spec.folder], config)
|
|
528
|
+
const routes = renderRoutes(plan.procs)
|
|
529
|
+
if (!routes.length) {
|
|
530
|
+
process.stdout.write(
|
|
531
|
+
'spec-connect: no dev process declares a frontPort — nothing to expose.\n',
|
|
532
|
+
)
|
|
533
|
+
return
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// Stop any proxy we already run (a previous connect), freeing the canonical
|
|
537
|
+
// ports, then refuse if the primary checkout still holds one of them.
|
|
538
|
+
await stopProcess(proxyProc, { rootDir: dir })
|
|
539
|
+
const busy = await portsInUse(routes.map((r) => r.frontPort), config.proxy.host)
|
|
540
|
+
if (busy.length) {
|
|
541
|
+
process.stdout.write(
|
|
542
|
+
`spec-connect: canonical port(s) ${busy.join(', ')} are in use (your main dev server?).\n` +
|
|
543
|
+
'Stop main on those ports, then re-run spec-connect.\n',
|
|
544
|
+
)
|
|
545
|
+
return
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
fs.mkdirSync(abs(sdir), { recursive: true })
|
|
549
|
+
fs.writeFileSync(abs(routesFile), JSON.stringify(routes, null, 2) + '\n')
|
|
550
|
+
const res = startProcess(proxyProc, { cwd: dir, rootDir: dir })
|
|
551
|
+
fs.writeFileSync(abs(connectedFile), spec.folder + '\n')
|
|
552
|
+
|
|
553
|
+
const ready = await waitListening(
|
|
554
|
+
routes.map((r) => r.frontPort),
|
|
555
|
+
{ host: config.proxy.host },
|
|
556
|
+
)
|
|
557
|
+
|
|
558
|
+
const out = [
|
|
559
|
+
`spec-connect: ${spec.folder} → canonical ports (proxy pid ${res.pid})` +
|
|
560
|
+
(ready ? '' : ' [WARNING: proxy did not come up — see .spec-env/logs/proxy.log]'),
|
|
561
|
+
]
|
|
562
|
+
for (const r of routes) {
|
|
563
|
+
out.push(
|
|
564
|
+
` http://${config.proxy.host}:${r.frontPort} → ${r.name} (127.0.0.1:${r.targetPort})`,
|
|
565
|
+
)
|
|
566
|
+
}
|
|
567
|
+
out.push('')
|
|
568
|
+
out.push(' Disconnect with: skitterspec spec-env connect main')
|
|
569
|
+
process.stdout.write(out.join('\n') + '\n')
|
|
570
|
+
}
|
|
571
|
+
|
|
392
572
|
// Dispatch `skitterspec spec-env <sub> [args] [--dir path]`. No-ops with a clear
|
|
393
573
|
// message when the feature isn't enabled (no specs/.core/env.config.json).
|
|
394
|
-
function specEnv(rest) {
|
|
574
|
+
async function specEnv(rest) {
|
|
395
575
|
const [sub, ...args] = rest
|
|
396
576
|
let dir = process.cwd()
|
|
397
577
|
const positional = []
|
|
@@ -420,6 +600,12 @@ function specEnv(rest) {
|
|
|
420
600
|
case 'down':
|
|
421
601
|
specEnvDown(dir, config, positional[0], flags)
|
|
422
602
|
break
|
|
603
|
+
case 'dev':
|
|
604
|
+
await specEnvDev(dir, config, positional)
|
|
605
|
+
break
|
|
606
|
+
case 'connect':
|
|
607
|
+
await specEnvConnect(dir, config, positional[0])
|
|
608
|
+
break
|
|
423
609
|
case 'integrate':
|
|
424
610
|
specEnvIntegrate(dir, config, positional[0])
|
|
425
611
|
break
|
|
@@ -431,7 +617,7 @@ function specEnv(rest) {
|
|
|
431
617
|
break
|
|
432
618
|
default:
|
|
433
619
|
process.stdout.write(
|
|
434
|
-
'Usage: skitterspec spec-env <up|down|integrate|status|resolve> [spec] [--keep-volumes] [--force]\n',
|
|
620
|
+
'Usage: skitterspec spec-env <up|down|dev|connect|integrate|status|resolve> [spec] [--keep-volumes] [--force]\n',
|
|
435
621
|
)
|
|
436
622
|
}
|
|
437
623
|
}
|
|
@@ -449,7 +635,7 @@ async function run(argv) {
|
|
|
449
635
|
const [cmd, ...rest] = argv
|
|
450
636
|
|
|
451
637
|
if (cmd === 'spec-env') {
|
|
452
|
-
specEnv(rest)
|
|
638
|
+
await specEnv(rest)
|
|
453
639
|
return
|
|
454
640
|
}
|
|
455
641
|
|
package/src/env/config.js
CHANGED
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
* worktree: { root, folderPattern },
|
|
17
17
|
* docker: { enabled, composeFile, projectNamePattern, portBase,
|
|
18
18
|
* portsPerSpec, envFile, backupCommand },
|
|
19
|
+
* dev: [ { name, command, portVar, health?, frontPort? } ], // host dev
|
|
20
|
+
* // servers started on the spec's port block (empty = none)
|
|
21
|
+
* proxy: { enabled, host }, // bundled front-door proxy (spec-env connect)
|
|
19
22
|
* open: { command }, // optional, editor/terminal-agnostic opener
|
|
20
23
|
* registry: ".spec-env/registry.json",
|
|
21
24
|
* branch: { pattern, identifierField }, // git branch naming (provider-neutral)
|
|
@@ -40,6 +43,12 @@ const DEFAULT_CONFIG = Object.freeze({
|
|
|
40
43
|
envFile: '.env',
|
|
41
44
|
backupCommand: '',
|
|
42
45
|
}),
|
|
46
|
+
// Host dev servers started on the spec's port block by `spec-env dev up`.
|
|
47
|
+
// Each: { name, command, portVar, health?, frontPort? }. Default: none.
|
|
48
|
+
dev: Object.freeze([]),
|
|
49
|
+
// Front-door proxy (`spec-env connect`): a bundled Node reverse proxy that
|
|
50
|
+
// exposes one connected spec's frontPort processes on the canonical ports.
|
|
51
|
+
proxy: Object.freeze({ enabled: true, host: '127.0.0.1' }),
|
|
43
52
|
open: Object.freeze({ command: '' }),
|
|
44
53
|
registry: '.spec-env/registry.json',
|
|
45
54
|
// Git branch naming, provider-neutral. `pattern` expands {type}/{slug} and,
|
|
@@ -61,6 +70,8 @@ function defaults() {
|
|
|
61
70
|
return {
|
|
62
71
|
worktree: { ...DEFAULT_CONFIG.worktree },
|
|
63
72
|
docker: { ...DEFAULT_CONFIG.docker },
|
|
73
|
+
dev: [],
|
|
74
|
+
proxy: { ...DEFAULT_CONFIG.proxy },
|
|
64
75
|
open: { ...DEFAULT_CONFIG.open },
|
|
65
76
|
registry: DEFAULT_CONFIG.registry,
|
|
66
77
|
branch: { ...DEFAULT_CONFIG.branch },
|
|
@@ -85,6 +96,30 @@ function assign(base, parsed, key, type) {
|
|
|
85
96
|
}
|
|
86
97
|
}
|
|
87
98
|
|
|
99
|
+
/**
|
|
100
|
+
* Normalise a parsed `dev` array into well-formed process entries. Each entry
|
|
101
|
+
* needs non-empty string `name`, `command`, and `portVar`; `health` (string) and
|
|
102
|
+
* `frontPort` (finite number) are optional. Malformed entries are dropped
|
|
103
|
+
* (lenient, like the rest of the loader) so a stray entry can't crash provisioning.
|
|
104
|
+
*/
|
|
105
|
+
function normalizeDev(parsed) {
|
|
106
|
+
const out = []
|
|
107
|
+
for (const raw of parsed) {
|
|
108
|
+
if (!isObject(raw)) continue
|
|
109
|
+
const name = typeof raw.name === 'string' ? raw.name.trim() : ''
|
|
110
|
+
const command = typeof raw.command === 'string' ? raw.command.trim() : ''
|
|
111
|
+
const portVar = typeof raw.portVar === 'string' ? raw.portVar.trim() : ''
|
|
112
|
+
if (!name || !command || !portVar) continue
|
|
113
|
+
const entry = { name, command, portVar }
|
|
114
|
+
if (typeof raw.health === 'string' && raw.health.trim()) entry.health = raw.health.trim()
|
|
115
|
+
if (typeof raw.frontPort === 'number' && Number.isFinite(raw.frontPort)) {
|
|
116
|
+
entry.frontPort = raw.frontPort
|
|
117
|
+
}
|
|
118
|
+
out.push(entry)
|
|
119
|
+
}
|
|
120
|
+
return out
|
|
121
|
+
}
|
|
122
|
+
|
|
88
123
|
/**
|
|
89
124
|
* Merge a parsed config over the defaults. Only known keys are copied (unknown
|
|
90
125
|
* keys ignored for forward-compat). Nested objects are merged field-by-field.
|
|
@@ -107,6 +142,15 @@ function mergeConfig(base, parsed) {
|
|
|
107
142
|
assign(base.docker, parsed.docker, 'backupCommand', 'string?')
|
|
108
143
|
}
|
|
109
144
|
|
|
145
|
+
if (Array.isArray(parsed.dev)) {
|
|
146
|
+
base.dev = normalizeDev(parsed.dev)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (isObject(parsed.proxy)) {
|
|
150
|
+
assign(base.proxy, parsed.proxy, 'enabled', 'boolean')
|
|
151
|
+
assign(base.proxy, parsed.proxy, 'host', 'string')
|
|
152
|
+
}
|
|
153
|
+
|
|
110
154
|
if (isObject(parsed.open)) {
|
|
111
155
|
// command may be intentionally empty (no auto-open)
|
|
112
156
|
assign(base.open, parsed.open, 'command', 'string?')
|
package/src/env/dev.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Pure planner for host dev-process supervision (`spec-env dev up|down`).
|
|
5
|
+
*
|
|
6
|
+
* Given a resolved spec and its allocated slot, `planDev` computes — for each
|
|
7
|
+
* configured `dev` process — its resolved port, the env var to inject, the log
|
|
8
|
+
* and pid file paths, the expanded health URL, and the canonical `frontPort` a
|
|
9
|
+
* later proxy (Phase 2) routes to it. No side effects: the CLI's `supervise.js`
|
|
10
|
+
* does the spawning/killing; this stays unit-testable with no processes.
|
|
11
|
+
*
|
|
12
|
+
* Port math: process `i` in the slot's block gets `portBase + slot*portsPerSpec
|
|
13
|
+
* + i` (reusing the same block the Docker stack draws from — see registry.js).
|
|
14
|
+
* So host dev servers get a reserved block even on a worktree-only spec.
|
|
15
|
+
*
|
|
16
|
+
* Log/pid files live beside the registry (default `.spec-env/`) and are keyed by
|
|
17
|
+
* the spec **folder** (not the bare slug) so a `feat-foo`/`bug-foo` pair can't
|
|
18
|
+
* collide — matching how the registry keys slots.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const path = require('node:path')
|
|
22
|
+
const { portOffset } = require('./registry.js')
|
|
23
|
+
const { expandTokens } = require('./resolve.js')
|
|
24
|
+
|
|
25
|
+
// The state dir (logs + pids) sits beside the registry file, e.g. `.spec-env`.
|
|
26
|
+
function stateDir(config) {
|
|
27
|
+
return path.posix.dirname(config.registry) || '.spec-env'
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Plan the host dev processes for `spec` at `slot`.
|
|
32
|
+
*
|
|
33
|
+
* @returns {object} { slot, portOffset, procs: [{ name, command, port, portVar,
|
|
34
|
+
* env, frontPort, logFile, pidFile, health }] } — logFile/pidFile are paths
|
|
35
|
+
* relative to the primary checkout root (the CLI resolves them).
|
|
36
|
+
*/
|
|
37
|
+
function planDev(spec, slot, config) {
|
|
38
|
+
const base = portOffset(slot, config)
|
|
39
|
+
const dir = stateDir(config)
|
|
40
|
+
const procs = (config.dev || []).map((entry, i) => {
|
|
41
|
+
const port = base + i
|
|
42
|
+
const tokens = {
|
|
43
|
+
[entry.portVar]: String(port),
|
|
44
|
+
port: String(port),
|
|
45
|
+
slug: spec.slug,
|
|
46
|
+
name: entry.name,
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
name: entry.name,
|
|
50
|
+
command: expandTokens(entry.command, tokens),
|
|
51
|
+
port,
|
|
52
|
+
portVar: entry.portVar,
|
|
53
|
+
env: { [entry.portVar]: String(port) },
|
|
54
|
+
frontPort: typeof entry.frontPort === 'number' ? entry.frontPort : null,
|
|
55
|
+
logFile: `${dir}/logs/${spec.folder}-${entry.name}.log`,
|
|
56
|
+
pidFile: `${dir}/pids/${spec.folder}-${entry.name}.pid`,
|
|
57
|
+
health: entry.health ? expandTokens(entry.health, tokens) : null,
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
return { slot, portOffset: base, procs }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = { planDev, stateDir }
|
package/src/env/proxy.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Front-door reverse proxy for `spec-env connect` — a small, dependency-free
|
|
5
|
+
* Node proxy that exposes the ONE connected spec's warm dev servers on the
|
|
6
|
+
* canonical ports (exclusive model: no cookie, one target at a time).
|
|
7
|
+
*
|
|
8
|
+
* `renderRoutes` is pure (procs → routes). `startProxy` builds one `http` server
|
|
9
|
+
* per route, forwarding HTTP and `upgrade` (WebSocket/HMR) to the spec's port.
|
|
10
|
+
* Run directly (`node proxy.js <routesFile>`) it becomes the long-lived,
|
|
11
|
+
* detached proxy process the CLI supervises with the Phase 1 seam.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const http = require('node:http')
|
|
15
|
+
const net = require('node:net')
|
|
16
|
+
const fs = require('node:fs')
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The connected spec's `dev` processes that declare a `frontPort` → proxy routes.
|
|
20
|
+
* @param {Array} procs from `planDev(...).procs`
|
|
21
|
+
* @returns {Array<{name, frontPort, targetPort}>}
|
|
22
|
+
*/
|
|
23
|
+
function renderRoutes(procs) {
|
|
24
|
+
return (procs || [])
|
|
25
|
+
.filter((p) => typeof p.frontPort === 'number')
|
|
26
|
+
.map((p) => ({ name: p.name, frontPort: p.frontPort, targetPort: p.port }))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Build (but don't listen on) one proxy server: frontPort → targetPort. Forwards
|
|
30
|
+
// regular requests and transparently pipes `upgrade` (WebSocket) connections.
|
|
31
|
+
function createRouteServer(route, host = '127.0.0.1') {
|
|
32
|
+
const { targetPort } = route
|
|
33
|
+
|
|
34
|
+
const server = http.createServer((req, res) => {
|
|
35
|
+
const upstream = http.request(
|
|
36
|
+
{ host, port: targetPort, method: req.method, path: req.url, headers: req.headers },
|
|
37
|
+
(ur) => {
|
|
38
|
+
res.writeHead(ur.statusCode || 502, ur.headers)
|
|
39
|
+
ur.pipe(res)
|
|
40
|
+
},
|
|
41
|
+
)
|
|
42
|
+
upstream.on('error', () => {
|
|
43
|
+
if (!res.headersSent) res.writeHead(502, { 'content-type': 'text/plain' })
|
|
44
|
+
res.end(`spec-connect: upstream 127.0.0.1:${targetPort} not reachable\n`)
|
|
45
|
+
})
|
|
46
|
+
req.pipe(upstream)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
// WebSocket / HTTP upgrade passthrough — reconstruct the request line + headers
|
|
50
|
+
// onto a raw TCP connection to the upstream, then pipe both ways.
|
|
51
|
+
server.on('upgrade', (req, socket, head) => {
|
|
52
|
+
const upstream = net.connect(targetPort, host, () => {
|
|
53
|
+
let raw = `${req.method} ${req.url} HTTP/1.1\r\n`
|
|
54
|
+
for (let i = 0; i < req.rawHeaders.length; i += 2) {
|
|
55
|
+
raw += `${req.rawHeaders[i]}: ${req.rawHeaders[i + 1]}\r\n`
|
|
56
|
+
}
|
|
57
|
+
raw += '\r\n'
|
|
58
|
+
upstream.write(raw)
|
|
59
|
+
if (head && head.length) upstream.write(head)
|
|
60
|
+
socket.pipe(upstream)
|
|
61
|
+
upstream.pipe(socket)
|
|
62
|
+
})
|
|
63
|
+
upstream.on('error', () => socket.destroy())
|
|
64
|
+
socket.on('error', () => upstream.destroy())
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
return server
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Start every route's server listening. Returns `{ servers, close() }` where
|
|
72
|
+
* `close()` resolves once all servers are shut. Rejects (via the returned
|
|
73
|
+
* promise on the server 'error') if a port can't be bound.
|
|
74
|
+
*/
|
|
75
|
+
function startProxy(routes, { host = '127.0.0.1' } = {}) {
|
|
76
|
+
const servers = routes.map((route) => {
|
|
77
|
+
const server = createRouteServer(route, host)
|
|
78
|
+
server.listen(route.frontPort, host)
|
|
79
|
+
return server
|
|
80
|
+
})
|
|
81
|
+
return {
|
|
82
|
+
servers,
|
|
83
|
+
close() {
|
|
84
|
+
return Promise.all(
|
|
85
|
+
servers.map((s) => new Promise((resolve) => s.close(() => resolve()))),
|
|
86
|
+
)
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Which of `ports` are already bound (so `connect` can tell the operator to stop
|
|
93
|
+
* main first). Resolves to the list of busy ports. `host` scopes the check.
|
|
94
|
+
*/
|
|
95
|
+
function portsInUse(ports, host = '127.0.0.1') {
|
|
96
|
+
return Promise.all(
|
|
97
|
+
ports.map(
|
|
98
|
+
(port) =>
|
|
99
|
+
new Promise((resolve) => {
|
|
100
|
+
const tester = net
|
|
101
|
+
.createServer()
|
|
102
|
+
.once('error', () => resolve(port)) // EADDRINUSE → busy
|
|
103
|
+
.once('listening', () => tester.close(() => resolve(null)))
|
|
104
|
+
.listen(port, host)
|
|
105
|
+
}),
|
|
106
|
+
),
|
|
107
|
+
).then((results) => results.filter((p) => p !== null))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Does `port` accept a TCP connection right now?
|
|
111
|
+
function checkListening(port, host) {
|
|
112
|
+
return new Promise((resolve) => {
|
|
113
|
+
const sock = net.connect(port, host)
|
|
114
|
+
sock.once('connect', () => {
|
|
115
|
+
sock.destroy()
|
|
116
|
+
resolve(true)
|
|
117
|
+
})
|
|
118
|
+
sock.once('error', () => {
|
|
119
|
+
sock.destroy()
|
|
120
|
+
resolve(false)
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Poll until every port in `ports` accepts a connection (the detached proxy has
|
|
127
|
+
* finished binding), or `timeoutMs` elapses. Resolves true when all are up.
|
|
128
|
+
* `now`/`wait` are injectable for tests.
|
|
129
|
+
*/
|
|
130
|
+
async function waitListening(
|
|
131
|
+
ports,
|
|
132
|
+
{ host = '127.0.0.1', timeoutMs = 3000, intervalMs = 50, now = () => Date.now(), wait } = {},
|
|
133
|
+
) {
|
|
134
|
+
const sleep = wait || ((ms) => new Promise((r) => setTimeout(r, ms)))
|
|
135
|
+
const deadline = now() + timeoutMs
|
|
136
|
+
while (now() < deadline) {
|
|
137
|
+
const results = await Promise.all(ports.map((p) => checkListening(p, host)))
|
|
138
|
+
if (results.every(Boolean)) return true
|
|
139
|
+
await sleep(intervalMs)
|
|
140
|
+
}
|
|
141
|
+
return false
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
module.exports = { renderRoutes, createRouteServer, startProxy, portsInUse, waitListening }
|
|
145
|
+
|
|
146
|
+
// Entry point: run as a detached process by the CLI. Reads its routes from a
|
|
147
|
+
// file so a re-`connect` just rewrites the file and restarts this (tiny) process.
|
|
148
|
+
if (require.main === module) {
|
|
149
|
+
const routesFile = process.argv[2]
|
|
150
|
+
if (!routesFile) {
|
|
151
|
+
process.stderr.write('proxy: usage: node proxy.js <routesFile>\n')
|
|
152
|
+
process.exit(1)
|
|
153
|
+
}
|
|
154
|
+
const routes = JSON.parse(fs.readFileSync(routesFile, 'utf-8'))
|
|
155
|
+
const { close } = startProxy(routes)
|
|
156
|
+
const shutdown = () => close().then(() => process.exit(0))
|
|
157
|
+
process.on('SIGTERM', shutdown)
|
|
158
|
+
process.on('SIGINT', shutdown)
|
|
159
|
+
}
|
package/src/env/resolve.js
CHANGED
|
@@ -20,12 +20,17 @@ const BUCKETS = ['backlog', 'in-progress', 'complete', 'cancelled']
|
|
|
20
20
|
|
|
21
21
|
// Find the spec folder under specs/<bucket>/<name>. `specArg` may be a bare
|
|
22
22
|
// folder name or a path — only its basename is matched against the buckets.
|
|
23
|
-
|
|
23
|
+
// Searches `dir` first, then any `extraDirs` in order — so a caller (e.g.
|
|
24
|
+
// `spec-env integrate`) can fall back to a worktree checkout for a spec that
|
|
25
|
+
// was authored on its branch and never committed to the primary checkout.
|
|
26
|
+
function findSpecFolder(specArg, dir, extraDirs = []) {
|
|
24
27
|
const name = path.basename(specArg)
|
|
25
|
-
for (const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
for (const root of [dir, ...extraDirs]) {
|
|
29
|
+
for (const bucket of BUCKETS) {
|
|
30
|
+
const abs = path.join(root, 'specs', bucket, name)
|
|
31
|
+
if (fs.existsSync(abs) && fs.statSync(abs).isDirectory()) {
|
|
32
|
+
return { folder: name, bucket, path: abs }
|
|
33
|
+
}
|
|
29
34
|
}
|
|
30
35
|
}
|
|
31
36
|
return null
|
|
@@ -149,9 +154,13 @@ function resolveBaseBranch(config, git) {
|
|
|
149
154
|
/**
|
|
150
155
|
* Resolve a spec argument to its identity + isolation coordinates.
|
|
151
156
|
* Throws a clear Error when the spec folder can't be found.
|
|
157
|
+
*
|
|
158
|
+
* `opts.searchDirs` adds fallback checkout roots to look under (after `dir`) when
|
|
159
|
+
* locating the spec folder; identity/coordinate tokens still expand against `dir`
|
|
160
|
+
* (the primary checkout), so a worktree-only spec resolves to the right base.
|
|
152
161
|
*/
|
|
153
|
-
function resolveSpec(specArg, dir, config) {
|
|
154
|
-
const found = findSpecFolder(specArg, dir)
|
|
162
|
+
function resolveSpec(specArg, dir, config, opts = {}) {
|
|
163
|
+
const found = findSpecFolder(specArg, dir, opts.searchDirs || [])
|
|
155
164
|
if (!found) {
|
|
156
165
|
throw new Error(`spec not found under specs/**: ${specArg}`)
|
|
157
166
|
}
|