@kurvball/rehearse 1.13.2 → 1.13.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,7 @@ import { addToWalk, beginWalk, failed, driveArgv, exitOf, parseDrive, resultText
7
7
  import { sh, tail } from '../exec.js';
8
8
  import { fillEnv } from '../manifest/index.js';
9
9
  import { applyWorld, baseVars } from '../run/index.js';
10
- import { alive, runningServices, stateDir, bringDown, bringUp, handoverSettled, release, } from '../services/index.js';
10
+ import { alive, runningServices, stateDir, bringDown, bringUp, envOf, handoverSettled, release, } from '../services/index.js';
11
11
  const quote = (s) => `'${s.replace(/'/g, `'\\''`)}'`;
12
12
  const here = fileURLToPath(import.meta.url);
13
13
  const daemonFile = path.join(path.dirname(here), '..', 'drive', `daemon${path.extname(here)}`);
@@ -197,10 +197,7 @@ Each answers in the drive result shape (schema/v1/drive.schema.json): 0 done, 1
197
197
  if (command.command === 'fill')
198
198
  return answer(failed('fill', at, 'usage', `${svc.name}'s driver takes no fill step: click the field, then type.`, 'service'));
199
199
  const cwd = svc.cwd ? path.resolve(driving.checkout, svc.cwd) : driving.checkout;
200
- const env = running?.env ?? {
201
- ...fillEnv(svc.env, {}),
202
- ...(svc.isolate && running?.scratch && { [svc.isolate.env]: running.scratch }),
203
- };
200
+ const env = running ? envOf(m, running) : fillEnv(svc.env, {});
204
201
  const ran = await sh(`${svc.drive} ${driveArgv(command).map(quote).join(' ')} --json`, {
205
202
  cwd,
206
203
  env,
@@ -4,13 +4,13 @@ import { jsonDifferences, noCounts, setAsideText, textDifferences, } from '../..
4
4
  import { act, contextFor, driveArgv, errorsOf, FIXED_TIME, launch, seenTree, signInBySteps, signInOf, stepCommand, stepOf, stepWords, } from '../../drive/index.js';
5
5
  import { errorText } from '../../errors.js';
6
6
  import { sh, tail } from '../../exec.js';
7
- import { fill, fillEnv, hashOf, readYaml } from '../../manifest/index.js';
7
+ import { fill, hashOf, readYaml } from '../../manifest/index.js';
8
8
  import { scrub, scrubDeep } from '../../people/index.js';
9
9
  import { readScenarios, registerReader } from '../../scenarios/index.js';
10
10
  import { validate } from '../../schema.js';
11
11
  import { broken, registerRunner } from '../kind.js';
12
12
  import { startPieceServer } from '../piece/server.js';
13
- import { handoverSettled, restart, restartable, stateDir } from '../../services/index.js';
13
+ import { envOf, handoverSettled, restart, restartable, stateDir, } from '../../services/index.js';
14
14
  import { applyWorld } from '../../worlds/index.js';
15
15
  import { defaultExportOf, LiteralError } from './literal.js';
16
16
  const refuse = (says, line) => {
@@ -96,10 +96,7 @@ async function throughDriver(s, ctx) {
96
96
  const svc = m.services[s.service];
97
97
  const running = ctx.running.find((r) => r.name === svc.name);
98
98
  const cwd = svc.cwd ? path.resolve(ctx.checkout, svc.cwd) : ctx.checkout;
99
- const env = running.env ?? {
100
- ...fillEnv(svc.env, ctx.variables),
101
- ...(svc.isolate && running.scratch && { [svc.isolate.env]: running.scratch }),
102
- };
99
+ const env = envOf(m, running);
103
100
  const drive = async (argv) => {
104
101
  const ran = await sh(`${svc.drive} ${argv.map(quote).join(' ')} --json`, { cwd, env, timeoutMs: 60_000 });
105
102
  try {
@@ -1,2 +1,2 @@
1
1
  export { alive, runningServices, stateDir, type Running } from './registry.ts';
2
- export { bringDown, bringUp, downStartedHere, freePort, handoverSettled, inUse, liveServices, makeRoom, release, restart, restartable, standinsFor, } from './service.ts';
2
+ export { bringDown, bringUp, downStartedHere, envOf, freePort, handoverSettled, inUse, liveServices, makeRoom, release, restart, restartable, standinsFor, } from './service.ts';
@@ -1,2 +1,2 @@
1
1
  export { alive, runningServices, stateDir } from './registry.js';
2
- export { bringDown, bringUp, downStartedHere, freePort, handoverSettled, inUse, liveServices, makeRoom, release, restart, restartable, standinsFor, } from './service.js';
2
+ export { bringDown, bringUp, downStartedHere, envOf, freePort, handoverSettled, inUse, liveServices, makeRoom, release, restart, restartable, standinsFor, } from './service.js';
@@ -10,6 +10,7 @@ export interface Running {
10
10
  standins: string[];
11
11
  up?: string;
12
12
  env?: Record<string, string>;
13
+ vars?: Record<string, string>;
13
14
  world?: string | null;
14
15
  daemon?: boolean;
15
16
  starting?: boolean;
@@ -1,5 +1,5 @@
1
1
  import { createHash } from 'node:crypto';
2
- import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
2
+ import { mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
3
3
  import path from 'node:path';
4
4
  export const stateDir = (manifestDir) => path.join(manifestDir, '.rehearse');
5
5
  const registryDir = (manifestDir) => path.join(stateDir(manifestDir), 'services');
@@ -15,8 +15,12 @@ export const alive = (pid) => {
15
15
  }
16
16
  };
17
17
  export function remember(manifestDir, r) {
18
+ const { env: _keptInMemoryOnly, ...onDisk } = r;
19
+ const file = fileOf(manifestDir, r.name, r.checkout);
20
+ const writing = `${file}.${process.pid}.writing`;
18
21
  mkdirSync(registryDir(manifestDir), { recursive: true });
19
- writeFileSync(fileOf(manifestDir, r.name, r.checkout), `${JSON.stringify(r, null, 2)}\n`);
22
+ writeFileSync(writing, `${JSON.stringify(onDisk, null, 2)}\n`);
23
+ renameSync(writing, file);
20
24
  }
21
25
  export function rememberIfStillUp(manifestDir, r) {
22
26
  const file = fileOf(manifestDir, r.name, r.checkout);
@@ -16,6 +16,7 @@ export declare const logOf: (m: Manifest, name: string, checkout: string) => str
16
16
  export declare function liveServices(m: Manifest, checkout?: string | null): Promise<Running[]>;
17
17
  export declare const restartable: (r: Running) => boolean;
18
18
  export declare function handoverSettled(r: Running): Promise<void>;
19
+ export declare function envOf(m: Manifest, r: Running): Record<string, string>;
19
20
  export declare function restart(m: Manifest, r: Running, whileDown?: () => Promise<void>): Promise<string | null>;
20
21
  export declare const release: (r: Running) => boolean;
21
22
  export declare function downStartedHere(): Promise<void>;
@@ -121,9 +121,8 @@ export async function liveServices(m, checkout = null) {
121
121
  continue;
122
122
  }
123
123
  const s = m.services[r.name];
124
- const vars = readyVarsOf(m, r);
125
124
  const answers = s?.ready
126
- ? await readyNow(s.ready, vars, r.env ?? {}, s.cwd ? path.resolve(r.checkout, s.cwd) : r.checkout, '', r.port)
125
+ ? await readyNow(s.ready, varsOf(m, r), envOf(m, r), s.cwd ? path.resolve(r.checkout, s.cwd) : r.checkout, '', r.port)
127
126
  : r.port !== null
128
127
  ? await portTaken(r.port)
129
128
  : false;
@@ -149,22 +148,48 @@ export async function handoverSettled(r) {
149
148
  }
150
149
  upsStillExiting.delete(r);
151
150
  }
152
- function readyVarsOf(m, r) {
153
- return {
151
+ function varsOf(m, r) {
152
+ return (r.vars ?? {
154
153
  checkout: r.checkout,
155
154
  manifest: m.dir,
156
155
  ...(r.scratch && { scratch: r.scratch }),
157
156
  ...(r.world && { world: r.world }),
158
157
  ...(r.port !== null && { port: String(r.port) }),
159
158
  ...(r.url && { url: r.url }),
159
+ });
160
+ }
161
+ function serviceEnv(s, standins, vars, scratch, port) {
162
+ return {
163
+ ...fillEnv(s.env, vars),
164
+ ...Object.assign({}, ...standins.map(({ standin, real }) => fillEnv(real ? standin.real.env : standin.env, vars))),
165
+ ...(s.isolate && scratch && { [s.isolate.env]: scratch }),
166
+ ...(port !== null && { PORT: String(port) }),
167
+ ...(scratch && { REHEARSE_SCRATCH: scratch }),
160
168
  };
161
169
  }
170
+ const REAL = ':real';
171
+ export function envOf(m, r) {
172
+ if (r.env)
173
+ return r.env;
174
+ const s = m.services[r.name];
175
+ if (!s)
176
+ return {
177
+ ...(r.port !== null && { PORT: String(r.port) }),
178
+ ...(r.scratch && { REHEARSE_SCRATCH: r.scratch }),
179
+ };
180
+ const standins = r.standins.flatMap((named) => {
181
+ const real = named.endsWith(REAL);
182
+ const standin = m.standins[real ? named.slice(0, -REAL.length) : named];
183
+ return standin && (!real || standin.real) ? [{ standin, real }] : [];
184
+ });
185
+ return serviceEnv(s, standins, varsOf(m, r), r.scratch, r.port);
186
+ }
162
187
  export async function restart(m, r, whileDown = async () => undefined) {
163
188
  const s = m.services[r.name];
164
189
  if (!s || !restartable(r))
165
190
  return null;
166
191
  const cwd = s.cwd ? path.resolve(r.checkout, s.cwd) : r.checkout;
167
- const env = r.env ?? {};
192
+ const env = envOf(m, r);
168
193
  await sh(r.down, { cwd, env, timeoutMs: 60_000 });
169
194
  await whileDown();
170
195
  const up = await sh(r.up, { cwd, env, timeoutMs: s.timeoutMs });
@@ -172,7 +197,7 @@ export async function restart(m, r, whileDown = async () => undefined) {
172
197
  writeFileSync(upSaidThisTime, `${up.stdout}${up.stderr}`);
173
198
  if (up.code !== 0)
174
199
  return `${s.name} didn't come up again (exit ${up.code}): ${tail(up.stderr || up.stdout)}`;
175
- const vars = readyVarsOf(m, r);
200
+ const vars = varsOf(m, r);
176
201
  const begun = Date.now();
177
202
  while (!(await readyNow(s.ready, vars, env, cwd, upSaidThisTime, r.port))) {
178
203
  if (Date.now() - begun > s.timeoutMs)
@@ -201,7 +226,7 @@ export async function bringUp(m, s, checkout, options) {
201
226
  if (await readyNow(s.ready, {
202
227
  ...(already.url && { url: already.url }),
203
228
  ...(already.port !== null && { port: String(already.port) }),
204
- }, already.env ?? {}, s.cwd ? path.resolve(checkout, s.cwd) : checkout, '', already.port))
229
+ }, envOf(m, already), s.cwd ? path.resolve(checkout, s.cwd) : checkout, '', already.port))
205
230
  return already;
206
231
  forget(m.dir, already.name, already.checkout);
207
232
  }
@@ -230,13 +255,7 @@ export async function bringUp(m, s, checkout, options) {
230
255
  const url = s.url === null ? null : fill(s.url, vars);
231
256
  if (url !== null)
232
257
  vars.url = url;
233
- const env = {
234
- ...fillEnv(s.env, vars),
235
- ...Object.assign({}, ...standins.map(({ standin, real }) => fillEnv(real ? standin.real.env : standin.env, vars))),
236
- ...(s.isolate && { [s.isolate.env]: scratch }),
237
- ...(port !== null && { PORT: String(port) }),
238
- REHEARSE_SCRATCH: scratch,
239
- };
258
+ const env = serviceEnv(s, standins, vars, scratch, port);
240
259
  if (stoppingWhatStartedHere) {
241
260
  rmSync(scratch, { recursive: true, force: true });
242
261
  throw new RehearseError('service', `Rehearse is stopping, so it doesn't bring ${s.name} up.`);
@@ -276,6 +295,7 @@ export async function bringUp(m, s, checkout, options) {
276
295
  standins: standins.map(({ standin, real }) => (real ? `${standin.name}:real` : standin.name)),
277
296
  up: command,
278
297
  env,
298
+ vars,
279
299
  world: options.world,
280
300
  starting: true,
281
301
  };
@@ -320,7 +340,7 @@ export async function bringDown(m, r, progress) {
320
340
  await sh(r.down, {
321
341
  cwd,
322
342
  timeoutMs: 30_000,
323
- env: r.env ?? (r.scratch && s?.isolate ? { [s.isolate.env]: r.scratch } : {}),
343
+ env: envOf(m, r),
324
344
  });
325
345
  }
326
346
  killGroup(r.pid, 'SIGTERM');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kurvball/rehearse",
3
- "version": "1.13.2",
3
+ "version": "1.13.3",
4
4
  "description": "Rehearse a change before it ships: bring the product up, sign in as named people, start from named worlds, and compare any scenario on the base and on the head.",
5
5
  "homepage": "https://rehearse.kurvball.ca/",
6
6
  "repository": {