@kurvball/ashlar 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.
Files changed (132) hide show
  1. package/CHANGELOG.md +238 -0
  2. package/LICENSE +21 -0
  3. package/README.md +145 -2
  4. package/dist/affected.d.ts +19 -0
  5. package/dist/affected.js +78 -0
  6. package/dist/areas.d.ts +2 -0
  7. package/dist/areas.js +20 -0
  8. package/dist/baseline.d.ts +27 -0
  9. package/dist/baseline.js +109 -0
  10. package/dist/beside.d.ts +1 -0
  11. package/dist/beside.js +4 -0
  12. package/dist/check.d.ts +39 -0
  13. package/dist/check.js +109 -0
  14. package/dist/cli.d.ts +2 -0
  15. package/dist/cli.js +211 -0
  16. package/dist/config.d.ts +84 -0
  17. package/dist/config.js +98 -0
  18. package/dist/configCheck.d.ts +2 -0
  19. package/dist/configCheck.js +204 -0
  20. package/dist/css.d.ts +13 -0
  21. package/dist/css.js +66 -0
  22. package/dist/drive.d.ts +77 -0
  23. package/dist/drive.js +188 -0
  24. package/dist/features.d.ts +32 -0
  25. package/dist/features.js +156 -0
  26. package/dist/git.d.ts +18 -0
  27. package/dist/git.js +140 -0
  28. package/dist/glob.d.ts +5 -0
  29. package/dist/glob.js +49 -0
  30. package/dist/graph.d.ts +16 -0
  31. package/dist/graph.js +83 -0
  32. package/dist/identity.d.ts +1 -0
  33. package/dist/identity.js +1 -0
  34. package/dist/index.d.ts +21 -0
  35. package/dist/index.js +18 -0
  36. package/dist/jsonc.d.ts +1 -0
  37. package/dist/jsonc.js +66 -0
  38. package/dist/knowledge/areas.d.ts +25 -0
  39. package/dist/knowledge/areas.js +86 -0
  40. package/dist/knowledge/decisions.d.ts +14 -0
  41. package/dist/knowledge/decisions.js +53 -0
  42. package/dist/knowledge/guards.d.ts +4 -0
  43. package/dist/knowledge/guards.js +60 -0
  44. package/dist/knowledge/rules.d.ts +12 -0
  45. package/dist/knowledge/rules.js +100 -0
  46. package/dist/knowledge/secrets.d.ts +6 -0
  47. package/dist/knowledge/secrets.js +58 -0
  48. package/dist/layout.d.ts +61 -0
  49. package/dist/layout.js +120 -0
  50. package/dist/ledger.d.ts +28 -0
  51. package/dist/ledger.js +44 -0
  52. package/dist/lint.d.ts +11 -0
  53. package/dist/lint.js +109 -0
  54. package/dist/map.d.ts +25 -0
  55. package/dist/map.js +37 -0
  56. package/dist/move.d.ts +18 -0
  57. package/dist/move.js +94 -0
  58. package/dist/onboard/applies.d.ts +11 -0
  59. package/dist/onboard/applies.js +28 -0
  60. package/dist/onboard/brief.d.ts +40 -0
  61. package/dist/onboard/brief.js +126 -0
  62. package/dist/onboard/guide.d.ts +7 -0
  63. package/dist/onboard/guide.js +150 -0
  64. package/dist/onboard/model.d.ts +15 -0
  65. package/dist/onboard/model.js +57 -0
  66. package/dist/onboard/onboard.d.ts +28 -0
  67. package/dist/onboard/onboard.js +86 -0
  68. package/dist/onboard/questions.d.ts +15 -0
  69. package/dist/onboard/questions.js +90 -0
  70. package/dist/onboard/snapshot.d.ts +28 -0
  71. package/dist/onboard/snapshot.js +58 -0
  72. package/dist/order.d.ts +1 -0
  73. package/dist/order.js +1 -0
  74. package/dist/origins.d.ts +6 -0
  75. package/dist/origins.js +51 -0
  76. package/dist/plugin/boundaries.d.ts +5 -0
  77. package/dist/plugin/boundaries.js +174 -0
  78. package/dist/plugin/exports.d.ts +2 -0
  79. package/dist/plugin/exports.js +41 -0
  80. package/dist/plugin/features.d.ts +4 -0
  81. package/dist/plugin/features.js +34 -0
  82. package/dist/plugin/here.d.ts +13 -0
  83. package/dist/plugin/here.js +69 -0
  84. package/dist/plugin/imports.d.ts +12 -0
  85. package/dist/plugin/imports.js +103 -0
  86. package/dist/plugin/index.d.ts +14 -0
  87. package/dist/plugin/index.js +19 -0
  88. package/dist/plugin/messages.d.ts +20 -0
  89. package/dist/plugin/messages.js +36 -0
  90. package/dist/plugin/names.d.ts +3 -0
  91. package/dist/plugin/names.js +68 -0
  92. package/dist/plugin/references.d.ts +5 -0
  93. package/dist/plugin/references.js +52 -0
  94. package/dist/plugin/text.d.ts +7 -0
  95. package/dist/plugin/text.js +87 -0
  96. package/dist/plugin/types.d.ts +87 -0
  97. package/dist/plugin/types.js +1 -0
  98. package/dist/reach.d.ts +82 -0
  99. package/dist/reach.js +163 -0
  100. package/dist/reachDrive.d.ts +31 -0
  101. package/dist/reachDrive.js +200 -0
  102. package/dist/reachReader.d.ts +1 -0
  103. package/dist/reachReader.js +31 -0
  104. package/dist/rehearse/dependents.d.ts +9 -0
  105. package/dist/rehearse/dependents.js +38 -0
  106. package/dist/rehearse/fill.d.ts +28 -0
  107. package/dist/rehearse/fill.js +44 -0
  108. package/dist/report.d.ts +6 -0
  109. package/dist/report.js +91 -0
  110. package/dist/resolve.d.ts +3 -0
  111. package/dist/resolve.js +114 -0
  112. package/dist/runner.d.ts +6 -0
  113. package/dist/runner.js +29 -0
  114. package/dist/skill.d.ts +23 -0
  115. package/dist/skill.js +235 -0
  116. package/dist/usage.d.ts +1 -0
  117. package/dist/usage.js +53 -0
  118. package/dist/workspace.d.ts +16 -0
  119. package/dist/workspace.js +125 -0
  120. package/ledger.json +201 -0
  121. package/package.json +73 -3
  122. package/schema/affected.v1.json +58 -0
  123. package/schema/brief.v1.json +205 -0
  124. package/schema/check.v1.json +151 -0
  125. package/schema/dependents.v1.json +30 -0
  126. package/schema/drive.v1.json +55 -0
  127. package/schema/ledger.v1.json +61 -0
  128. package/schema/map.v1.json +62 -0
  129. package/schema/move.v1.json +61 -0
  130. package/schema/onboard.v1.json +186 -0
  131. package/schema/rehearse.v1.json +91 -0
  132. package/schema/snapshot.v1.json +101 -0
