@kurvball/rehearse 1.13.3 → 1.15.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/dist/cli.js +5 -0
- package/dist/commands/announce.js +2 -2
- package/dist/commands/clean.js +6 -3
- package/dist/commands/compare.js +6 -1
- package/dist/commands/drive.js +12 -3
- package/dist/commands/index.js +2 -0
- package/dist/commands/init.js +1 -1
- package/dist/commands/keys.d.ts +2 -0
- package/dist/commands/keys.js +67 -0
- package/dist/commands/run.js +8 -4
- package/dist/commands/services.d.ts +3 -0
- package/dist/commands/services.js +10 -1
- package/dist/commands/stage.js +18 -2
- package/dist/commands/up.js +11 -3
- package/dist/compare/compare.d.ts +2 -0
- package/dist/compare/compare.js +19 -10
- package/dist/detect/detect.js +4 -0
- package/dist/detect/index.d.ts +1 -0
- package/dist/detect/index.js +1 -0
- package/dist/detect/keys.d.ts +16 -0
- package/dist/detect/keys.js +141 -0
- package/dist/detect/write.d.ts +2 -0
- package/dist/detect/write.js +20 -1
- package/dist/drive/daemon.js +2 -1
- package/dist/drive/open.js +6 -1
- package/dist/errors.js +2 -1
- package/dist/git/checkout.d.ts +6 -0
- package/dist/git/checkout.js +28 -19
- package/dist/git/index.d.ts +3 -2
- package/dist/git/index.js +3 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/keys/index.d.ts +3 -0
- package/dist/keys/index.js +3 -0
- package/dist/keys/keys.d.ts +16 -0
- package/dist/keys/keys.js +139 -0
- package/dist/keys/report.d.ts +23 -0
- package/dist/keys/report.js +31 -0
- package/dist/keys/store.d.ts +7 -0
- package/dist/keys/store.js +133 -0
- package/dist/keys/tool.d.ts +11 -0
- package/dist/keys/tool.js +27 -0
- package/dist/kinds/flow/kind.js +2 -2
- package/dist/manifest/index.d.ts +1 -1
- package/dist/manifest/load.js +33 -0
- package/dist/manifest/types.d.ts +28 -0
- package/dist/repositories/copies.d.ts +16 -0
- package/dist/repositories/copies.js +132 -0
- package/dist/repositories/index.d.ts +1 -0
- package/dist/repositories/index.js +1 -0
- package/dist/run/cache.d.ts +2 -1
- package/dist/run/cache.js +3 -1
- package/dist/run/index.d.ts +1 -1
- package/dist/run/index.js +1 -0
- package/dist/run/observation.d.ts +4 -0
- package/dist/run/observation.js +5 -1
- package/dist/run/session.d.ts +3 -1
- package/dist/run/session.js +27 -13
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +1 -0
- package/dist/services/index.d.ts +2 -2
- package/dist/services/index.js +2 -2
- package/dist/services/registry.d.ts +1 -0
- package/dist/services/service.d.ts +4 -0
- package/dist/services/service.js +46 -14
- package/dist/stage/api.d.ts +2 -0
- package/dist/stage/api.js +4 -0
- package/dist/stage/http.js +2 -1
- package/dist/stage/sending.d.ts +2 -1
- package/dist/stage/sending.js +8 -2
- package/dist/stage/server.d.ts +2 -0
- package/dist/stage/server.js +14 -1
- package/llms.txt +7 -6
- package/package.json +2 -2
- package/schema/v1/common.schema.json +22 -0
- package/schema/v1/init.schema.json +2 -1
- package/schema/v1/keys.schema.json +51 -0
- package/schema/v1/manifest.schema.json +66 -0
- package/schema/v1/samples/keys.json +22 -0
- package/schema/v1/samples/manifest.yaml +12 -0
- package/schema/v1/services.schema.json +13 -0
- package/skill/rehearse/SKILL.md +8 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { RehearseError } from '../errors.js';
|
|
2
|
+
import { ask, firstLine } from './tool.js';
|
|
3
|
+
export const KEPT_UNDER = 'rehearse';
|
|
4
|
+
const CREDENTIALS = `
|
|
5
|
+
$ErrorActionPreference = 'Stop'
|
|
6
|
+
[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding $false
|
|
7
|
+
[Console]::InputEncoding = New-Object System.Text.UTF8Encoding $false
|
|
8
|
+
Add-Type -TypeDefinition @'
|
|
9
|
+
using System;
|
|
10
|
+
using System.Runtime.InteropServices;
|
|
11
|
+
using System.Text;
|
|
12
|
+
public static class RehearseKeys {
|
|
13
|
+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
14
|
+
public struct Credential {
|
|
15
|
+
public int Flags; public int Type; public string TargetName; public string Comment;
|
|
16
|
+
public System.Runtime.InteropServices.ComTypes.FILETIME LastWritten;
|
|
17
|
+
public int CredentialBlobSize; public IntPtr CredentialBlob; public int Persist;
|
|
18
|
+
public int AttributeCount; public IntPtr Attributes; public string TargetAlias; public string UserName;
|
|
19
|
+
}
|
|
20
|
+
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
21
|
+
static extern bool CredRead(string target, int type, int flags, out IntPtr found);
|
|
22
|
+
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
|
23
|
+
static extern bool CredWrite(ref Credential credential, int flags);
|
|
24
|
+
[DllImport("advapi32.dll")]
|
|
25
|
+
static extern void CredFree(IntPtr found);
|
|
26
|
+
public static string Read(string target) {
|
|
27
|
+
IntPtr found;
|
|
28
|
+
if (!CredRead(target, 1, 0, out found)) {
|
|
29
|
+
int why = Marshal.GetLastWin32Error();
|
|
30
|
+
if (why == 1168) return null;
|
|
31
|
+
throw new System.ComponentModel.Win32Exception(why);
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
var c = (Credential)Marshal.PtrToStructure(found, typeof(Credential));
|
|
35
|
+
return Marshal.PtrToStringUni(c.CredentialBlob, c.CredentialBlobSize / 2);
|
|
36
|
+
} finally { CredFree(found); }
|
|
37
|
+
}
|
|
38
|
+
public static void Keep(string target, string value) {
|
|
39
|
+
var bytes = Encoding.Unicode.GetBytes(value);
|
|
40
|
+
var c = new Credential();
|
|
41
|
+
c.Type = 1; c.TargetName = target; c.UserName = "${KEPT_UNDER}"; c.Persist = 2;
|
|
42
|
+
c.CredentialBlobSize = bytes.Length;
|
|
43
|
+
c.CredentialBlob = Marshal.AllocHGlobal(bytes.Length);
|
|
44
|
+
try {
|
|
45
|
+
Marshal.Copy(bytes, 0, c.CredentialBlob, bytes.Length);
|
|
46
|
+
if (!CredWrite(ref c, 0)) throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
|
|
47
|
+
} finally { Marshal.FreeHGlobal(c.CredentialBlob); }
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
'@
|
|
51
|
+
`;
|
|
52
|
+
const READ_CREDENTIAL = `${CREDENTIALS}
|
|
53
|
+
$found = [RehearseKeys]::Read($env:REHEARSE_KEY_TARGET)
|
|
54
|
+
if ($null -eq $found) { exit 44 }
|
|
55
|
+
[Console]::Out.Write($found)
|
|
56
|
+
`;
|
|
57
|
+
const KEEP_CREDENTIAL = `${CREDENTIALS}
|
|
58
|
+
[RehearseKeys]::Keep($env:REHEARSE_KEY_TARGET, [Console]::In.ReadToEnd())
|
|
59
|
+
`;
|
|
60
|
+
function unreadable(store, tool, a) {
|
|
61
|
+
throw new RehearseError('key', a.missing
|
|
62
|
+
? `Keys kept on this computer are read from ${store} through \`${tool}\`, which isn't installed here.`
|
|
63
|
+
: `${store} didn't answer through \`${tool}\`: ${firstLine(a.err) || `exit ${a.code}`}.`);
|
|
64
|
+
}
|
|
65
|
+
const withoutOneNewline = (text) => text.replace(/\r?\n$/, '');
|
|
66
|
+
const keychain = {
|
|
67
|
+
called: "the Mac's keychain",
|
|
68
|
+
async read(name) {
|
|
69
|
+
const a = await ask('security', ['find-generic-password', '-s', KEPT_UNDER, '-a', name, '-w']);
|
|
70
|
+
if (a.code === 0)
|
|
71
|
+
return withoutOneNewline(a.out);
|
|
72
|
+
if (a.code === 44)
|
|
73
|
+
return null;
|
|
74
|
+
return unreadable(this.called, 'security', a);
|
|
75
|
+
},
|
|
76
|
+
async keep(name, value) {
|
|
77
|
+
const a = await ask('security', [
|
|
78
|
+
'add-generic-password',
|
|
79
|
+
'-U',
|
|
80
|
+
'-s',
|
|
81
|
+
KEPT_UNDER,
|
|
82
|
+
'-a',
|
|
83
|
+
name,
|
|
84
|
+
'-l',
|
|
85
|
+
`Rehearse: ${name}`,
|
|
86
|
+
'-w',
|
|
87
|
+
value,
|
|
88
|
+
]);
|
|
89
|
+
if (a.code !== 0)
|
|
90
|
+
unreadable(this.called, 'security', a);
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
const secretService = {
|
|
94
|
+
called: 'the Secret Service',
|
|
95
|
+
async read(name) {
|
|
96
|
+
const a = await ask('secret-tool', ['lookup', 'service', KEPT_UNDER, 'account', name]);
|
|
97
|
+
if (a.code === 0)
|
|
98
|
+
return withoutOneNewline(a.out);
|
|
99
|
+
if (a.code === 1 && !a.err.trim())
|
|
100
|
+
return null;
|
|
101
|
+
return unreadable(this.called, 'secret-tool', a);
|
|
102
|
+
},
|
|
103
|
+
async keep(name, value) {
|
|
104
|
+
const a = await ask('secret-tool', ['store', `--label=Rehearse: ${name}`, 'service', KEPT_UNDER, 'account', name], { input: value });
|
|
105
|
+
if (a.code !== 0)
|
|
106
|
+
unreadable(this.called, 'secret-tool', a);
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
const powershell = (script, name, input) => ask('powershell.exe', [
|
|
110
|
+
'-NoProfile',
|
|
111
|
+
'-NonInteractive',
|
|
112
|
+
'-OutputFormat',
|
|
113
|
+
'Text',
|
|
114
|
+
'-EncodedCommand',
|
|
115
|
+
Buffer.from(script, 'utf16le').toString('base64'),
|
|
116
|
+
], { ...(input !== undefined && { input }), env: { REHEARSE_KEY_TARGET: `${KEPT_UNDER}:${name}` } });
|
|
117
|
+
const credentialManager = {
|
|
118
|
+
called: 'Credential Manager',
|
|
119
|
+
async read(name) {
|
|
120
|
+
const a = await powershell(READ_CREDENTIAL, name);
|
|
121
|
+
if (a.code === 0)
|
|
122
|
+
return a.out;
|
|
123
|
+
if (a.code === 44)
|
|
124
|
+
return null;
|
|
125
|
+
return unreadable(this.called, 'powershell.exe', a);
|
|
126
|
+
},
|
|
127
|
+
async keep(name, value) {
|
|
128
|
+
const a = await powershell(KEEP_CREDENTIAL, name, value);
|
|
129
|
+
if (a.code !== 0)
|
|
130
|
+
unreadable(this.called, 'powershell.exe', a);
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
export const storeOf = (platform = process.platform) => platform === 'darwin' ? keychain : platform === 'win32' ? credentialManager : secretService;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface Answer {
|
|
2
|
+
code: number | null;
|
|
3
|
+
out: string;
|
|
4
|
+
err: string;
|
|
5
|
+
missing: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function ask(command: string, args: readonly string[], o?: {
|
|
8
|
+
input?: string;
|
|
9
|
+
env?: Record<string, string>;
|
|
10
|
+
}): Promise<Answer>;
|
|
11
|
+
export declare const firstLine: (text: string) => string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
const LONG_ENOUGH_TO_UNLOCK_MS = 120_000;
|
|
3
|
+
export function ask(command, args, o = {}) {
|
|
4
|
+
return new Promise((resolve) => {
|
|
5
|
+
const child = spawn(command, args, {
|
|
6
|
+
env: { ...process.env, ...o.env },
|
|
7
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
8
|
+
windowsHide: true,
|
|
9
|
+
});
|
|
10
|
+
let out = '';
|
|
11
|
+
let err = '';
|
|
12
|
+
const timer = setTimeout(() => child.kill('SIGKILL'), LONG_ENOUGH_TO_UNLOCK_MS);
|
|
13
|
+
child.stdout.setEncoding('utf8').on('data', (d) => (out += d));
|
|
14
|
+
child.stderr.setEncoding('utf8').on('data', (d) => (err += d));
|
|
15
|
+
child.on('error', (error) => {
|
|
16
|
+
clearTimeout(timer);
|
|
17
|
+
resolve({ code: null, out: '', err: error.message, missing: error.code === 'ENOENT' });
|
|
18
|
+
});
|
|
19
|
+
child.on('close', (code) => {
|
|
20
|
+
clearTimeout(timer);
|
|
21
|
+
resolve({ code, out, err, missing: false });
|
|
22
|
+
});
|
|
23
|
+
child.stdin.on('error', () => undefined);
|
|
24
|
+
child.stdin.end(o.input ?? '');
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
export const firstLine = (text) => text.trim().split('\n')[0]?.trim() ?? '';
|
package/dist/kinds/flow/kind.js
CHANGED
|
@@ -10,7 +10,7 @@ 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 { envOf, handoverSettled, restart, restartable, stateDir, } from '../../services/index.js';
|
|
13
|
+
import { cwdOf, 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) => {
|
|
@@ -95,7 +95,7 @@ async function throughDriver(s, ctx) {
|
|
|
95
95
|
const m = ctx.manifest;
|
|
96
96
|
const svc = m.services[s.service];
|
|
97
97
|
const running = ctx.running.find((r) => r.name === svc.name);
|
|
98
|
-
const cwd = svc
|
|
98
|
+
const cwd = cwdOf(svc, running);
|
|
99
99
|
const env = envOf(m, running);
|
|
100
100
|
const drive = async (argv) => {
|
|
101
101
|
const ran = await sh(`${svc.drive} ${argv.map(quote).join(' ')} --json`, { cwd, env, timeoutMs: 60_000 });
|
package/dist/manifest/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { fill, fillEnv, placeholdersIn } from './fill.ts';
|
|
2
2
|
export { STARTER, STARTER_BLOCKS, STARTER_HEAD } from './init.ts';
|
|
3
3
|
export { findManifest, hashOf, INERT_BY_DEFAULT, loadManifest, MANIFEST_FILE, manifestFrom } from './load.ts';
|
|
4
|
-
export { KIND_BLOCK, KINDS, type Kind, type Manifest, type PathEntry, type Person, type Real, type Service, type Standin, type World, } from './types.ts';
|
|
4
|
+
export { KIND_BLOCK, KINDS, type Key, type Kind, type Manifest, type PathEntry, type Person, type Real, type Repository, type Service, type Standin, type World, } from './types.ts';
|
|
5
5
|
export { readYaml } from './yaml.ts';
|
package/dist/manifest/load.js
CHANGED
|
@@ -74,8 +74,24 @@ function serviceOf(name, s) {
|
|
|
74
74
|
env: strings(s.env),
|
|
75
75
|
isolate: s.isolate ?? null,
|
|
76
76
|
drive: s.drive ?? null,
|
|
77
|
+
keys: s.keys ?? {},
|
|
78
|
+
repository: s.repository ?? null,
|
|
77
79
|
};
|
|
78
80
|
}
|
|
81
|
+
const AN_ADDRESS = /^(?:[a-z][a-z0-9+.-]*:\/\/|[\w.-]+@[\w.-]+:|[\w-]+(?:\.[\w-]+)+:)/i;
|
|
82
|
+
function repositoryOf(dir, name, r) {
|
|
83
|
+
const from = AN_ADDRESS.test(r.from)
|
|
84
|
+
? r.from
|
|
85
|
+
: path.resolve(dir, r.from.replace(/^~(?=\/)/, process.env.HOME ?? '~'));
|
|
86
|
+
return { name, from, base: r.base ?? null, prepare: r.prepare ?? null };
|
|
87
|
+
}
|
|
88
|
+
function keyOf(name, k) {
|
|
89
|
+
if (k === null)
|
|
90
|
+
return { name, from: null, find: null };
|
|
91
|
+
if (typeof k === 'string')
|
|
92
|
+
return { name, from: k, find: null };
|
|
93
|
+
return { name, from: k.from ?? null, find: k.find ?? null };
|
|
94
|
+
}
|
|
79
95
|
const fromOf = (glob) => glob.startsWith('./') || glob.startsWith('../') ? 'manifest' : 'checkout';
|
|
80
96
|
function pathsOf(raw) {
|
|
81
97
|
return (raw ?? []).map((p) => typeof p === 'string'
|
|
@@ -142,6 +158,21 @@ function checkWhatTheSchemaCant(m) {
|
|
|
142
158
|
};
|
|
143
159
|
defaults('person', Object.values(m.people), 'people');
|
|
144
160
|
defaults('world', Object.values(m.worlds), 'worlds');
|
|
161
|
+
for (const s of Object.values(m.services))
|
|
162
|
+
if (s.repository !== null)
|
|
163
|
+
named('The repository', s.repository, Object.keys(m.repositories), ['services', s.name, 'repository']);
|
|
164
|
+
for (const s of Object.values(m.services))
|
|
165
|
+
for (const [env, key] of Object.entries(s.keys)) {
|
|
166
|
+
named('The key', key, Object.keys(m.keys), ['services', s.name, 'keys', env]);
|
|
167
|
+
const alsoFrom = env in s.env
|
|
168
|
+
? 'its env'
|
|
169
|
+
: env === 'PORT' || env === 'REHEARSE_SCRATCH' || env === s.isolate?.env
|
|
170
|
+
? 'Rehearse'
|
|
171
|
+
: Object.values(m.standins).find((x) => (x.services === null || x.services.includes(s.name)) &&
|
|
172
|
+
(env in x.env || env in (x.real?.env ?? {})))?.name;
|
|
173
|
+
if (alsoFrom)
|
|
174
|
+
refuse(m.file, at(['services', s.name, 'keys', env]), `${s.name} gets ${env} from both ${alsoFrom.includes(' ') || alsoFrom === 'Rehearse' ? alsoFrom : `the stand-in ${alsoFrom}`} and its keys: give it one of them.`);
|
|
175
|
+
}
|
|
145
176
|
for (const [i, rule] of [...m.setAside.keys, ...m.setAside.values].entries()) {
|
|
146
177
|
try {
|
|
147
178
|
void new RegExp(rule);
|
|
@@ -228,6 +259,8 @@ export function manifestFrom(text, file) {
|
|
|
228
259
|
},
|
|
229
260
|
affected: { graph: raw.affected?.graph ?? null, inert: raw.affected?.inert ?? [...INERT_BY_DEFAULT] },
|
|
230
261
|
budget: { services: raw.budget?.services ?? 1, checkouts: raw.budget?.checkouts ?? 3 },
|
|
262
|
+
keys: Object.fromEntries(Object.entries(raw.keys ?? {}).map(([n, k]) => [n, keyOf(n, k)])),
|
|
263
|
+
repositories: Object.fromEntries(Object.entries(raw.repositories ?? {}).map(([n, r]) => [n, repositoryOf(dir, n, r)])),
|
|
231
264
|
lineOf: (at, key) => read.lineOf(at, key),
|
|
232
265
|
};
|
|
233
266
|
checkWhatTheSchemaCant(m);
|
package/dist/manifest/types.d.ts
CHANGED
|
@@ -46,6 +46,28 @@ export interface RawManifest {
|
|
|
46
46
|
services?: number;
|
|
47
47
|
checkouts?: number;
|
|
48
48
|
};
|
|
49
|
+
keys?: Record<string, RawKey>;
|
|
50
|
+
repositories?: Record<string, RawRepository>;
|
|
51
|
+
}
|
|
52
|
+
export interface RawRepository {
|
|
53
|
+
from: string;
|
|
54
|
+
base?: string;
|
|
55
|
+
prepare?: string;
|
|
56
|
+
}
|
|
57
|
+
export interface Repository {
|
|
58
|
+
name: string;
|
|
59
|
+
from: string;
|
|
60
|
+
base: string | null;
|
|
61
|
+
prepare: string | null;
|
|
62
|
+
}
|
|
63
|
+
export type RawKey = string | null | {
|
|
64
|
+
from?: string;
|
|
65
|
+
find?: string;
|
|
66
|
+
};
|
|
67
|
+
export interface Key {
|
|
68
|
+
name: string;
|
|
69
|
+
from: string | null;
|
|
70
|
+
find: string | null;
|
|
49
71
|
}
|
|
50
72
|
export type Env = Record<string, string | number | boolean>;
|
|
51
73
|
export type RawReady = string | {
|
|
@@ -67,6 +89,8 @@ export interface RawService {
|
|
|
67
89
|
env: string;
|
|
68
90
|
};
|
|
69
91
|
drive?: string;
|
|
92
|
+
keys?: Record<string, string>;
|
|
93
|
+
repository?: string;
|
|
70
94
|
}
|
|
71
95
|
export type Ready = {
|
|
72
96
|
http: string;
|
|
@@ -90,6 +114,8 @@ export interface Service {
|
|
|
90
114
|
env: string;
|
|
91
115
|
} | null;
|
|
92
116
|
drive: string | null;
|
|
117
|
+
keys: Record<string, string>;
|
|
118
|
+
repository: string | null;
|
|
93
119
|
}
|
|
94
120
|
export type Real = 'local' | 'stand-in' | 'real';
|
|
95
121
|
export interface Step {
|
|
@@ -254,5 +280,7 @@ export interface Manifest {
|
|
|
254
280
|
services: number;
|
|
255
281
|
checkouts: number;
|
|
256
282
|
};
|
|
283
|
+
keys: Record<string, Key>;
|
|
284
|
+
repositories: Record<string, Repository>;
|
|
257
285
|
lineOf(at: readonly string[], key?: string): number | undefined;
|
|
258
286
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Manifest } from '../manifest/index.ts';
|
|
2
|
+
export interface Copy {
|
|
3
|
+
path: string;
|
|
4
|
+
ref: string | null;
|
|
5
|
+
commit: string | null;
|
|
6
|
+
dirty: boolean;
|
|
7
|
+
}
|
|
8
|
+
export type Copies = Record<string, Copy>;
|
|
9
|
+
export declare const repositoriesDir: (m: Manifest) => string;
|
|
10
|
+
export declare function copyOf(m: Manifest, name: string, given: string | null, forSet: string, progress: (line: string) => void, cwd?: string): Promise<Copy>;
|
|
11
|
+
export declare function evictCopies(m: Manifest, name: string): string[];
|
|
12
|
+
export declare function refsOf(given: readonly string[], flag: string): Record<string, string>;
|
|
13
|
+
export declare function refuseUnknownRepositories(m: Manifest, given: Readonly<Record<string, string>>): void;
|
|
14
|
+
export declare function copiesFor(m: Manifest, given: Readonly<Record<string, string>>, forSet: string, progress: (line: string) => void, cwd?: string): Promise<Copies>;
|
|
15
|
+
export declare const pinnedOf: (copies: Readonly<Copies>) => Record<string, string>;
|
|
16
|
+
export declare const rootsOf: (copies: Readonly<Copies> | undefined) => Record<string, string>;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, renameSync, rmSync, statSync, utimesSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { RehearseError, errorText, usage } from '../errors.js';
|
|
4
|
+
import { commitOf, folderNamed, git, gitOr, isDirty, preparedMark, preparedOnce, topOf, } from '../git/index.js';
|
|
5
|
+
import { checkoutKey, runningServices, stateDir } from '../services/index.js';
|
|
6
|
+
export const repositoriesDir = (m) => path.join(stateDir(m.dir), 'repositories');
|
|
7
|
+
const cloneOf = (m, name) => path.join(repositoriesDir(m), `${name}.git`);
|
|
8
|
+
const copiesOf = (m, name) => path.join(repositoriesDir(m), name);
|
|
9
|
+
const WITH_CREDENTIALS = /(?<=:\/\/)[^/@\s]+@/g;
|
|
10
|
+
const withoutCredentials = (text) => text.replace(WITH_CREDENTIALS, '');
|
|
11
|
+
const usedHere = new Set();
|
|
12
|
+
function cloned(m, r, progress) {
|
|
13
|
+
const clone = cloneOf(m, r.name);
|
|
14
|
+
const from = withoutCredentials(r.from);
|
|
15
|
+
if (existsSync(clone)) {
|
|
16
|
+
if (gitOr(clone, ['config', '--get', 'remote.origin.url']) !== r.from) {
|
|
17
|
+
progress(`${r.name} now comes from ${from}.`);
|
|
18
|
+
git(clone, ['remote', 'set-url', 'origin', r.from]);
|
|
19
|
+
}
|
|
20
|
+
const fetched = gitOr(clone, [
|
|
21
|
+
'fetch',
|
|
22
|
+
'--quiet',
|
|
23
|
+
'--prune',
|
|
24
|
+
'origin',
|
|
25
|
+
'+refs/heads/*:refs/heads/*',
|
|
26
|
+
'+refs/tags/*:refs/tags/*',
|
|
27
|
+
]);
|
|
28
|
+
if (fetched === null)
|
|
29
|
+
progress(`Rehearse couldn't fetch ${r.name} from ${from}, so it uses what it fetched before.`);
|
|
30
|
+
return clone;
|
|
31
|
+
}
|
|
32
|
+
mkdirSync(repositoriesDir(m), { recursive: true });
|
|
33
|
+
progress(`Cloning ${r.name} from ${from}`);
|
|
34
|
+
const into = `${clone}.${process.pid}.cloning`;
|
|
35
|
+
rmSync(into, { recursive: true, force: true });
|
|
36
|
+
try {
|
|
37
|
+
git(repositoriesDir(m), ['clone', '--bare', '--quiet', r.from, into]);
|
|
38
|
+
renameSync(into, clone);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
rmSync(into, { recursive: true, force: true });
|
|
42
|
+
if (!existsSync(clone))
|
|
43
|
+
throw new RehearseError('git', withoutCredentials(`Rehearse can't clone ${r.name} from ${r.from}: ${errorText(error)}`));
|
|
44
|
+
}
|
|
45
|
+
return clone;
|
|
46
|
+
}
|
|
47
|
+
const baseOf = (clone, r) => r.base ?? gitOr(clone, ['symbolic-ref', '--short', 'HEAD']) ?? 'main';
|
|
48
|
+
const dirtyIn = (folder) => {
|
|
49
|
+
const status = gitOr(folder, ['status', '--porcelain', '--untracked-files=all']);
|
|
50
|
+
return status === null || status !== '';
|
|
51
|
+
};
|
|
52
|
+
export async function copyOf(m, name, given, forSet, progress, cwd = process.cwd()) {
|
|
53
|
+
const r = m.repositories[name];
|
|
54
|
+
if (!r)
|
|
55
|
+
throw new RehearseError('usage', `There is no repository “${name}” in the manifest.`, { exit: 2 });
|
|
56
|
+
const folder = given === null ? null : folderNamed(given, cwd);
|
|
57
|
+
if (folder !== null)
|
|
58
|
+
return { path: folder, ref: null, commit: commitOf(folder), dirty: dirtyIn(folder) };
|
|
59
|
+
const clone = cloned(m, r, progress);
|
|
60
|
+
const ref = given ?? baseOf(clone, r);
|
|
61
|
+
const commit = commitOf(clone, ref);
|
|
62
|
+
if (commit === null)
|
|
63
|
+
throw new RehearseError('git', `${ref} isn't a branch, tag or commit of ${name} (${withoutCredentials(r.from)}).${r.base === null && given === null ? ' Say its base in rehearse.yaml.' : ''}`);
|
|
64
|
+
const dir = path.join(copiesOf(m, name), `${commit.slice(0, 12)}@${checkoutKey(forSet)}`);
|
|
65
|
+
if (!existsSync(dir)) {
|
|
66
|
+
mkdirSync(copiesOf(m, name), { recursive: true });
|
|
67
|
+
progress(`Checking out ${name} at ${ref} (${commit.slice(0, 12)}) into ${dir}`);
|
|
68
|
+
gitOr(clone, ['worktree', 'prune']);
|
|
69
|
+
git(clone, ['worktree', 'add', '--detach', dir, commit]);
|
|
70
|
+
}
|
|
71
|
+
await preparedOnce(dir, { prepare: r.prepare, seed: m.seed, given: `${name} at ${ref}` }, progress);
|
|
72
|
+
const now = new Date();
|
|
73
|
+
utimesSync(dir, now, now);
|
|
74
|
+
usedHere.add(path.resolve(dir));
|
|
75
|
+
evictCopies(m, name);
|
|
76
|
+
return { path: dir, ref, commit, dirty: isDirty(dir) };
|
|
77
|
+
}
|
|
78
|
+
export function evictCopies(m, name) {
|
|
79
|
+
const root = copiesOf(m, name);
|
|
80
|
+
if (!existsSync(root))
|
|
81
|
+
return [];
|
|
82
|
+
const dirs = readdirSync(root, { withFileTypes: true })
|
|
83
|
+
.filter((d) => d.isDirectory())
|
|
84
|
+
.map((d) => path.join(root, d.name))
|
|
85
|
+
.sort((a, b) => statSync(b).mtimeMs - statSync(a).mtimeMs);
|
|
86
|
+
const gone = [];
|
|
87
|
+
const clone = cloneOf(m, name);
|
|
88
|
+
const inUse = [
|
|
89
|
+
...usedHere,
|
|
90
|
+
...runningServices(m.dir).flatMap((r) => (r.root === undefined ? [] : [r.root])),
|
|
91
|
+
];
|
|
92
|
+
for (const dir of dirs.slice(m.budget.checkouts * 2)) {
|
|
93
|
+
if (inUse.some((k) => path.resolve(k) === path.resolve(dir)))
|
|
94
|
+
continue;
|
|
95
|
+
if (existsSync(clone))
|
|
96
|
+
gitOr(clone, ['worktree', 'remove', '--force', dir]);
|
|
97
|
+
rmSync(dir, { recursive: true, force: true });
|
|
98
|
+
rmSync(preparedMark(dir), { force: true });
|
|
99
|
+
gone.push(dir);
|
|
100
|
+
}
|
|
101
|
+
if (existsSync(clone))
|
|
102
|
+
gitOr(clone, ['worktree', 'prune']);
|
|
103
|
+
return gone;
|
|
104
|
+
}
|
|
105
|
+
const A_REPOSITORY_NAMED = /^([A-Za-z_][A-Za-z0-9_-]*)=(.+)$/;
|
|
106
|
+
export function refsOf(given, flag) {
|
|
107
|
+
const refs = {};
|
|
108
|
+
for (const value of given) {
|
|
109
|
+
const named = A_REPOSITORY_NAMED.exec(value);
|
|
110
|
+
if (!named)
|
|
111
|
+
throw usage(`--${flag} takes a repository's name, = and a ref or folder, as in api=feature-x: not “${value}”.`);
|
|
112
|
+
const [, name = '', ref = ''] = named;
|
|
113
|
+
if (name in refs)
|
|
114
|
+
throw usage(`--${flag} names ${name} twice.`);
|
|
115
|
+
refs[name] = ref;
|
|
116
|
+
}
|
|
117
|
+
return refs;
|
|
118
|
+
}
|
|
119
|
+
export function refuseUnknownRepositories(m, given) {
|
|
120
|
+
for (const name of Object.keys(given))
|
|
121
|
+
if (!(name in m.repositories))
|
|
122
|
+
throw usage(`There is no repository “${name}”${Object.keys(m.repositories).length ? `: the manifest names ${Object.keys(m.repositories).join(', ')}` : ' in the manifest'}.`);
|
|
123
|
+
}
|
|
124
|
+
export async function copiesFor(m, given, forSet, progress, cwd = process.cwd()) {
|
|
125
|
+
refuseUnknownRepositories(m, given);
|
|
126
|
+
const out = {};
|
|
127
|
+
for (const name of Object.keys(m.repositories))
|
|
128
|
+
out[name] = await copyOf(m, name, given[name] ?? null, topOf(forSet) ?? forSet, progress, cwd);
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
export const pinnedOf = (copies) => Object.fromEntries(Object.entries(copies).map(([name, c]) => [name, c.ref === null ? c.path : (c.commit ?? c.path)]));
|
|
132
|
+
export const rootsOf = (copies) => Object.fromEntries(Object.entries(copies ?? {}).map(([name, c]) => [name, c.path]));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { copiesFor, copyOf, evictCopies, pinnedOf, refsOf, refuseUnknownRepositories, repositoriesDir, rootsOf, type Copies, type Copy, } from './copies.ts';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { copiesFor, copyOf, evictCopies, pinnedOf, refsOf, refuseUnknownRepositories, repositoriesDir, rootsOf, } from './copies.js';
|
package/dist/run/cache.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import type { KindRunner } from '../kinds/index.ts';
|
|
2
2
|
import { type Manifest } from '../manifest/index.ts';
|
|
3
3
|
import type { Scenario } from '../scenarios/index.ts';
|
|
4
|
-
import type
|
|
4
|
+
import { type Observation } from './observation.ts';
|
|
5
5
|
export declare function hashOfContents(at: string): string;
|
|
6
6
|
export declare function manifestPartsUsedBy(m: Manifest, services: readonly string[], world: string | null, kind: string): unknown;
|
|
7
7
|
export declare function groupsOf(m: Manifest, scenarios: readonly Scenario[], world: string | null): Promise<Map<string, string>>;
|
|
8
8
|
export interface KeyParts {
|
|
9
9
|
group?: string | null;
|
|
10
10
|
commit: string;
|
|
11
|
+
copies?: Readonly<Record<string, string>> | undefined;
|
|
11
12
|
scenario: Scenario;
|
|
12
13
|
world: string | null;
|
|
13
14
|
person: string | null;
|
package/dist/run/cache.js
CHANGED
|
@@ -4,6 +4,7 @@ import path from 'node:path';
|
|
|
4
4
|
import { hashOf } from '../manifest/index.js';
|
|
5
5
|
import { stateDir } from '../services/index.js';
|
|
6
6
|
import { chainOf } from '../worlds/index.js';
|
|
7
|
+
import { dirtyAnywhere } from './observation.js';
|
|
7
8
|
export function hashOfContents(at) {
|
|
8
9
|
const hash = createHash('sha256');
|
|
9
10
|
const walk = (p, rel) => {
|
|
@@ -62,6 +63,7 @@ export function cacheKeyOf(p) {
|
|
|
62
63
|
const services = p.runner.services(p.scenario, p.manifest);
|
|
63
64
|
return hashOf({
|
|
64
65
|
commit: p.commit,
|
|
66
|
+
...(p.copies && { copies: p.copies }),
|
|
65
67
|
id: p.scenario.id,
|
|
66
68
|
definition: p.scenario.definition,
|
|
67
69
|
context: p.scenario.context ?? null,
|
|
@@ -87,7 +89,7 @@ export function cached(m, commit, key) {
|
|
|
87
89
|
}
|
|
88
90
|
const didntAnswer = (o) => o.failures.some((f) => typeof f.got === 'string' && /^It didn’t answer|timed out/.test(f.got));
|
|
89
91
|
export function keep(m, o) {
|
|
90
|
-
if (!o.cacheKey || !o.checkout.commit || o.checkout
|
|
92
|
+
if (!o.cacheKey || !o.checkout.commit || dirtyAnywhere(o.checkout) || o.broke)
|
|
91
93
|
return false;
|
|
92
94
|
if (didntAnswer(o))
|
|
93
95
|
return false;
|
package/dist/run/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { cached, cacheKeyOf, groupsOf, keep, type KeyParts } from './cache.ts';
|
|
2
|
-
export type
|
|
2
|
+
export { commitsOf, dirtyAnywhere, withCopies, type CheckoutInfo, type Observation } from './observation.ts';
|
|
3
3
|
export { slotsFor, type Slots } from './parallel.ts';
|
|
4
4
|
export { applyWorld, placedIn, runSide, runSideSharing, type NotRun, type SessionOptions, type SideRun, } from './session.ts';
|
|
5
5
|
export { baseVars } from './vars.ts';
|
package/dist/run/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { cached, cacheKeyOf, groupsOf, keep } from './cache.js';
|
|
2
|
+
export { commitsOf, dirtyAnywhere, withCopies } from './observation.js';
|
|
2
3
|
export { slotsFor } from './parallel.js';
|
|
3
4
|
export { applyWorld, placedIn, runSide, runSideSharing, } from './session.js';
|
|
4
5
|
export { baseVars } from './vars.js';
|
|
@@ -5,7 +5,11 @@ export interface CheckoutInfo {
|
|
|
5
5
|
ref: string | null;
|
|
6
6
|
commit: string | null;
|
|
7
7
|
dirty: boolean;
|
|
8
|
+
repositories?: Record<string, CheckoutInfo>;
|
|
8
9
|
}
|
|
10
|
+
export declare const withCopies: (info: CheckoutInfo, copies: Readonly<Record<string, CheckoutInfo>> | undefined) => CheckoutInfo;
|
|
11
|
+
export declare const dirtyAnywhere: (info: CheckoutInfo) => boolean;
|
|
12
|
+
export declare const commitsOf: (info: CheckoutInfo) => Record<string, string> | undefined;
|
|
9
13
|
export interface Observation {
|
|
10
14
|
v: 1;
|
|
11
15
|
ok: boolean;
|
package/dist/run/observation.js
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export const withCopies = (info, copies) => (copies && Object.keys(copies).length ? { ...info, repositories: { ...copies } } : info);
|
|
2
|
+
export const dirtyAnywhere = (info) => info.dirty || Object.values(info.repositories ?? {}).some((c) => c.dirty || c.commit === null);
|
|
3
|
+
export const commitsOf = (info) => info.repositories
|
|
4
|
+
? Object.fromEntries(Object.entries(info.repositories).map(([name, c]) => [name, c.commit ?? '']))
|
|
5
|
+
: undefined;
|
package/dist/run/session.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { type Scenario } from '../scenarios/index.ts';
|
|
2
2
|
import { type StandinUse } from '../kinds/index.ts';
|
|
3
3
|
import { type Manifest } from '../manifest/index.ts';
|
|
4
|
+
import { type Copies } from '../repositories/index.ts';
|
|
4
5
|
import { applyWorld } from '../worlds/index.ts';
|
|
5
6
|
export { applyWorld };
|
|
6
|
-
import type
|
|
7
|
+
import { type CheckoutInfo, type Observation } from './observation.ts';
|
|
7
8
|
import { type Slots } from './parallel.ts';
|
|
8
9
|
export interface SessionOptions {
|
|
9
10
|
world: string | null;
|
|
@@ -14,6 +15,7 @@ export interface SessionOptions {
|
|
|
14
15
|
ref: string | null;
|
|
15
16
|
groups?: ReadonlyMap<string, string>;
|
|
16
17
|
parallel?: number;
|
|
18
|
+
copies?: Copies;
|
|
17
19
|
}
|
|
18
20
|
export interface NotRun {
|
|
19
21
|
scenario: Scenario;
|