@kurvball/rehearse 0.0.0-stage → 1.3.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 +107 -2
- package/dist/affected/affected.d.ts +22 -0
- package/dist/affected/affected.js +162 -0
- package/dist/affected/index.d.ts +1 -0
- package/dist/affected/index.js +1 -0
- package/dist/args.d.ts +12 -0
- package/dist/args.js +42 -0
- package/dist/cli.d.ts +4 -0
- package/dist/cli.js +100 -0
- package/dist/commands/check.d.ts +2 -0
- package/dist/commands/check.js +42 -0
- package/dist/commands/clean.d.ts +2 -0
- package/dist/commands/clean.js +37 -0
- package/dist/commands/command.d.ts +30 -0
- package/dist/commands/command.js +38 -0
- package/dist/commands/compare.d.ts +4 -0
- package/dist/commands/compare.js +89 -0
- package/dist/commands/drive.d.ts +2 -0
- package/dist/commands/drive.js +244 -0
- package/dist/commands/index.d.ts +3 -0
- package/dist/commands/index.js +32 -0
- package/dist/commands/init.d.ts +2 -0
- package/dist/commands/init.js +19 -0
- package/dist/commands/list.d.ts +2 -0
- package/dist/commands/list.js +69 -0
- package/dist/commands/open.d.ts +2 -0
- package/dist/commands/open.js +60 -0
- package/dist/commands/people.d.ts +2 -0
- package/dist/commands/people.js +49 -0
- package/dist/commands/run.d.ts +4 -0
- package/dist/commands/run.js +84 -0
- package/dist/commands/services.d.ts +28 -0
- package/dist/commands/services.js +51 -0
- package/dist/commands/stage.d.ts +7 -0
- package/dist/commands/stage.js +53 -0
- package/dist/commands/up.d.ts +3 -0
- package/dist/commands/up.js +77 -0
- package/dist/commands/validate.d.ts +2 -0
- package/dist/commands/validate.js +35 -0
- package/dist/commands/worlds.d.ts +2 -0
- package/dist/commands/worlds.js +15 -0
- package/dist/compare/announce.d.ts +6 -0
- package/dist/compare/announce.js +27 -0
- package/dist/compare/compare.d.ts +83 -0
- package/dist/compare/compare.js +335 -0
- package/dist/compare/diff.d.ts +15 -0
- package/dist/compare/diff.js +105 -0
- package/dist/compare/index.d.ts +5 -0
- package/dist/compare/index.js +5 -0
- package/dist/compare/setaside.d.ts +29 -0
- package/dist/compare/setaside.js +121 -0
- package/dist/compare/verdict.d.ts +21 -0
- package/dist/compare/verdict.js +30 -0
- package/dist/drive/aria.d.ts +8 -0
- package/dist/drive/aria.js +12 -0
- package/dist/drive/browser.d.ts +34 -0
- package/dist/drive/browser.js +216 -0
- package/dist/drive/daemon.d.ts +1 -0
- package/dist/drive/daemon.js +171 -0
- package/dist/drive/grammar.d.ts +93 -0
- package/dist/drive/grammar.js +303 -0
- package/dist/drive/index.d.ts +4 -0
- package/dist/drive/index.js +4 -0
- package/dist/drive/names.d.ts +24 -0
- package/dist/drive/names.js +97 -0
- package/dist/drive/state.d.ts +13 -0
- package/dist/drive/state.js +28 -0
- package/dist/errors.d.ts +26 -0
- package/dist/errors.js +32 -0
- package/dist/exec.d.ts +16 -0
- package/dist/exec.js +43 -0
- package/dist/git/checkout.d.ts +11 -0
- package/dist/git/checkout.js +90 -0
- package/dist/git/git.d.ts +7 -0
- package/dist/git/git.js +27 -0
- package/dist/git/index.d.ts +2 -0
- package/dist/git/index.js +2 -0
- package/dist/git/seed.d.ts +3 -0
- package/dist/git/seed.js +115 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +15 -0
- package/dist/kinds/all.d.ts +4 -0
- package/dist/kinds/all.js +4 -0
- package/dist/kinds/call/check.d.ts +29 -0
- package/dist/kinds/call/check.js +187 -0
- package/dist/kinds/call/definition.d.ts +16 -0
- package/dist/kinds/call/definition.js +33 -0
- package/dist/kinds/call/hurl.d.ts +65 -0
- package/dist/kinds/call/hurl.js +333 -0
- package/dist/kinds/call/kind.d.ts +8 -0
- package/dist/kinds/call/kind.js +122 -0
- package/dist/kinds/call/send.d.ts +32 -0
- package/dist/kinds/call/send.js +152 -0
- package/dist/kinds/call/words.d.ts +22 -0
- package/dist/kinds/call/words.js +24 -0
- package/dist/kinds/file/kind.d.ts +44 -0
- package/dist/kinds/file/kind.js +331 -0
- package/dist/kinds/file/layouts.d.ts +14 -0
- package/dist/kinds/file/layouts.js +103 -0
- package/dist/kinds/flow/kind.d.ts +21 -0
- package/dist/kinds/flow/kind.js +412 -0
- package/dist/kinds/flow/literal.d.ts +8 -0
- package/dist/kinds/flow/literal.js +148 -0
- package/dist/kinds/index.d.ts +10 -0
- package/dist/kinds/index.js +8 -0
- package/dist/kinds/kind.d.ts +63 -0
- package/dist/kinds/kind.js +21 -0
- package/dist/kinds/piece/canvas.d.ts +18 -0
- package/dist/kinds/piece/canvas.js +165 -0
- package/dist/kinds/piece/conventions.d.ts +12 -0
- package/dist/kinds/piece/conventions.js +101 -0
- package/dist/kinds/piece/csf.d.ts +10 -0
- package/dist/kinds/piece/csf.js +304 -0
- package/dist/kinds/piece/kind.d.ts +12 -0
- package/dist/kinds/piece/kind.js +297 -0
- package/dist/kinds/piece/server.d.ts +23 -0
- package/dist/kinds/piece/server.js +236 -0
- package/dist/manifest/fill.d.ts +4 -0
- package/dist/manifest/fill.js +7 -0
- package/dist/manifest/index.d.ts +5 -0
- package/dist/manifest/index.js +5 -0
- package/dist/manifest/init.d.ts +1 -0
- package/dist/manifest/init.js +47 -0
- package/dist/manifest/load.d.ts +7 -0
- package/dist/manifest/load.js +245 -0
- package/dist/manifest/types.d.ts +255 -0
- package/dist/manifest/types.js +7 -0
- package/dist/manifest/yaml.d.ts +5 -0
- package/dist/manifest/yaml.js +47 -0
- package/dist/people/index.d.ts +1 -0
- package/dist/people/index.js +1 -0
- package/dist/people/people.d.ts +39 -0
- package/dist/people/people.js +139 -0
- package/dist/run/cache.d.ts +20 -0
- package/dist/run/cache.js +98 -0
- package/dist/run/index.d.ts +5 -0
- package/dist/run/index.js +4 -0
- package/dist/run/observation.d.ts +27 -0
- package/dist/run/observation.js +1 -0
- package/dist/run/parallel.d.ts +28 -0
- package/dist/run/parallel.js +140 -0
- package/dist/run/session.d.ts +35 -0
- package/dist/run/session.js +399 -0
- package/dist/run/vars.d.ts +3 -0
- package/dist/run/vars.js +13 -0
- package/dist/scenarios/area.d.ts +6 -0
- package/dist/scenarios/area.js +37 -0
- package/dist/scenarios/files.d.ts +9 -0
- package/dist/scenarios/files.js +26 -0
- package/dist/scenarios/glob.d.ts +6 -0
- package/dist/scenarios/glob.js +106 -0
- package/dist/scenarios/index.d.ts +4 -0
- package/dist/scenarios/index.js +4 -0
- package/dist/scenarios/read.d.ts +18 -0
- package/dist/scenarios/read.js +54 -0
- package/dist/scenarios/scenario.d.ts +34 -0
- package/dist/scenarios/scenario.js +10 -0
- package/dist/schema.d.ts +14 -0
- package/dist/schema.js +122 -0
- package/dist/services/index.d.ts +2 -0
- package/dist/services/index.js +2 -0
- package/dist/services/registry.d.ts +23 -0
- package/dist/services/registry.js +49 -0
- package/dist/services/service.d.ts +23 -0
- package/dist/services/service.js +313 -0
- package/dist/stage/api.d.ts +30 -0
- package/dist/stage/api.js +206 -0
- package/dist/stage/client/client/api.js +11 -0
- package/dist/stage/client/client/index.js +1 -0
- package/dist/stage/client/features/canvas/call.js +65 -0
- package/dist/stage/client/features/canvas/context.js +1 -0
- package/dist/stage/client/features/canvas/file.js +36 -0
- package/dist/stage/client/features/canvas/flow.js +59 -0
- package/dist/stage/client/features/canvas/index.js +1 -0
- package/dist/stage/client/features/canvas/observed.js +8 -0
- package/dist/stage/client/features/canvas/parts.js +24 -0
- package/dist/stage/client/features/canvas/piece.js +31 -0
- package/dist/stage/client/features/canvas/reach.js +6 -0
- package/dist/stage/client/features/canvas/views.js +16 -0
- package/dist/stage/client/features/list/index.js +1 -0
- package/dist/stage/client/features/list/list.js +53 -0
- package/dist/stage/client/features/list/reach.js +10 -0
- package/dist/stage/client/features/scenario/heading.js +14 -0
- package/dist/stage/client/features/scenario/index.js +1 -0
- package/dist/stage/client/features/scenario/reach.js +9 -0
- package/dist/stage/client/features/scenario/scenario.js +145 -0
- package/dist/stage/client/features/try/index.js +1 -0
- package/dist/stage/client/features/try/reach.js +12 -0
- package/dist/stage/client/features/try/try.js +65 -0
- package/dist/stage/client/index.html +16 -0
- package/dist/stage/client/main.js +24 -0
- package/dist/stage/client/primitives/busy.js +6 -0
- package/dist/stage/client/primitives/focus.js +21 -0
- package/dist/stage/client/primitives/h.js +25 -0
- package/dist/stage/client/primitives/index.js +6 -0
- package/dist/stage/client/primitives/keyed.js +16 -0
- package/dist/stage/client/primitives/say.js +6 -0
- package/dist/stage/client/primitives/state.js +21 -0
- package/dist/stage/client/primitives/types.js +1 -0
- package/dist/stage/client/primitives/verdicts.js +8 -0
- package/dist/stage/client/stage.css +652 -0
- package/dist/stage/compared.d.ts +20 -0
- package/dist/stage/compared.js +58 -0
- package/dist/stage/files.d.ts +4 -0
- package/dist/stage/files.js +59 -0
- package/dist/stage/guard.d.ts +3 -0
- package/dist/stage/guard.js +20 -0
- package/dist/stage/http.d.ts +12 -0
- package/dist/stage/http.js +34 -0
- package/dist/stage/index.d.ts +1 -0
- package/dist/stage/index.js +1 -0
- package/dist/stage/sending.d.ts +18 -0
- package/dist/stage/sending.js +66 -0
- package/dist/stage/server.d.ts +33 -0
- package/dist/stage/server.js +253 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +5 -0
- package/dist/worlds/define.d.ts +16 -0
- package/dist/worlds/define.js +45 -0
- package/dist/worlds/index.d.ts +2 -0
- package/dist/worlds/index.js +2 -0
- package/dist/worlds/worlds.d.ts +15 -0
- package/dist/worlds/worlds.js +46 -0
- package/llms.txt +46 -0
- package/package.json +84 -3
- package/schema/v1/announce.schema.json +28 -0
- package/schema/v1/check.schema.json +67 -0
- package/schema/v1/clean.schema.json +19 -0
- package/schema/v1/common.schema.json +210 -0
- package/schema/v1/comparison.schema.json +298 -0
- package/schema/v1/dependents.schema.json +30 -0
- package/schema/v1/drive.schema.json +112 -0
- package/schema/v1/error.schema.json +26 -0
- package/schema/v1/file-scenario.schema.json +175 -0
- package/schema/v1/flow.schema.json +216 -0
- package/schema/v1/init.schema.json +20 -0
- package/schema/v1/list.schema.json +70 -0
- package/schema/v1/manifest.schema.json +434 -0
- package/schema/v1/observation.schema.json +388 -0
- package/schema/v1/open.schema.json +14 -0
- package/schema/v1/people.schema.json +88 -0
- package/schema/v1/samples/announce.yaml +5 -0
- package/schema/v1/samples/check.json +13 -0
- package/schema/v1/samples/clean.json +1 -0
- package/schema/v1/samples/comparison.json +83 -0
- package/schema/v1/samples/dependents.json +7 -0
- package/schema/v1/samples/drive.json +10 -0
- package/schema/v1/samples/error.json +10 -0
- package/schema/v1/samples/file-scenario.yaml +13 -0
- package/schema/v1/samples/flow.yaml +13 -0
- package/schema/v1/samples/init.json +1 -0
- package/schema/v1/samples/list.json +24 -0
- package/schema/v1/samples/manifest.yaml +55 -0
- package/schema/v1/samples/observation.json +49 -0
- package/schema/v1/samples/open.json +6 -0
- package/schema/v1/samples/people.json +11 -0
- package/schema/v1/samples/services.json +27 -0
- package/schema/v1/samples/stage.json +6 -0
- package/schema/v1/samples/try.json +13 -0
- package/schema/v1/samples/up.json +16 -0
- package/schema/v1/samples/validate.json +7 -0
- package/schema/v1/samples/worlds.json +21 -0
- package/schema/v1/services.schema.json +72 -0
- package/schema/v1/stage.schema.json +14 -0
- package/schema/v1/try.schema.json +56 -0
- package/schema/v1/up.schema.json +55 -0
- package/schema/v1/validate.schema.json +40 -0
- package/schema/v1/worlds.schema.json +33 -0
- package/skill/rehearse/SKILL.md +148 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { KINDS } from '../manifest/index.js';
|
|
3
|
+
import { filesOf } from './files.js';
|
|
4
|
+
import { areaOf } from './area.js';
|
|
5
|
+
const readers = new Map();
|
|
6
|
+
export function registerReader(reader) {
|
|
7
|
+
readers.set(reader.kind, reader);
|
|
8
|
+
}
|
|
9
|
+
export async function readScenarios(m, checkout, kinds = KINDS) {
|
|
10
|
+
await (await import('../kinds/index.js')).loadKinds();
|
|
11
|
+
const out = { scenarios: [], refused: [] };
|
|
12
|
+
for (const kind of kinds) {
|
|
13
|
+
const reader = readers.get(kind);
|
|
14
|
+
for (const f of filesOf(m, kind, checkout)) {
|
|
15
|
+
if (!reader) {
|
|
16
|
+
out.refused.push({ file: f.file, says: `Rehearse has no reader for ${kind} scenarios yet.` });
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
let text;
|
|
20
|
+
try {
|
|
21
|
+
text = readFileSync(f.abs, 'utf8');
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
out.refused.push({ file: f.file, says: `Rehearse can't read it: ${error.message}` });
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
out.scenarios.push(...reader.read(f, text, { manifest: m, checkout, area: areaOf(m, checkout, f) }));
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
const e = error;
|
|
32
|
+
out.refused.push({
|
|
33
|
+
file: f.file,
|
|
34
|
+
...(e.line !== undefined && { line: e.line }),
|
|
35
|
+
says: e.says ?? e.message ?? String(error),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const seen = new Map();
|
|
41
|
+
for (const s of out.scenarios) {
|
|
42
|
+
const twice = seen.get(s.id);
|
|
43
|
+
if (twice)
|
|
44
|
+
out.refused.push({
|
|
45
|
+
file: s.file,
|
|
46
|
+
...(s.line !== null && { line: s.line }),
|
|
47
|
+
says: `Two scenarios have the id “${s.id}”.`,
|
|
48
|
+
});
|
|
49
|
+
else
|
|
50
|
+
seen.set(s.id, s);
|
|
51
|
+
}
|
|
52
|
+
out.scenarios = [...seen.values()];
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Kind } from '../manifest/index.ts';
|
|
2
|
+
export interface Scenario {
|
|
3
|
+
id: string;
|
|
4
|
+
kind: Kind;
|
|
5
|
+
name: string;
|
|
6
|
+
file: string;
|
|
7
|
+
abs: string;
|
|
8
|
+
line: number | null;
|
|
9
|
+
from: 'checkout' | 'manifest';
|
|
10
|
+
area: string;
|
|
11
|
+
definition: string;
|
|
12
|
+
context?: string;
|
|
13
|
+
world: string | null;
|
|
14
|
+
person: string | null;
|
|
15
|
+
service: string | null;
|
|
16
|
+
covers: string[];
|
|
17
|
+
spec: unknown;
|
|
18
|
+
}
|
|
19
|
+
export interface Refused {
|
|
20
|
+
file: string;
|
|
21
|
+
line?: number;
|
|
22
|
+
says: string;
|
|
23
|
+
}
|
|
24
|
+
export interface ScenarioRef {
|
|
25
|
+
id: string;
|
|
26
|
+
kind: Kind;
|
|
27
|
+
name: string;
|
|
28
|
+
file: string;
|
|
29
|
+
line?: number;
|
|
30
|
+
area: string;
|
|
31
|
+
from: 'checkout' | 'manifest';
|
|
32
|
+
definition: string;
|
|
33
|
+
}
|
|
34
|
+
export declare const refOf: (s: Scenario) => ScenarioRef;
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ErrorObject, type ValidateFunction } from 'ajv/dist/2020.js';
|
|
2
|
+
export declare const SCHEMA_DIR: string;
|
|
3
|
+
export declare const SCHEMAS: readonly ['manifest', 'flow', 'file-scenario', 'announce', 'services', 'people', 'worlds', 'list', 'observation', 'comparison', 'drive', 'open', 'stage', 'try', 'error', 'dependents', 'check', 'init', 'validate', 'up', 'clean'];
|
|
4
|
+
export type SchemaName = (typeof SCHEMAS)[number];
|
|
5
|
+
export declare const schemaId: (name: SchemaName | 'common') => string;
|
|
6
|
+
export declare function validatorOf(name: SchemaName): ValidateFunction;
|
|
7
|
+
export interface Invalid {
|
|
8
|
+
at: string[];
|
|
9
|
+
key?: string;
|
|
10
|
+
says: string;
|
|
11
|
+
}
|
|
12
|
+
export declare const placeOf: (at: readonly string[]) => string;
|
|
13
|
+
export declare function explain(errors: readonly ErrorObject[]): Invalid[];
|
|
14
|
+
export declare function validate(name: SchemaName, value: unknown): Invalid[];
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { readdirSync, readFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { Ajv2020 } from 'ajv/dist/2020.js';
|
|
4
|
+
import { PACKAGE_ROOT } from './version.js';
|
|
5
|
+
export const SCHEMA_DIR = path.join(PACKAGE_ROOT, 'schema', 'v1');
|
|
6
|
+
export const SCHEMAS = [
|
|
7
|
+
'manifest',
|
|
8
|
+
'flow',
|
|
9
|
+
'file-scenario',
|
|
10
|
+
'announce',
|
|
11
|
+
'services',
|
|
12
|
+
'people',
|
|
13
|
+
'worlds',
|
|
14
|
+
'list',
|
|
15
|
+
'observation',
|
|
16
|
+
'comparison',
|
|
17
|
+
'drive',
|
|
18
|
+
'open',
|
|
19
|
+
'stage',
|
|
20
|
+
'try',
|
|
21
|
+
'error',
|
|
22
|
+
'dependents',
|
|
23
|
+
'check',
|
|
24
|
+
'init',
|
|
25
|
+
'validate',
|
|
26
|
+
'up',
|
|
27
|
+
'clean',
|
|
28
|
+
];
|
|
29
|
+
const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
30
|
+
let ajv = null;
|
|
31
|
+
const compiled = new Map();
|
|
32
|
+
function load() {
|
|
33
|
+
if (ajv)
|
|
34
|
+
return ajv;
|
|
35
|
+
const made = new Ajv2020({ allErrors: true, strict: false, allowUnionTypes: true });
|
|
36
|
+
made.addFormat('date-time', ISO);
|
|
37
|
+
for (const file of readdirSync(SCHEMA_DIR).filter((f) => f.endsWith('.schema.json')))
|
|
38
|
+
made.addSchema(JSON.parse(readFileSync(path.join(SCHEMA_DIR, file), 'utf8')));
|
|
39
|
+
ajv = made;
|
|
40
|
+
return made;
|
|
41
|
+
}
|
|
42
|
+
export const schemaId = (name) => `https://rehearse.kurvball.ca/schema/v1/${name}.schema.json`;
|
|
43
|
+
export function validatorOf(name) {
|
|
44
|
+
const known = compiled.get(name);
|
|
45
|
+
if (known)
|
|
46
|
+
return known;
|
|
47
|
+
const v = load().getSchema(schemaId(name));
|
|
48
|
+
if (!v)
|
|
49
|
+
throw new Error(`Rehearse has no schema named ${name}.`);
|
|
50
|
+
compiled.set(name, v);
|
|
51
|
+
return v;
|
|
52
|
+
}
|
|
53
|
+
const segments = (pointer) => pointer
|
|
54
|
+
.split('/')
|
|
55
|
+
.slice(1)
|
|
56
|
+
.map((s) => s.replace(/~1/g, '/').replace(/~0/g, '~'));
|
|
57
|
+
export const placeOf = (at) => at.length === 0
|
|
58
|
+
? 'the top'
|
|
59
|
+
: at
|
|
60
|
+
.map((s) => (/^\d+$/.test(s) ? `[${s}]` : s))
|
|
61
|
+
.join('.')
|
|
62
|
+
.replace(/\.\[/g, '[');
|
|
63
|
+
function words(e) {
|
|
64
|
+
const at = segments(e.instancePath);
|
|
65
|
+
const where = placeOf(at);
|
|
66
|
+
const p = e.params;
|
|
67
|
+
switch (e.keyword) {
|
|
68
|
+
case 'additionalProperties':
|
|
69
|
+
return `${where} has no field “${String(p.additionalProperty)}”.`;
|
|
70
|
+
case 'required':
|
|
71
|
+
return `${where} needs “${String(p.missingProperty)}”.`;
|
|
72
|
+
case 'type':
|
|
73
|
+
return `${where} should be ${String(p.type).split(',').join(' or ')}.`;
|
|
74
|
+
case 'enum':
|
|
75
|
+
return `${where} should be one of ${p.allowedValues.map((v) => JSON.stringify(v)).join(', ')}.`;
|
|
76
|
+
case 'const':
|
|
77
|
+
return `${where} should be ${JSON.stringify(p.allowedValue)}.`;
|
|
78
|
+
case 'pattern':
|
|
79
|
+
return `${where} doesn't have the form it needs (${String(p.pattern)}).`;
|
|
80
|
+
case 'propertyNames':
|
|
81
|
+
return `${where} has a name that can't be used: “${String(p.propertyName)}”.`;
|
|
82
|
+
case 'minItems':
|
|
83
|
+
return `${where} needs at least ${String(p.limit)}.`;
|
|
84
|
+
case 'minLength':
|
|
85
|
+
return `${where} can't be empty.`;
|
|
86
|
+
case 'minProperties':
|
|
87
|
+
return `${where} needs at least one field.`;
|
|
88
|
+
case 'maxProperties':
|
|
89
|
+
return `${where} takes only one of its fields.`;
|
|
90
|
+
default:
|
|
91
|
+
return `${where} ${e.message ?? 'is not valid'}.`;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const depthOf = (e) => segments(e.instancePath).length;
|
|
95
|
+
const namesAField = (e) => e.keyword === 'additionalProperties' || e.keyword === 'required';
|
|
96
|
+
const isShapeMismatch = (e) => e.keyword === 'type';
|
|
97
|
+
const worthSayingFirst = (e) => depthOf(e) * 10 + (namesAField(e) ? 5 : 0) - (isShapeMismatch(e) ? 3 : 0);
|
|
98
|
+
export function explain(errors) {
|
|
99
|
+
const useful = errors.filter((e) => !['oneOf', 'anyOf', 'if', 'not', '$ref'].includes(e.keyword));
|
|
100
|
+
const pool = useful.length ? useful : [...errors];
|
|
101
|
+
const sorted = [...pool].sort((a, b) => worthSayingFirst(b) - worthSayingFirst(a));
|
|
102
|
+
const seen = new Set();
|
|
103
|
+
const out = [];
|
|
104
|
+
for (const e of sorted) {
|
|
105
|
+
const said = words(e);
|
|
106
|
+
if (seen.has(said))
|
|
107
|
+
continue;
|
|
108
|
+
seen.add(said);
|
|
109
|
+
const p = e.params;
|
|
110
|
+
const key = e.keyword === 'additionalProperties'
|
|
111
|
+
? String(p.additionalProperty)
|
|
112
|
+
: e.keyword === 'propertyNames'
|
|
113
|
+
? String(p.propertyName)
|
|
114
|
+
: undefined;
|
|
115
|
+
out.push({ at: segments(e.instancePath), says: said, ...(key !== undefined && { key }) });
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
export function validate(name, value) {
|
|
120
|
+
const v = validatorOf(name);
|
|
121
|
+
return v(value) ? [] : explain(v.errors ?? []);
|
|
122
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface Running {
|
|
2
|
+
name: string;
|
|
3
|
+
checkout: string;
|
|
4
|
+
pid: number;
|
|
5
|
+
port: number | null;
|
|
6
|
+
url: string | null;
|
|
7
|
+
since: string;
|
|
8
|
+
down: string | null;
|
|
9
|
+
scratch: string | null;
|
|
10
|
+
standins: string[];
|
|
11
|
+
up?: string;
|
|
12
|
+
env?: Record<string, string>;
|
|
13
|
+
world?: string | null;
|
|
14
|
+
daemon?: boolean;
|
|
15
|
+
starting?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare const stateDir: (manifestDir: string) => string;
|
|
18
|
+
export declare const checkoutKey: (checkout: string) => string;
|
|
19
|
+
export declare const alive: (pid: number) => boolean;
|
|
20
|
+
export declare function remember(manifestDir: string, r: Running): void;
|
|
21
|
+
export declare function forget(manifestDir: string, name: string, checkout: string): void;
|
|
22
|
+
export declare function runningServices(manifestDir: string): Running[];
|
|
23
|
+
export declare const runningOf: (manifestDir: string, name: string, checkout: string) => Running | null;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
export const stateDir = (manifestDir) => path.join(manifestDir, '.rehearse');
|
|
5
|
+
const registryDir = (manifestDir) => path.join(stateDir(manifestDir), 'services');
|
|
6
|
+
export const checkoutKey = (checkout) => createHash('sha256').update(path.resolve(checkout)).digest('hex').slice(0, 10);
|
|
7
|
+
const fileOf = (manifestDir, name, checkout) => path.join(registryDir(manifestDir), `${name}@${checkoutKey(checkout)}.json`);
|
|
8
|
+
export const alive = (pid) => {
|
|
9
|
+
try {
|
|
10
|
+
process.kill(pid, 0);
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
return error.code === 'EPERM';
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
export function remember(manifestDir, r) {
|
|
18
|
+
mkdirSync(registryDir(manifestDir), { recursive: true });
|
|
19
|
+
writeFileSync(fileOf(manifestDir, r.name, r.checkout), `${JSON.stringify(r, null, 2)}\n`);
|
|
20
|
+
}
|
|
21
|
+
export function forget(manifestDir, name, checkout) {
|
|
22
|
+
rmSync(fileOf(manifestDir, name, checkout), { force: true });
|
|
23
|
+
}
|
|
24
|
+
const daemonOrStoppableByItsDown = (r) => !r.starting && (Boolean(r.daemon) || r.down !== null);
|
|
25
|
+
export function runningServices(manifestDir) {
|
|
26
|
+
let files;
|
|
27
|
+
try {
|
|
28
|
+
files = readdirSync(registryDir(manifestDir)).filter((f) => f.endsWith('.json'));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
const out = [];
|
|
34
|
+
for (const f of files) {
|
|
35
|
+
const file = path.join(registryDir(manifestDir), f);
|
|
36
|
+
try {
|
|
37
|
+
const r = JSON.parse(readFileSync(file, 'utf8'));
|
|
38
|
+
if (alive(r.pid) || daemonOrStoppableByItsDown(r))
|
|
39
|
+
out.push(r);
|
|
40
|
+
else
|
|
41
|
+
rmSync(file, { force: true });
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
rmSync(file, { force: true });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return out.sort((a, b) => a.name.localeCompare(b.name) || a.since.localeCompare(b.since));
|
|
48
|
+
}
|
|
49
|
+
export const runningOf = (manifestDir, name, checkout) => runningServices(manifestDir).find((r) => r.name === name && path.resolve(r.checkout) === path.resolve(checkout)) ?? null;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Manifest, type Service, type Standin } from '../manifest/index.ts';
|
|
2
|
+
import { type Running } from './registry.ts';
|
|
3
|
+
export declare function freePort(): Promise<number>;
|
|
4
|
+
export declare function inUse<T>(checkout: string, work: () => Promise<T>): Promise<T>;
|
|
5
|
+
export declare function standinsFor(m: Manifest, service: string, real: ReadonlySet<string>): {
|
|
6
|
+
standin: Standin;
|
|
7
|
+
real: boolean;
|
|
8
|
+
}[];
|
|
9
|
+
export interface UpOptions {
|
|
10
|
+
world: string | null;
|
|
11
|
+
real: ReadonlySet<string>;
|
|
12
|
+
vars: Record<string, string>;
|
|
13
|
+
progress?: (line: string) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare const logOf: (m: Manifest, name: string, checkout: string) => string;
|
|
16
|
+
export declare function liveServices(m: Manifest, checkout?: string | null): Promise<Running[]>;
|
|
17
|
+
export declare const restartable: (r: Running) => boolean;
|
|
18
|
+
export declare function restart(m: Manifest, r: Running, whileDown?: () => Promise<void>): Promise<string | null>;
|
|
19
|
+
export declare const release: (r: Running) => boolean;
|
|
20
|
+
export declare function downStartedHere(): Promise<void>;
|
|
21
|
+
export declare function bringUp(m: Manifest, s: Service, checkout: string, options: UpOptions): Promise<Running>;
|
|
22
|
+
export declare function bringDown(m: Manifest, r: Running, progress?: (line: string) => void): Promise<void>;
|
|
23
|
+
export declare function makeRoom(m: Manifest, checkout: string, progress?: (line: string) => void): Promise<void>;
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { closeSync, existsSync, mkdirSync, mkdtempSync, openSync, readFileSync, rmSync, writeFileSync, } from 'node:fs';
|
|
3
|
+
import { connect, createServer } from 'node:net';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { RehearseError } from '../errors.js';
|
|
6
|
+
import { sh, tail } from '../exec.js';
|
|
7
|
+
import { fill, fillEnv } from '../manifest/index.js';
|
|
8
|
+
import { alive, checkoutKey, forget, remember, runningServices, stateDir } from './registry.js';
|
|
9
|
+
const POLL_MS = 250;
|
|
10
|
+
const STOP_MS = 5_000;
|
|
11
|
+
const TRIES_FOR_A_PORT_NOT_YET_HANDED_OUT = 20;
|
|
12
|
+
const anyFreePort = () => new Promise((resolve, reject) => {
|
|
13
|
+
const server = createServer();
|
|
14
|
+
server.unref();
|
|
15
|
+
server.once('error', reject);
|
|
16
|
+
server.listen(0, '127.0.0.1', () => {
|
|
17
|
+
const address = server.address();
|
|
18
|
+
const port = typeof address === 'object' && address ? address.port : 0;
|
|
19
|
+
server.close(() => resolve(port));
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
const LONG_ENOUGH_FOR_ITS_SERVICE_TO_LISTEN_MS = 120_000;
|
|
23
|
+
const handedOutAt = new Map();
|
|
24
|
+
export async function freePort() {
|
|
25
|
+
const now = Date.now();
|
|
26
|
+
for (const [port, at] of handedOutAt)
|
|
27
|
+
if (now - at > LONG_ENOUGH_FOR_ITS_SERVICE_TO_LISTEN_MS)
|
|
28
|
+
handedOutAt.delete(port);
|
|
29
|
+
let port = await anyFreePort();
|
|
30
|
+
for (let i = 0; i < TRIES_FOR_A_PORT_NOT_YET_HANDED_OUT && handedOutAt.has(port); i += 1)
|
|
31
|
+
port = await anyFreePort();
|
|
32
|
+
handedOutAt.set(port, now);
|
|
33
|
+
return port;
|
|
34
|
+
}
|
|
35
|
+
const checkoutsInUseHere = new Map();
|
|
36
|
+
export async function inUse(checkout, work) {
|
|
37
|
+
const key = path.resolve(checkout);
|
|
38
|
+
checkoutsInUseHere.set(key, (checkoutsInUseHere.get(key) ?? 0) + 1);
|
|
39
|
+
try {
|
|
40
|
+
return await work();
|
|
41
|
+
}
|
|
42
|
+
finally {
|
|
43
|
+
const left = (checkoutsInUseHere.get(key) ?? 1) - 1;
|
|
44
|
+
if (left > 0)
|
|
45
|
+
checkoutsInUseHere.set(key, left);
|
|
46
|
+
else
|
|
47
|
+
checkoutsInUseHere.delete(key);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const portTaken = (port) => new Promise((resolve) => {
|
|
51
|
+
const socket = connect({ port, host: '127.0.0.1' });
|
|
52
|
+
socket.once('connect', () => {
|
|
53
|
+
socket.destroy();
|
|
54
|
+
resolve(true);
|
|
55
|
+
});
|
|
56
|
+
socket.once('error', () => resolve(false));
|
|
57
|
+
});
|
|
58
|
+
export function standinsFor(m, service, real) {
|
|
59
|
+
for (const name of real)
|
|
60
|
+
if (!m.standins[name])
|
|
61
|
+
throw new RehearseError('usage', `There is no stand-in “${name}” to replace with the real one.`, {
|
|
62
|
+
exit: 2,
|
|
63
|
+
});
|
|
64
|
+
return Object.values(m.standins)
|
|
65
|
+
.filter((s) => s.services === null || s.services.includes(service))
|
|
66
|
+
.map((standin) => {
|
|
67
|
+
const isReal = real.has(standin.name);
|
|
68
|
+
if (isReal && standin.real === null)
|
|
69
|
+
throw new RehearseError('no_real', `The stand-in ${standin.name} has no real one to choose locally: its manifest entry names no \`real\`.`);
|
|
70
|
+
return { standin, real: isReal };
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const logDir = (m) => path.join(stateDir(m.dir), 'logs');
|
|
74
|
+
export const logOf = (m, name, checkout) => path.join(logDir(m), `${name}@${checkoutKey(checkout)}.log`);
|
|
75
|
+
function signalledIfStillThere(pid, signal) {
|
|
76
|
+
try {
|
|
77
|
+
process.kill(pid, signal);
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function killGroup(pid, signal) {
|
|
85
|
+
if (!signalledIfStillThere(-pid, signal))
|
|
86
|
+
signalledIfStillThere(pid, signal);
|
|
87
|
+
}
|
|
88
|
+
async function readyNow(ready, vars, env, cwd, log, port) {
|
|
89
|
+
if (ready === null)
|
|
90
|
+
return port === null ? true : portTaken(port);
|
|
91
|
+
if ('http' in ready) {
|
|
92
|
+
try {
|
|
93
|
+
const r = await fetch(fill(ready.http, vars), {
|
|
94
|
+
signal: AbortSignal.timeout(2_000),
|
|
95
|
+
redirect: 'manual',
|
|
96
|
+
});
|
|
97
|
+
await r.body?.cancel();
|
|
98
|
+
return r.status < 500;
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if ('command' in ready)
|
|
105
|
+
return (await sh(fill(ready.command, vars), { cwd, env, timeoutMs: 15_000 })).code === 0;
|
|
106
|
+
try {
|
|
107
|
+
return new RegExp(ready.log).test(readFileSync(log, 'utf8'));
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
export async function liveServices(m, checkout = null) {
|
|
114
|
+
const out = [];
|
|
115
|
+
for (const r of runningServices(m.dir)) {
|
|
116
|
+
if (checkout !== null && path.resolve(r.checkout) !== path.resolve(checkout))
|
|
117
|
+
continue;
|
|
118
|
+
if (alive(r.pid)) {
|
|
119
|
+
out.push(r);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const s = m.services[r.name];
|
|
123
|
+
const vars = readyVarsOf(m, r);
|
|
124
|
+
const answers = s?.ready
|
|
125
|
+
? await readyNow(s.ready, vars, r.env ?? {}, s.cwd ? path.resolve(r.checkout, s.cwd) : r.checkout, '', r.port)
|
|
126
|
+
: r.port !== null
|
|
127
|
+
? await portTaken(r.port)
|
|
128
|
+
: false;
|
|
129
|
+
if (answers)
|
|
130
|
+
out.push(r);
|
|
131
|
+
else
|
|
132
|
+
forget(m.dir, r.name, r.checkout);
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
export const restartable = (r) => !!(r.up && r.down && r.daemon);
|
|
137
|
+
function readyVarsOf(m, r) {
|
|
138
|
+
return {
|
|
139
|
+
checkout: r.checkout,
|
|
140
|
+
manifest: m.dir,
|
|
141
|
+
...(r.scratch && { scratch: r.scratch }),
|
|
142
|
+
...(r.world && { world: r.world }),
|
|
143
|
+
...(r.port !== null && { port: String(r.port) }),
|
|
144
|
+
...(r.url && { url: r.url }),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
export async function restart(m, r, whileDown = async () => undefined) {
|
|
148
|
+
const s = m.services[r.name];
|
|
149
|
+
if (!s || !restartable(r))
|
|
150
|
+
return null;
|
|
151
|
+
const cwd = s.cwd ? path.resolve(r.checkout, s.cwd) : r.checkout;
|
|
152
|
+
const env = r.env ?? {};
|
|
153
|
+
await sh(r.down, { cwd, env, timeoutMs: 60_000 });
|
|
154
|
+
await whileDown();
|
|
155
|
+
const up = await sh(r.up, { cwd, env, timeoutMs: s.timeoutMs });
|
|
156
|
+
const upSaidThisTime = logOf(m, s.name, r.checkout);
|
|
157
|
+
writeFileSync(upSaidThisTime, `${up.stdout}${up.stderr}`);
|
|
158
|
+
if (up.code !== 0)
|
|
159
|
+
return `${s.name} didn't come up again (exit ${up.code}): ${tail(up.stderr || up.stdout)}`;
|
|
160
|
+
const vars = readyVarsOf(m, r);
|
|
161
|
+
const begun = Date.now();
|
|
162
|
+
while (!(await readyNow(s.ready, vars, env, cwd, upSaidThisTime, r.port))) {
|
|
163
|
+
if (Date.now() - begun > s.timeoutMs)
|
|
164
|
+
return `${s.name} wasn't ready again after ${Math.round(s.timeoutMs / 1000)} seconds.`;
|
|
165
|
+
await new Promise((resolve) => setTimeout(resolve, POLL_MS));
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
const startedHereNotHandedOver = new Map();
|
|
170
|
+
const keyOf = (r) => `${r.name}@${path.resolve(r.checkout)}`;
|
|
171
|
+
export const release = (r) => startedHereNotHandedOver.delete(keyOf(r));
|
|
172
|
+
export async function downStartedHere() {
|
|
173
|
+
for (const { m, r } of [...startedHereNotHandedOver.values()].toReversed())
|
|
174
|
+
await bringDown(m, r).catch(() => undefined);
|
|
175
|
+
}
|
|
176
|
+
export async function bringUp(m, s, checkout, options) {
|
|
177
|
+
const already = runningServices(m.dir).find((r) => r.name === s.name && path.resolve(r.checkout) === path.resolve(checkout));
|
|
178
|
+
if (already) {
|
|
179
|
+
if (alive(already.pid) || !s.ready)
|
|
180
|
+
return already;
|
|
181
|
+
if (await readyNow(s.ready, {
|
|
182
|
+
...(already.url && { url: already.url }),
|
|
183
|
+
...(already.port !== null && { port: String(already.port) }),
|
|
184
|
+
}, already.env ?? {}, s.cwd ? path.resolve(checkout, s.cwd) : checkout, '', already.port))
|
|
185
|
+
return already;
|
|
186
|
+
forget(m.dir, already.name, already.checkout);
|
|
187
|
+
}
|
|
188
|
+
let port = null;
|
|
189
|
+
if (s.port === 'auto')
|
|
190
|
+
port = await freePort();
|
|
191
|
+
else if (typeof s.port === 'number') {
|
|
192
|
+
port = s.port;
|
|
193
|
+
if (await portTaken(port))
|
|
194
|
+
throw new RehearseError('service', `${s.name} listens on port ${port}, and something already answers there. Stop it, or give the service port: auto.`);
|
|
195
|
+
}
|
|
196
|
+
const standins = standinsFor(m, s.name, options.real);
|
|
197
|
+
const cwd = s.cwd ? path.resolve(checkout, s.cwd) : checkout;
|
|
198
|
+
if (!existsSync(cwd))
|
|
199
|
+
throw new RehearseError('service', `${s.name} runs in ${cwd}, which isn't there.`);
|
|
200
|
+
mkdirSync(path.join(stateDir(m.dir), 'scratch'), { recursive: true });
|
|
201
|
+
const scratch = mkdtempSync(path.join(stateDir(m.dir), 'scratch', `${s.name}-${checkoutKey(checkout)}-`));
|
|
202
|
+
const vars = {
|
|
203
|
+
...options.vars,
|
|
204
|
+
checkout,
|
|
205
|
+
manifest: m.dir,
|
|
206
|
+
scratch,
|
|
207
|
+
...(options.world !== null && { world: options.world }),
|
|
208
|
+
...(port !== null && { port: String(port) }),
|
|
209
|
+
};
|
|
210
|
+
const url = s.url === null ? null : fill(s.url, vars);
|
|
211
|
+
if (url !== null)
|
|
212
|
+
vars.url = url;
|
|
213
|
+
const env = {
|
|
214
|
+
...fillEnv(s.env, vars),
|
|
215
|
+
...Object.assign({}, ...standins.map(({ standin, real }) => fillEnv(real ? standin.real.env : standin.env, vars))),
|
|
216
|
+
...(s.isolate && { [s.isolate.env]: scratch }),
|
|
217
|
+
...(port !== null && { PORT: String(port) }),
|
|
218
|
+
REHEARSE_SCRATCH: scratch,
|
|
219
|
+
};
|
|
220
|
+
mkdirSync(logDir(m), { recursive: true });
|
|
221
|
+
const log = logOf(m, s.name, checkout);
|
|
222
|
+
const out = openSync(log, 'w');
|
|
223
|
+
const command = fill(s.up, vars);
|
|
224
|
+
options.progress?.(`Bringing ${s.name} up in ${checkout}: ${command}`);
|
|
225
|
+
const child = spawn('/bin/sh', ['-c', command], {
|
|
226
|
+
cwd,
|
|
227
|
+
env: { ...process.env, ...env },
|
|
228
|
+
stdio: ['ignore', out, out],
|
|
229
|
+
detached: true,
|
|
230
|
+
});
|
|
231
|
+
closeSync(out);
|
|
232
|
+
let exited;
|
|
233
|
+
child.on('exit', (code) => (exited = code));
|
|
234
|
+
child.on('error', () => (exited = 127));
|
|
235
|
+
child.unref();
|
|
236
|
+
const pid = child.pid;
|
|
237
|
+
if (pid === undefined) {
|
|
238
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
239
|
+
throw new RehearseError('service', `${s.name} didn't start: \`${command}\`.`);
|
|
240
|
+
}
|
|
241
|
+
const running = {
|
|
242
|
+
name: s.name,
|
|
243
|
+
checkout,
|
|
244
|
+
pid,
|
|
245
|
+
port,
|
|
246
|
+
url,
|
|
247
|
+
since: new Date().toISOString(),
|
|
248
|
+
down: s.down === null ? null : fill(s.down, vars),
|
|
249
|
+
scratch,
|
|
250
|
+
standins: standins.map(({ standin, real }) => (real ? `${standin.name}:real` : standin.name)),
|
|
251
|
+
up: command,
|
|
252
|
+
env,
|
|
253
|
+
world: options.world,
|
|
254
|
+
starting: true,
|
|
255
|
+
};
|
|
256
|
+
remember(m.dir, running);
|
|
257
|
+
startedHereNotHandedOver.set(keyOf(running), { m, r: running });
|
|
258
|
+
const begun = Date.now();
|
|
259
|
+
const said = () => tail(readFileSync(log, 'utf8'), 4);
|
|
260
|
+
const giveUp = async (why) => {
|
|
261
|
+
await bringDown(m, running);
|
|
262
|
+
throw new RehearseError('service', why);
|
|
263
|
+
};
|
|
264
|
+
for (;;) {
|
|
265
|
+
if (exited !== undefined && exited !== 0)
|
|
266
|
+
return giveUp(`${s.name} stopped before it was ready (exit ${exited}): ${said() || 'it said nothing'}. Its log: ${log}`);
|
|
267
|
+
if (await readyNow(s.ready, vars, env, cwd, log, port))
|
|
268
|
+
break;
|
|
269
|
+
if (Date.now() - begun > s.timeoutMs)
|
|
270
|
+
return giveUp(`${s.name} wasn't ready after ${Math.round(s.timeoutMs / 1000)} seconds. ${said() ? `It last said: ${said()}. ` : ''}Its log: ${log}`);
|
|
271
|
+
await new Promise((resolve) => setTimeout(resolve, POLL_MS));
|
|
272
|
+
}
|
|
273
|
+
running.starting = false;
|
|
274
|
+
running.daemon = exited === 0;
|
|
275
|
+
remember(m.dir, running);
|
|
276
|
+
options.progress?.(`${s.name} is ready${url ? ` at ${url}` : ''} (${Date.now() - begun} ms).`);
|
|
277
|
+
return running;
|
|
278
|
+
}
|
|
279
|
+
export async function bringDown(m, r, progress) {
|
|
280
|
+
if (r.down) {
|
|
281
|
+
const s = m.services[r.name];
|
|
282
|
+
const cwd = s?.cwd ? path.resolve(r.checkout, s.cwd) : r.checkout;
|
|
283
|
+
await sh(r.down, {
|
|
284
|
+
cwd,
|
|
285
|
+
timeoutMs: 30_000,
|
|
286
|
+
env: r.env ?? (r.scratch && s?.isolate ? { [s.isolate.env]: r.scratch } : {}),
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
killGroup(r.pid, 'SIGTERM');
|
|
290
|
+
const begun = Date.now();
|
|
291
|
+
while (alive(r.pid) && Date.now() - begun < STOP_MS)
|
|
292
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
293
|
+
if (alive(r.pid))
|
|
294
|
+
killGroup(r.pid, 'SIGKILL');
|
|
295
|
+
if (r.scratch)
|
|
296
|
+
rmSync(r.scratch, { recursive: true, force: true });
|
|
297
|
+
forget(m.dir, r.name, r.checkout);
|
|
298
|
+
startedHereNotHandedOver.delete(keyOf(r));
|
|
299
|
+
progress?.(`${r.name} is down (${r.checkout}).`);
|
|
300
|
+
}
|
|
301
|
+
export async function makeRoom(m, checkout, progress) {
|
|
302
|
+
const others = runningServices(m.dir).filter((r) => path.resolve(r.checkout) !== path.resolve(checkout));
|
|
303
|
+
const sets = [...new Set(others.map((r) => path.resolve(r.checkout)))];
|
|
304
|
+
const over = sets.length + 1 - m.budget.services;
|
|
305
|
+
if (over <= 0)
|
|
306
|
+
return;
|
|
307
|
+
const notInUseHere = sets.filter((set) => !checkoutsInUseHere.has(set));
|
|
308
|
+
for (const set of notInUseHere.slice(0, over)) {
|
|
309
|
+
progress?.(`The budget allows ${m.budget.services} set${m.budget.services === 1 ? '' : 's'} of services at once: stopping those in ${set}.`);
|
|
310
|
+
for (const r of others.filter((x) => path.resolve(x.checkout) === set))
|
|
311
|
+
await bringDown(m, r, progress);
|
|
312
|
+
}
|
|
313
|
+
}
|