@@ -0,0 +1,87 @@
1
+ import type { AppLayout } from '../layout.ts';
2
+ import type { Workspace } from '../workspace.ts';
3
+ export interface Position {
4
+ line: number;
5
+ column: number;
6
+ }
7
+ export interface Loc {
8
+ start: Position;
9
+ end: Position;
10
+ }
11
+ export type Node = {
12
+ type: string;
13
+ loc: Loc;
14
+ parent?: Node;
15
+ } & Record<string, any>;
16
+ export interface Comment {
17
+ type: string;
18
+ value: string;
19
+ loc: Loc;
20
+ }
21
+ export interface Scope {
22
+ type: string;
23
+ set: Map<string, unknown>;
24
+ upper: Scope | null;
25
+ }
26
+ export interface SourceCode {
27
+ text: string;
28
+ lines: string[];
29
+ getAllComments(): Comment[];
30
+ getScope(node: Node): Scope;
31
+ }
32
+ export interface Context {
33
+ filename: string;
34
+ settings: Record<string, unknown>;
35
+ sourceCode: SourceCode;
36
+ report(descriptor: {
37
+ loc: Loc;
38
+ message: string;
39
+ }): void;
40
+ }
41
+ export type Visitor = Record<string, (node: Node) => void>;
42
+ export interface Rule {
43
+ meta: {
44
+ type: 'problem';
45
+ docs: {
46
+ description: string;
47
+ };
48
+ };
49
+ create(context: Context): Visitor;
50
+ }
51
+ export interface Confined {
52
+ words: string[];
53
+ only: string[];
54
+ within: string[];
55
+ message: string;
56
+ }
57
+ export interface Settings {
58
+ root: string;
59
+ apps: AppLayout[];
60
+ maxLines: number;
61
+ tests: string[];
62
+ confined: Confined[];
63
+ messages?: Record<string, string>;
64
+ workspace: Workspace;
65
+ record: string | null;
66
+ }
67
+ export interface Break {
68
+ rule: string;
69
+ line: number;
70
+ column: number;
71
+ size: number;
72
+ message: string;
73
+ }
74
+ export interface Edge {
75
+ to: string;
76
+ type: boolean;
77
+ takes?: string[] | null;
78
+ passes?: [string, string][];
79
+ }
80
+ export interface FileFindings {
81
+ breaks: Break[];
82
+ edges: Edge[];
83
+ exports?: string[];
84
+ }
85
+ export interface Findings {
86
+ files: Record<string, FileFindings>;
87
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,82 @@
1
+ export declare const REACH_FILE = "reach.ts";
2
+ export declare const DRIVE_ROLES: readonly ['alertdialog', 'article', 'button', 'checkbox', 'combobox', 'complementary', 'dialog', 'grid', 'gridcell', 'group', 'heading', 'link', 'list', 'listitem', 'main', 'menu', 'menuitem', 'menuitemcheckbox', 'menuitemradio', 'navigation', 'option', 'radio', 'region', 'row', 'searchbox', 'status', 'switch', 'tab', 'tablist', 'tabpanel', 'textbox', 'toolbar', 'tree', 'treeitem'];
3
+ export type DriveRole = (typeof DRIVE_ROLES)[number];
4
+ export declare const isDriveRole: (v: unknown) => v is DriveRole;
5
+ export declare const NAMED_KEYS: readonly ['Enter', 'Escape', 'Tab', 'Backspace', 'Delete', 'Space', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Home', 'End', 'PageUp', 'PageDown'];
6
+ export declare const MODIFIERS: readonly ['ControlOrMeta', 'Meta', 'Control', 'Alt', 'Shift'];
7
+ export declare const isDriveKey: (v: unknown) => v is string;
8
+ export interface ClickStep {
9
+ readonly click: string;
10
+ readonly role?: DriveRole;
11
+ readonly type?: never;
12
+ readonly key?: never;
13
+ readonly snapshot?: never;
14
+ }
15
+ export interface TypeStep {
16
+ readonly type: string;
17
+ readonly click?: never;
18
+ readonly role?: never;
19
+ readonly key?: never;
20
+ readonly snapshot?: never;
21
+ }
22
+ export interface KeyStep {
23
+ readonly key: string;
24
+ readonly click?: never;
25
+ readonly role?: never;
26
+ readonly type?: never;
27
+ readonly snapshot?: never;
28
+ }
29
+ export interface SnapshotStep {
30
+ readonly snapshot: string;
31
+ readonly role?: DriveRole;
32
+ readonly click?: never;
33
+ readonly type?: never;
34
+ readonly key?: never;
35
+ }
36
+ export type ReachAction = ClickStep | TypeStep | KeyStep;
37
+ export type ReachStep = ReachAction | SnapshotStep;
38
+ export type ReachSteps = readonly [...ReachAction[], SnapshotStep];
39
+ export interface ReachScript {
40
+ readonly name: string;
41
+ readonly steps: ReachSteps;
42
+ }
43
+ export declare const REACH_LIMITS: {
44
+ readonly name: 60;
45
+ readonly steps: 40;
46
+ readonly words: 120;
47
+ };
48
+ export declare const REACH_REFUSALS: readonly ['not_a_script', 'no_name', 'long_name', 'no_steps', 'too_many_steps', 'bad_step', 'bad_key', 'bad_role', 'not_ending_on_snapshot', 'snapshot_midway'];
49
+ export type ReachRefusal = (typeof REACH_REFUSALS)[number];
50
+ export type ReachRead = {
51
+ ok: true;
52
+ script: ReachScript;
53
+ } | {
54
+ ok: false;
55
+ refusal: ReachRefusal;
56
+ step: number | null;
57
+ says: string;
58
+ };
59
+ export declare function reachScriptOf(v: unknown): ReachRead;
60
+ export declare const REACH_WORDS: {
61
+ readonly [R in ReachRefusal]: (step: number) => string;
62
+ };
63
+ export declare function stepSays(step: ReachStep): string;
64
+ export declare const REACH_SEPARATOR = " \u203A ";
65
+ export declare function reachLine(steps: readonly ReachStep[]): string;
66
+ export declare const FEATURE_REACH: {
67
+ readonly rule: 'feature-reach';
68
+ readonly counts: 'Each feature folder in the layout’s features without a reach script that reads: one per folder, counted on its `reach.ts`.';
69
+ readonly message: "Every feature folder holds a reach script, `reach.ts`: the developer's way to the feature as drive steps, ending on a snapshot of what it reaches. It is plain data that imports only types. Write one, then run it with `ashlar drive <feature>`.";
70
+ };
71
+ export declare const REACH_OUTCOMES: readonly ['reached', 'stopped', 'no_reach', 'unreadable'];
72
+ export type ReachOutcome = (typeof REACH_OUTCOMES)[number];
73
+ export interface ReachRun {
74
+ feature: string;
75
+ folder: string;
76
+ outcome: ReachOutcome;
77
+ steps: number;
78
+ stoppedAt: number | null;
79
+ step: string | null;
80
+ says: string | null;
81
+ ms: number;
82
+ }
package/dist/reach.js ADDED
@@ -0,0 +1,163 @@
1
+ export const REACH_FILE = 'reach.ts';
2
+ export const DRIVE_ROLES = [
3
+ 'alertdialog',
4
+ 'article',
5
+ 'button',
6
+ 'checkbox',
7
+ 'combobox',
8
+ 'complementary',
9
+ 'dialog',
10
+ 'grid',
11
+ 'gridcell',
12
+ 'group',
13
+ 'heading',
14
+ 'link',
15
+ 'list',
16
+ 'listitem',
17
+ 'main',
18
+ 'menu',
19
+ 'menuitem',
20
+ 'menuitemcheckbox',
21
+ 'menuitemradio',
22
+ 'navigation',
23
+ 'option',
24
+ 'radio',
25
+ 'region',
26
+ 'row',
27
+ 'searchbox',
28
+ 'status',
29
+ 'switch',
30
+ 'tab',
31
+ 'tablist',
32
+ 'tabpanel',
33
+ 'textbox',
34
+ 'toolbar',
35
+ 'tree',
36
+ 'treeitem',
37
+ ];
38
+ export const isDriveRole = (v) => typeof v === 'string' && DRIVE_ROLES.includes(v);
39
+ export const NAMED_KEYS = [
40
+ 'Enter',
41
+ 'Escape',
42
+ 'Tab',
43
+ 'Backspace',
44
+ 'Delete',
45
+ 'Space',
46
+ 'ArrowUp',
47
+ 'ArrowDown',
48
+ 'ArrowLeft',
49
+ 'ArrowRight',
50
+ 'Home',
51
+ 'End',
52
+ 'PageUp',
53
+ 'PageDown',
54
+ ];
55
+ export const MODIFIERS = ['ControlOrMeta', 'Meta', 'Control', 'Alt', 'Shift'];
56
+ const KEY = new RegExp(`^(?:(?:${MODIFIERS.join('|')})\\+)*(?:${NAMED_KEYS.join('|')}|F(?:[1-9]|1[0-2])|[a-z0-9]|[,./;'\`\\[\\]\\\\=-])$`);
57
+ export const isDriveKey = (v) => typeof v === 'string' && KEY.test(v);
58
+ export const REACH_LIMITS = { name: 60, steps: 40, words: 120 };
59
+ export const REACH_REFUSALS = [
60
+ 'not_a_script',
61
+ 'no_name',
62
+ 'long_name',
63
+ 'no_steps',
64
+ 'too_many_steps',
65
+ 'bad_step',
66
+ 'bad_key',
67
+ 'bad_role',
68
+ 'not_ending_on_snapshot',
69
+ 'snapshot_midway',
70
+ ];
71
+ const record = (v) => v && typeof v === 'object' && !Array.isArray(v) ? v : null;
72
+ const words = (v) => {
73
+ if (typeof v !== 'string')
74
+ return null;
75
+ const t = v.trim();
76
+ return t && t.length <= REACH_LIMITS.words ? t : null;
77
+ };
78
+ const VERBS = ['click', 'type', 'key', 'snapshot'];
79
+ function refused(refusal, step) {
80
+ return { ok: false, refusal, step, says: REACH_WORDS[refusal](step ?? 0) };
81
+ }
82
+ function stepOf(v, at) {
83
+ const s = record(v);
84
+ const verbs = s ? VERBS.filter((verb) => verb in s) : [];
85
+ if (!s || verbs.length !== 1)
86
+ return refused('bad_step', at);
87
+ const verb = verbs[0];
88
+ const extra = Object.keys(s).filter((k) => k !== verb && !(k === 'role' && (verb === 'click' || verb === 'snapshot')));
89
+ if (extra.length > 0)
90
+ return refused('bad_step', at);
91
+ if (s.role !== undefined && !isDriveRole(s.role))
92
+ return refused('bad_role', at);
93
+ const role = s.role;
94
+ if (verb === 'key')
95
+ return isDriveKey(s.key) ? { key: s.key } : refused('bad_key', at);
96
+ if (verb === 'type')
97
+ return typeof s.type === 'string' && s.type !== '' ? { type: s.type } : refused('bad_step', at);
98
+ const name = words(s[verb]);
99
+ if (!name)
100
+ return refused('bad_step', at);
101
+ if (verb === 'click')
102
+ return role ? { click: name, role } : { click: name };
103
+ return role ? { snapshot: name, role } : { snapshot: name };
104
+ }
105
+ export function reachScriptOf(v) {
106
+ const s = record(v);
107
+ if (!s)
108
+ return refused('not_a_script', null);
109
+ if (typeof s.name !== 'string' || !s.name.trim())
110
+ return refused('no_name', null);
111
+ const name = s.name.trim();
112
+ if (name.length > REACH_LIMITS.name)
113
+ return refused('long_name', null);
114
+ if (!Array.isArray(s.steps) || s.steps.length === 0)
115
+ return refused('no_steps', null);
116
+ if (s.steps.length > REACH_LIMITS.steps)
117
+ return refused('too_many_steps', null);
118
+ const steps = [];
119
+ for (const [i, raw] of s.steps.entries()) {
120
+ const step = stepOf(raw, i + 1);
121
+ if ('ok' in step)
122
+ return step;
123
+ steps.push(step);
124
+ }
125
+ const last = steps.at(-1);
126
+ if (last.snapshot === undefined)
127
+ return refused('not_ending_on_snapshot', steps.length);
128
+ const midway = steps.findIndex((step, i) => i < steps.length - 1 && step.snapshot !== undefined);
129
+ if (midway !== -1)
130
+ return refused('snapshot_midway', midway + 1);
131
+ return { ok: true, script: { name, steps: steps } };
132
+ }
133
+ export const REACH_WORDS = {
134
+ not_a_script: () => `A reach script's default export is an object: its name and its steps.`,
135
+ no_name: () => 'Give the reach script the name the developer and the agents say the feature by.',
136
+ long_name: () => `A feature's name is at most ${REACH_LIMITS.name} characters.`,
137
+ no_steps: () => 'Give the reach script its steps, ending on a snapshot of what it reaches.',
138
+ too_many_steps: () => `A reach is at most ${REACH_LIMITS.steps} steps.`,
139
+ bad_step: (n) => `Step ${n} isn't a drive step: one of click (with a role, if it helps), type, key or snapshot.`,
140
+ bad_key: (n) => `Step ${n} presses a key drive doesn't know: name it as Playwright does, such as Meta+k.`,
141
+ bad_role: (n) => `Step ${n} names a role drive doesn't know.`,
142
+ not_ending_on_snapshot: () => 'A reach ends on a snapshot of what it reaches, so a run proves it got there.',
143
+ snapshot_midway: (n) => `Step ${n} is a snapshot: only the last step is.`,
144
+ };
145
+ export function stepSays(step) {
146
+ if (step.click !== undefined)
147
+ return step.click;
148
+ if (step.type !== undefined)
149
+ return `“${step.type}”`;
150
+ if (step.key !== undefined)
151
+ return step.key;
152
+ return step.snapshot;
153
+ }
154
+ export const REACH_SEPARATOR = ' › ';
155
+ export function reachLine(steps) {
156
+ return steps.map(stepSays).join(REACH_SEPARATOR);
157
+ }
158
+ export const FEATURE_REACH = {
159
+ rule: 'feature-reach',
160
+ counts: 'Each feature folder in the layout’s features without a reach script that reads: one per folder, counted on its `reach.ts`.',
161
+ message: `Every feature folder holds a reach script, \`${REACH_FILE}\`: the developer's way to the feature as drive steps, ending on a snapshot of what it reaches. It is plain data that imports only types. Write one, then run it with \`ashlar drive <feature>\`.`,
162
+ };
163
+ export const REACH_OUTCOMES = ['reached', 'stopped', 'no_reach', 'unreadable'];
@@ -0,0 +1,31 @@
1
+ import { type SpawnSyncReturns } from 'node:child_process';
2
+ import { type Loaded } from './config.ts';
3
+ import { SCHEME_VARIABLE, type DriveCommand, type DriveResult } from './drive.ts';
4
+ import type { Feature } from './features.ts';
5
+ import { type ReachRun } from './reach.ts';
6
+ export declare const SCHEMES: readonly ['light', 'dark'];
7
+ export type Scheme = (typeof SCHEMES)[number];
8
+ export declare const isScheme: (v: unknown) => v is Scheme;
9
+ export { SCHEME_VARIABLE };
10
+ export declare const STEP_MS = 60000;
11
+ export interface DriveOptions {
12
+ scheme: Scheme | null;
13
+ stepMs?: number;
14
+ }
15
+ export declare const shellQuote: (arg: string) => string;
16
+ export declare const driveLine: (drive: string, args: readonly string[]) => string;
17
+ export declare function driveOf(config: Loaded): string;
18
+ export declare const OUTPUT_MB = 64;
19
+ export declare const RAN_WORDS: {
20
+ readonly tooMuch: "The drive command printed more than 64 MB, more than Ashlar reads.";
21
+ readonly couldNotRun: (why: string) => string;
22
+ };
23
+ export declare function resultOf(c: DriveCommand, run: Pick<SpawnSyncReturns<string>, 'error' | 'signal' | 'stdout' | 'stderr' | 'status'>, ms: number): DriveResult;
24
+ export declare function driveCommand(config: Loaded, c: DriveCommand, options: DriveOptions): DriveResult;
25
+ export declare const START_MS = 180000;
26
+ export declare function driveFeature(config: Loaded, feature: Feature, options: DriveOptions): ReachRun;
27
+ export declare function featureNamed(features: readonly Feature[], said: string): Feature;
28
+ export declare const RUN_WORDS: {
29
+ readonly [O in ReachRun['outcome']]: (r: ReachRun) => string;
30
+ };
31
+ export declare function runsText(runs: readonly ReachRun[], all: boolean): string;
@@ -0,0 +1,200 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import { AshlarError, CONFIG_FILE } from './config.js';
3
+ import { DRIVE_FAILURE_WORDS, DRIVE_FAILURES, driveArgv, isDriveVerb, SCHEME_VARIABLE, stepCommand, } from './drive.js';
4
+ import { REACH_FILE, stepSays } from './reach.js';
5
+ import { driveCommandOf, rehearseRun } from './runner.js';
6
+ export const SCHEMES = ['light', 'dark'];
7
+ export const isScheme = (v) => typeof v === 'string' && SCHEMES.includes(v);
8
+ export { SCHEME_VARIABLE };
9
+ export const STEP_MS = 60_000;
10
+ const BARE = /^[A-Za-z0-9_./:=@%+,-]+$/;
11
+ export const shellQuote = (arg) => BARE.test(arg) ? arg : `'${arg.replaceAll("'", `'\\''`)}'`;
12
+ export const driveLine = (drive, args) => [drive.trim(), ...args.map(shellQuote)].join(' ');
13
+ export function driveOf(config) {
14
+ const drive = driveCommandOf(config);
15
+ if (drive)
16
+ return drive;
17
+ throw new AshlarError(`Nothing drives this project's app: add Rehearse (a rehearse.yaml), and Ashlar walks each reach through \`rehearse drive\`, or name a drive command in ${CONFIG_FILE}, such as \`drive: 'pnpm drive'\`.`);
18
+ }
19
+ const record = (v) => v && typeof v === 'object' && !Array.isArray(v) ? v : null;
20
+ function printedResult(stdout) {
21
+ const lines = stdout.split('\n').map((line) => line.trim());
22
+ for (const line of lines.reverse()) {
23
+ if (!line.startsWith('{'))
24
+ continue;
25
+ try {
26
+ const parsed = record(JSON.parse(line));
27
+ if (parsed && typeof parsed.ok === 'boolean')
28
+ return parsed;
29
+ }
30
+ catch {
31
+ continue;
32
+ }
33
+ }
34
+ return null;
35
+ }
36
+ const failureOf = (v) => DRIVE_FAILURES.includes(v) ? v : 'crashed';
37
+ const nameIn = (c) => c.command === 'click' || c.command === 'snapshot' ? c.name : null;
38
+ const lastLine = (text) => `${text ?? ''}`.trim().split('\n').at(-1)?.trim() ?? '';
39
+ export const OUTPUT_MB = 64;
40
+ export const RAN_WORDS = {
41
+ tooMuch: `The drive command printed more than ${OUTPUT_MB} MB, more than Ashlar reads.`,
42
+ couldNotRun: (why) => `The drive command couldn’t run: ${why}`,
43
+ };
44
+ export function resultOf(c, run, ms) {
45
+ const code = run.error?.code;
46
+ if (run.error && code !== 'ETIMEDOUT') {
47
+ return {
48
+ ok: false,
49
+ command: c.command,
50
+ ms,
51
+ failure: 'crashed',
52
+ says: code === 'ENOBUFS'
53
+ ? RAN_WORDS.tooMuch
54
+ : RAN_WORDS.couldNotRun(run.error.message.split('\n')[0].trim()),
55
+ };
56
+ }
57
+ if (run.error || run.signal) {
58
+ return {
59
+ ok: false,
60
+ command: c.command,
61
+ ms,
62
+ failure: 'timed_out',
63
+ says: DRIVE_FAILURE_WORDS.timed_out(nameIn(c)),
64
+ };
65
+ }
66
+ const printed = printedResult(run.stdout ?? '');
67
+ if (printed?.ok === true || (!printed && run.status === 0)) {
68
+ const text = (v) => (typeof v === 'string' ? v : null);
69
+ return { ok: true, command: c.command, ms, file: text(printed?.file), tree: text(printed?.tree) };
70
+ }
71
+ const error = record(printed?.error);
72
+ const says = typeof printed?.says === 'string' ? printed.says : typeof error?.says === 'string' ? error.says : '';
73
+ const said = says.trim();
74
+ return {
75
+ ok: false,
76
+ command: isDriveVerb(printed?.command) ? printed.command : c.command,
77
+ ms,
78
+ failure: failureOf(printed?.failure),
79
+ says: said ||
80
+ lastLine(run.stderr) ||
81
+ lastLine(run.stdout) ||
82
+ `The drive command stopped with code ${run.status ?? 'none'}.`,
83
+ };
84
+ }
85
+ function spawnDrive(config, args, options) {
86
+ const line = driveLine(driveOf(config), [...args, '--json']);
87
+ const env = options.scheme ? { ...process.env, [SCHEME_VARIABLE]: options.scheme } : process.env;
88
+ const started = performance.now();
89
+ const run = spawnSync('/bin/sh', ['-c', line], {
90
+ cwd: config.root,
91
+ encoding: 'utf8',
92
+ env,
93
+ timeout: options.stepMs ?? STEP_MS,
94
+ maxBuffer: OUTPUT_MB * 1024 * 1024,
95
+ });
96
+ return { run, ms: Math.round(performance.now() - started) };
97
+ }
98
+ export function driveCommand(config, c, options) {
99
+ const { run, ms } = spawnDrive(config, driveArgv(c), options);
100
+ return resultOf(c, run, ms);
101
+ }
102
+ export const START_MS = 180_000;
103
+ function saidBy(run, verb) {
104
+ const printed = printedResult(run.stdout ?? '');
105
+ const error = record(printed?.error);
106
+ const says = typeof printed?.says === 'string' ? printed.says : typeof error?.says === 'string' ? error.says : '';
107
+ const code = run.error?.code;
108
+ if (code === 'ETIMEDOUT' || (run.signal && !says.trim()))
109
+ return `\`rehearse drive ${verb}\` didn't answer in time.`;
110
+ if (run.error)
111
+ return RAN_WORDS.couldNotRun(run.error.message.split('\n')[0].trim());
112
+ return (says.trim() ||
113
+ lastLine(run.stderr) ||
114
+ lastLine(run.stdout) ||
115
+ `\`rehearse drive ${verb}\` stopped with code ${run.status ?? 'none'}.`);
116
+ }
117
+ const answered = (run) => {
118
+ const printed = printedResult(run.stdout ?? '');
119
+ return printed !== null && record(printed.error) === null;
120
+ };
121
+ function startThroughRehearse(config, options) {
122
+ const stop = spawnDrive(config, ['stop'], options).run;
123
+ if (!answered(stop))
124
+ throw rehearseCantDrive(saidBy(stop, 'stop'));
125
+ const start = spawnDrive(config, ['start'], {
126
+ ...options,
127
+ stepMs: Math.max(options.stepMs ?? STEP_MS, START_MS),
128
+ }).run;
129
+ if (!start.error && !start.signal && printedResult(start.stdout ?? '')?.ok === true)
130
+ return;
131
+ driveCommand(config, { command: 'stop' }, options);
132
+ throw new AshlarError(`\`rehearse drive start\` couldn't start this project's app: ${saidBy(start, 'start')}`);
133
+ }
134
+ const rehearseCantDrive = (said) => new AshlarError(`This project has a rehearse.yaml, so Ashlar walks each reach through \`rehearse drive\`, and it didn't answer: ${said} Rehearse drives from its 0.3.0; until then, name a drive command in ${CONFIG_FILE}, such as \`drive: 'pnpm drive'\`.`);
135
+ export function driveFeature(config, feature, options) {
136
+ const head = { feature: feature.name, folder: feature.folder, stoppedAt: null, step: null, ms: 0 };
137
+ if (!feature.reach)
138
+ return { ...head, outcome: 'no_reach', steps: 0, says: null };
139
+ if (!feature.reach.ok)
140
+ return { ...head, outcome: 'unreadable', steps: 0, says: feature.reach.says };
141
+ driveOf(config);
142
+ const steps = feature.reach.script.steps;
143
+ if (!config.drive && rehearseRun(config.root))
144
+ startThroughRehearse(config, options);
145
+ else
146
+ driveCommand(config, { command: 'stop' }, options);
147
+ const started = performance.now();
148
+ let stopped = null;
149
+ try {
150
+ for (const [i, step] of steps.entries()) {
151
+ const result = driveCommand(config, stepCommand(step), options);
152
+ if (!result.ok) {
153
+ stopped = { at: i + 1, says: result.says };
154
+ break;
155
+ }
156
+ }
157
+ }
158
+ finally {
159
+ driveCommand(config, { command: 'stop' }, options);
160
+ }
161
+ const ms = Math.round(performance.now() - started);
162
+ if (!stopped)
163
+ return { ...head, outcome: 'reached', steps: steps.length, says: null, ms };
164
+ return {
165
+ ...head,
166
+ outcome: 'stopped',
167
+ steps: steps.length,
168
+ stoppedAt: stopped.at,
169
+ step: stepSays(steps[stopped.at - 1]),
170
+ says: stopped.says,
171
+ ms,
172
+ };
173
+ }
174
+ const keyOf = (s) => s.trim().replace(/\s+/g, ' ').toLowerCase();
175
+ export function featureNamed(features, said) {
176
+ const key = keyOf(said);
177
+ const found = features.find((f) => keyOf(f.name) === key || keyOf(f.folder) === key || keyOf(f.folder.split('/').at(-1) ?? '') === key);
178
+ if (found)
179
+ return found;
180
+ if (features.length === 0)
181
+ throw new AshlarError('There are no feature folders in the layout.');
182
+ throw new AshlarError(`No feature is named “${said.trim()}”: ${features.map((f) => f.name).join(', ')}.`);
183
+ }
184
+ const quoted = (s) => `“${s}”`;
185
+ export const RUN_WORDS = {
186
+ reached: (r) => `${r.feature}: reached in ${r.steps === 1 ? 'one step' : `${r.steps} steps`}.`,
187
+ stopped: (r) => `${r.feature}: its reach stopped at ${quoted(r.step ?? '')}, step ${r.stoppedAt} of ${r.steps}.${r.says ? ` ${r.says}` : ''}`,
188
+ no_reach: (r) => `${r.feature} has no reach script yet: write ${r.folder}/${REACH_FILE}.`,
189
+ unreadable: (r) => `${r.feature}’s reach script doesn’t read. ${r.says ?? ''}`.trim(),
190
+ };
191
+ export function runsText(runs, all) {
192
+ const lines = runs.map((r) => RUN_WORDS[r.outcome](r));
193
+ if (all) {
194
+ const reached = runs.filter((r) => r.outcome === 'reached').length;
195
+ lines.push(runs.length === 0
196
+ ? 'There are no feature folders in the layout.'
197
+ : `${reached} of ${runs.length} ${runs.length === 1 ? 'feature' : 'features'} reached.`);
198
+ }
199
+ return `${lines.join('\n')}\n`;
200
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,31 @@
1
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
2
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
+ });
6
+ }
7
+ return path;
8
+ };
9
+ import { renameSync, writeFileSync } from 'node:fs';
10
+ import { pathToFileURL } from 'node:url';
11
+ import { reachScriptOf } from './reach.js';
12
+ const firstLine = (error) => (error instanceof Error ? error.message : String(error)).split('\n')[0].trim();
13
+ async function loadOne(file) {
14
+ try {
15
+ const module = (await import(__rewriteRelativeImportExtension(pathToFileURL(file).href)));
16
+ const read = reachScriptOf(module.default);
17
+ return read.ok ? { ok: true, script: read.script } : { ok: false, says: read.says };
18
+ }
19
+ catch (error) {
20
+ return { ok: false, says: `Node couldn’t load it: ${firstLine(error)}` };
21
+ }
22
+ }
23
+ const [out, ...files] = process.argv.slice(2);
24
+ if (out) {
25
+ const loaded = [];
26
+ for (const file of files) {
27
+ loaded.push(await loadOne(file));
28
+ writeFileSync(`${out}.part`, JSON.stringify(loaded));
29
+ renameSync(`${out}.part`, out);
30
+ }
31
+ }
@@ -0,0 +1,9 @@
1
+ import type { Loaded } from '../config.ts';
2
+ import { REHEARSE_CONTRACT } from './fill.ts';
3
+ export interface Dependents {
4
+ v: typeof REHEARSE_CONTRACT;
5
+ dependents: Record<string, string[]>;
6
+ unknown: string[];
7
+ }
8
+ export declare function dependentsOf(config: Loaded, said: readonly string[], cwd?: string): Dependents;
9
+ export declare function dependentsText(found: Dependents): string;
@@ -0,0 +1,38 @@
1
+ import { relative, resolve } from 'node:path';
2
+ import { featureFolders } from '../features.js';
3
+ import { dependents as reachedFrom, graphOf } from '../graph.js';
4
+ import { tidy, within } from '../layout.js';
5
+ import { byText } from '../order.js';
6
+ import { REACH_FILE } from '../reach.js';
7
+ import { REHEARSE_CONTRACT } from './fill.js';
8
+ export function dependentsOf(config, said, cwd = process.cwd()) {
9
+ const files = [...new Set(said.map((file) => tidy(relative(config.root, resolve(cwd, file)))))].sort(byText);
10
+ const graph = graphOf(config);
11
+ const reaches = featureFolders(config)
12
+ .map((folder) => ({ folder, reach: `${folder}/${REACH_FILE}` }))
13
+ .filter(({ reach }) => graph.files.has(reach));
14
+ const out = {};
15
+ const unknown = [];
16
+ for (const file of files) {
17
+ if (!graph.files.has(file)) {
18
+ unknown.push(file);
19
+ continue;
20
+ }
21
+ const reached = reachedFrom(file, graph);
22
+ for (const { folder, reach } of reaches) {
23
+ if ([...reached].some((path) => within(path, folder)))
24
+ reached.add(reach);
25
+ }
26
+ reached.delete(file);
27
+ out[file] = [...reached].sort(byText);
28
+ }
29
+ return { v: REHEARSE_CONTRACT, dependents: out, unknown };
30
+ }
31
+ export function dependentsText(found) {
32
+ const lines = Object.entries(found.dependents).map(([file, by]) => by.length === 0
33
+ ? `${file}: nothing reaches it`
34
+ : `${file}: reached by ${by.length}\n${by.map((path) => ` ${path}`).join('\n')}`);
35
+ if (found.unknown.length > 0)
36
+ lines.push(`Not in the import graph: ${found.unknown.join(', ')}.`);
37
+ return `${lines.join('\n')}\n`;
38
+ }
@@ -0,0 +1,28 @@
1
+ import type { Loaded } from '../config.ts';
2
+ import { type ReachStep } from '../reach.ts';
3
+ export declare const REHEARSE_CONTRACT = 1;
4
+ export interface FlowEntry {
5
+ path: string;
6
+ covers: string[];
7
+ }
8
+ export interface RehearseFlow {
9
+ file: string;
10
+ flow: {
11
+ name: string;
12
+ covers: string[];
13
+ steps: ReachStep[];
14
+ };
15
+ }
16
+ export interface RehearseFill {
17
+ v: typeof REHEARSE_CONTRACT;
18
+ manifest: {
19
+ version: 1;
20
+ scenarios?: {
21
+ flows: FlowEntry[];
22
+ };
23
+ };
24
+ flows: RehearseFlow[];
25
+ missing: string[];
26
+ }
27
+ export declare function rehearseFill(config: Loaded): RehearseFill;
28
+ export declare function fillText(fill: RehearseFill): string;