@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,15 @@
1
+ import { type Loaded } from '../config.ts';
2
+ import { type Feature } from '../features.ts';
3
+ import { type Area } from '../knowledge/areas.ts';
4
+ import { type Decision } from '../knowledge/decisions.ts';
5
+ import { type FolderRule } from '../knowledge/rules.ts';
6
+ export interface Knowledge {
7
+ project: string;
8
+ decisions: Decision[];
9
+ rules: FolderRule[];
10
+ features: Feature[];
11
+ areas: Area[];
12
+ }
13
+ export declare function refuseSecrets(config: Loaded): void;
14
+ export declare function projectName(config: Loaded): string;
15
+ export declare function knowledgeOf(config: Loaded): Knowledge;
@@ -0,0 +1,57 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { AshlarError } from '../config.js';
4
+ import { featuresOf } from '../features.js';
5
+ import { areasOf } from '../knowledge/areas.js';
6
+ import { readDecisions } from '../knowledge/decisions.js';
7
+ import { readGuards } from '../knowledge/guards.js';
8
+ import { readRules } from '../knowledge/rules.js';
9
+ import { secretsIn } from '../knowledge/secrets.js';
10
+ import { walkFolders } from '../workspace.js';
11
+ function knowledgeFiles(config) {
12
+ const files = [config.onboard.decisions].filter((file) => existsSync(join(config.root, file)));
13
+ if (existsSync(join(config.root, config.onboard.rules))) {
14
+ walkFolders(config.root, [config.onboard.rules], new Set(), (file) => {
15
+ if (file.endsWith('.md'))
16
+ files.push(file);
17
+ });
18
+ }
19
+ return files.sort();
20
+ }
21
+ export function refuseSecrets(config) {
22
+ const found = knowledgeFiles(config).flatMap((file) => secretsIn(readFileSync(join(config.root, file), 'utf8')).map((at) => ` ${file}:${at.line}:${at.column} holds what looks like ${at.kind}.`));
23
+ if (found.length > 0) {
24
+ throw new AshlarError([
25
+ 'Secrets never enter knowledge files, and these lines look like one:',
26
+ ...found,
27
+ 'Take the value out. Say who grants access instead, as an `access` line, never the credential.',
28
+ ].join('\n'));
29
+ }
30
+ }
31
+ const UNNAMED = 'this project';
32
+ export function projectName(config) {
33
+ const manifest = join(config.root, 'package.json');
34
+ if (existsSync(manifest)) {
35
+ try {
36
+ const name = JSON.parse(readFileSync(manifest, 'utf8')).name;
37
+ if (typeof name === 'string' && name.trim())
38
+ return name.trim();
39
+ }
40
+ catch {
41
+ return UNNAMED;
42
+ }
43
+ }
44
+ return UNNAMED;
45
+ }
46
+ export function knowledgeOf(config) {
47
+ refuseSecrets(config);
48
+ const features = featuresOf(config);
49
+ const names = new Map(features.map((feature) => [feature.folder, feature.name]));
50
+ return {
51
+ project: projectName(config),
52
+ decisions: readDecisions(config.root, config.onboard.decisions),
53
+ rules: [...readRules(config.root, config.onboard.rules), ...readGuards(config)],
54
+ features,
55
+ areas: areasOf(config, names),
56
+ };
57
+ }
@@ -0,0 +1,28 @@
1
+ import type { Loaded } from '../config.ts';
2
+ import { type Area } from '../knowledge/areas.ts';
3
+ import type { Decision } from '../knowledge/decisions.ts';
4
+ import type { FolderRule } from '../knowledge/rules.ts';
5
+ import { type Question } from './questions.ts';
6
+ export declare const ONBOARD_VERSION = 1;
7
+ export interface OnboardArea extends Area {
8
+ owner: string | null;
9
+ page: string;
10
+ }
11
+ export interface OnboardReport {
12
+ version: typeof ONBOARD_VERSION;
13
+ project: string;
14
+ out: string;
15
+ ok: boolean;
16
+ written: string[];
17
+ stale: string[];
18
+ removed: string[];
19
+ areas: OnboardArea[];
20
+ decisions: Decision[];
21
+ rules: FolderRule[];
22
+ questions: Question[];
23
+ }
24
+ export declare function onboard(config: Loaded, options?: {
25
+ check?: boolean;
26
+ json?: boolean;
27
+ }): OnboardReport;
28
+ export declare function onboardText(report: OnboardReport, check: boolean): string;
@@ -0,0 +1,86 @@
1
+ import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
2
+ import { dirname, join } from 'node:path';
3
+ import { ownerOf } from '../knowledge/areas.js';
4
+ import { areaPage, GENERATED, guideOf } from './guide.js';
5
+ import { knowledgeOf } from './model.js';
6
+ import { questionsOf } from './questions.js';
7
+ export const ONBOARD_VERSION = 1;
8
+ function generatedIn(root, out) {
9
+ const found = [];
10
+ const visit = (folder) => {
11
+ if (!existsSync(join(root, out, folder)))
12
+ return;
13
+ for (const entry of readdirSync(join(root, out, folder), { withFileTypes: true })) {
14
+ const path = folder ? `${folder}/${entry.name}` : entry.name;
15
+ if (entry.isDirectory())
16
+ visit(path);
17
+ else if (entry.name.endsWith('.md') && readFileSync(join(root, out, path), 'utf8').includes(GENERATED))
18
+ found.push(path);
19
+ }
20
+ };
21
+ visit('');
22
+ return found.sort();
23
+ }
24
+ export function onboard(config, options = {}) {
25
+ const knowledge = knowledgeOf(config);
26
+ const questions = questionsOf(config, knowledge);
27
+ const files = guideOf(config, knowledge, questions);
28
+ const out = config.onboard.out;
29
+ const stale = [];
30
+ const written = [];
31
+ for (const [path, text] of files) {
32
+ const file = join(config.root, out, path);
33
+ if (existsSync(file) && readFileSync(file, 'utf8') === text)
34
+ continue;
35
+ stale.push(path);
36
+ if (options.check)
37
+ continue;
38
+ mkdirSync(dirname(file), { recursive: true });
39
+ writeFileSync(file, text);
40
+ written.push(path);
41
+ }
42
+ const removed = generatedIn(config.root, out).filter((path) => !files.has(path));
43
+ if (!options.check)
44
+ for (const path of removed)
45
+ rmSync(join(config.root, out, path));
46
+ return {
47
+ version: ONBOARD_VERSION,
48
+ project: knowledge.project,
49
+ out,
50
+ ok: !options.check || (stale.length === 0 && removed.length === 0),
51
+ written,
52
+ stale: options.check ? stale : [],
53
+ removed,
54
+ areas: knowledge.areas.map((area) => ({
55
+ ...area,
56
+ owner: ownerOf(config, knowledge.decisions, area.path)?.handle ?? null,
57
+ page: `${out}/${areaPage(area)}`,
58
+ })),
59
+ decisions: knowledge.decisions.filter((decision) => decision.kind !== 'access'),
60
+ rules: knowledge.rules,
61
+ questions,
62
+ };
63
+ }
64
+ const count = (n, one) => `${n} ${one}${n === 1 ? '' : 's'}`;
65
+ export function onboardText(report, check) {
66
+ const asked = `${report.questions.length} open ${report.questions.length === 1 ? 'question' : 'questions'}`;
67
+ if (check) {
68
+ if (report.ok)
69
+ return `The guide in ${report.out} is current: ${asked}.\n`;
70
+ const lines = [
71
+ ...report.stale.map((path) => ` ${report.out}/${path} is stale or was edited by hand.`),
72
+ ...report.removed.map((path) => ` ${report.out}/${path} is no longer generated.`),
73
+ ];
74
+ return `The guide in ${report.out} is out of date:\n${lines.join('\n')}\nRun \`ashlar onboard\` to write it again.\n`;
75
+ }
76
+ const changed = report.written.length + report.removed.length;
77
+ const owners = [...new Set(report.questions.map((q) => q.owner))];
78
+ const routed = owners.map((owner) => `${owner} ${report.questions.filter((q) => q.owner === owner).length}`);
79
+ return [
80
+ changed === 0
81
+ ? `The guide in ${report.out} was already current.`
82
+ : `Wrote the guide in ${report.out}: ${report.written.length} written, ${report.removed.length} removed.`,
83
+ `${count(report.areas.length, 'area')}, ${count(report.decisions.length, 'decision')}, ${count(report.rules.length, 'rule')}, ${asked}${routed.length ? `: ${routed.join(', ')}` : ''}.`,
84
+ '',
85
+ ].join('\n');
86
+ }
@@ -0,0 +1,15 @@
1
+ import { type Loaded } from '../config.ts';
2
+ import type { Knowledge } from './model.ts';
3
+ export declare const QUESTION_KINDS: readonly ['owner', 'reach', 'know', 'setup', 'trust'];
4
+ export type QuestionKind = (typeof QUESTION_KINDS)[number];
5
+ export interface Question {
6
+ id: string;
7
+ kind: QuestionKind;
8
+ area: string;
9
+ owner: string;
10
+ asks: string;
11
+ answer: string;
12
+ }
13
+ export declare function documentsOf(config: Loaded): string[];
14
+ export declare function defaultOwner(config: Loaded): string;
15
+ export declare function questionsOf(config: Loaded, knowledge: Knowledge): Question[];
@@ -0,0 +1,90 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { AshlarError } from '../config.js';
4
+ import { decisionCovers, ownerOf, ruleCoversArea } from '../knowledge/areas.js';
5
+ import { EVERYWHERE } from '../knowledge/decisions.js';
6
+ import { tidy, within } from '../layout.js';
7
+ import { byText } from '../order.js';
8
+ import { REACH_FILE } from '../reach.js';
9
+ export const QUESTION_KINDS = ['owner', 'reach', 'know', 'setup', 'trust'];
10
+ const RUNS = ['dev', 'start'];
11
+ function scriptsOf(root, folder) {
12
+ const file = join(root, folder, 'package.json');
13
+ if (!existsSync(file))
14
+ return null;
15
+ try {
16
+ const scripts = JSON.parse(readFileSync(file, 'utf8')).scripts;
17
+ return scripts && typeof scripts === 'object' ? scripts : {};
18
+ }
19
+ catch {
20
+ return {};
21
+ }
22
+ }
23
+ export function documentsOf(config) {
24
+ const named = config.onboard.documents ?? [
25
+ 'README.md',
26
+ ...config.apps.map((app) => tidy(`${app.root}/README.md`)),
27
+ ];
28
+ return [...new Set(named)].filter((file) => existsSync(join(config.root, file))).sort();
29
+ }
30
+ export function defaultOwner(config) {
31
+ if (config.onboard.owner)
32
+ return config.onboard.owner;
33
+ throw new AshlarError('Name the project’s default owner, who gets every question nobody else owns: `onboard: { owner: "@handle" }` in ashlar.config.ts.');
34
+ }
35
+ export function questionsOf(config, knowledge) {
36
+ const fallback = defaultOwner(config);
37
+ const { decisions, rules } = knowledge;
38
+ const ownerAt = (path) => ownerOf(config, decisions, path)?.handle ?? fallback;
39
+ const questions = [];
40
+ const ask = (kind, area, asks, answer, id = `${kind}:${area}`) => questions.push({
41
+ id,
42
+ kind,
43
+ area,
44
+ owner: ownerAt(area === EVERYWHERE ? '' : area),
45
+ asks,
46
+ answer,
47
+ });
48
+ const line = (area, kind, what) => `a line in ${config.onboard.decisions}: \`- <date> @you [${area}] ${kind}: ${what}\``;
49
+ const owned = knowledge.areas.filter((area) => ownerOf(config, decisions, area.path)?.from !== 'default');
50
+ if (owned.length === 0 && knowledge.areas.length > 0) {
51
+ ask('owner', EVERYWHERE, `Who owns what? No area names its owner yet, so every question goes to ${fallback}.`, `${line('<area>', 'owner', '@handle')} for each area, or one for \`*\``);
52
+ }
53
+ for (const area of knowledge.areas) {
54
+ if (owned.length > 0 && !owned.includes(area)) {
55
+ ask('owner', area.path, `Who owns ${area.name} (\`${area.path}\`)?`, line(area.path, 'owner', '@handle'));
56
+ }
57
+ const feature = knowledge.features.find((candidate) => candidate.folder === area.path);
58
+ if (feature && feature.reach === null) {
59
+ ask('reach', area.path, `How does a person reach ${area.name} from the app’s home?`, `its reach script, \`${area.path}/${REACH_FILE}\``);
60
+ }
61
+ const decided = decisions.some((decision) => decision.kind !== 'owner' &&
62
+ !decision.areas.includes(EVERYWHERE) &&
63
+ decisionCovers(decision, area.path));
64
+ const ruled = rules.some((rule) => ruleCoversArea(rule, area.path));
65
+ if (!decided && !ruled) {
66
+ ask('know', area.path, `What must an agent know before changing ${area.name} (\`${area.path}\`)?`, `${line(area.path, 'rule', 'what to know')}, or a rule in \`${config.onboard.rules}/\` whose paths cover it`);
67
+ }
68
+ }
69
+ const setUp = (area) => decisions.some((decision) => decision.kind === 'setup' &&
70
+ (area === EVERYWHERE ? decision.areas.includes(EVERYWHERE) : decisionCovers(decision, area)));
71
+ const root = scriptsOf(config.root, '');
72
+ if (root && typeof root.test !== 'string' && !setUp(EVERYWHERE)) {
73
+ ask('setup', EVERYWHERE, 'How are the tests run?', `a \`test\` script, or ${line(EVERYWHERE, 'setup', 'how')}`, 'setup:tests');
74
+ }
75
+ for (const app of config.apps) {
76
+ const area = app.root || EVERYWHERE;
77
+ const scripts = scriptsOf(config.root, app.root) ?? root ?? {};
78
+ if (RUNS.some((name) => typeof scripts[name] === 'string') || setUp(area))
79
+ continue;
80
+ ask('setup', area, `How do you run ${app.root ? `\`${app.root}\`` : 'the app'} locally?`, `a \`dev\` or \`start\` script, or ${line(area, 'setup', 'how')}`, `setup:${app.root || '.'}`);
81
+ }
82
+ for (const document of documentsOf(config)) {
83
+ const trusted = decisions.some((decision) => decision.kind === 'trust' &&
84
+ decision.areas.some((area) => area !== EVERYWHERE && within(document, area)));
85
+ if (!trusted) {
86
+ ask('trust', document, `Is \`${document}\` current, and who keeps it?`, line(document, 'trust', 'what holds'));
87
+ }
88
+ }
89
+ return questions.sort((a, b) => byText(a.owner, b.owner) || byText(a.id, b.id));
90
+ }
@@ -0,0 +1,28 @@
1
+ import type { Loaded } from '../config.ts';
2
+ export declare const SNAPSHOT_VERSION = 1;
3
+ export interface SnapshotArea {
4
+ path: string;
5
+ name: string;
6
+ lastChange: string | null;
7
+ commits30: number;
8
+ }
9
+ export interface Snapshot {
10
+ version: typeof SNAPSHOT_VERSION;
11
+ date: string;
12
+ branch: string | null;
13
+ commit: string | null;
14
+ ratchet: {
15
+ rule: string;
16
+ baseline: number;
17
+ }[];
18
+ map: {
19
+ features: number;
20
+ withReach: number;
21
+ };
22
+ decisions: number;
23
+ rules: number;
24
+ questions: number | null;
25
+ areas: SnapshotArea[];
26
+ }
27
+ export declare function snapshot(config: Loaded, today?: Date): Snapshot;
28
+ export declare function snapshotText(s: Snapshot): string;
@@ -0,0 +1,58 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import { readBaseline, totalOf } from '../baseline.js';
3
+ import { byText } from '../order.js';
4
+ import { questionsOf } from './questions.js';
5
+ import { knowledgeOf } from './model.js';
6
+ export const SNAPSHOT_VERSION = 1;
7
+ function git(root, ...args) {
8
+ const run = spawnSync('git', args, { cwd: root, encoding: 'utf8' });
9
+ return run.status === 0 ? run.stdout.trim() : null;
10
+ }
11
+ export function snapshot(config, today = new Date()) {
12
+ const knowledge = knowledgeOf(config);
13
+ const baseline = readBaseline(config.baseline) ?? {};
14
+ const head = git(config.root, 'rev-parse', '--short', 'HEAD');
15
+ return {
16
+ version: SNAPSHOT_VERSION,
17
+ date: today.toISOString().slice(0, 10),
18
+ branch: head ? (git(config.root, 'symbolic-ref', '--short', '-q', 'HEAD') ?? null) : null,
19
+ commit: head,
20
+ ratchet: config.rules.map((rule) => ({ rule, baseline: totalOf(baseline, rule) })),
21
+ map: {
22
+ features: knowledge.features.length,
23
+ withReach: knowledge.features.filter((feature) => feature.reach?.ok).length,
24
+ },
25
+ decisions: knowledge.decisions.length,
26
+ rules: knowledge.rules.length,
27
+ questions: config.onboard.owner ? questionsOf(config, knowledge).length : null,
28
+ areas: knowledge.areas.map((area) => ({
29
+ path: area.path,
30
+ name: area.name,
31
+ lastChange: head ? git(config.root, 'log', '-1', '--format=%cs', '--', area.path) || null : null,
32
+ commits30: head
33
+ ? Number(git(config.root, 'rev-list', '--count', '--since=30.days', 'HEAD', '--', area.path) ?? 0)
34
+ : 0,
35
+ })),
36
+ };
37
+ }
38
+ const count = (n, one) => `${n} ${one}${n === 1 ? '' : 's'}`;
39
+ export function snapshotText(s) {
40
+ const lines = [
41
+ `Where things stand on ${s.date}${s.commit ? `, at ${s.commit}${s.branch ? ` on ${s.branch}` : ''}` : ''}.`,
42
+ '',
43
+ `The ratchet holds ${s.ratchet.reduce((sum, rule) => sum + rule.baseline, 0)} breaks in its baseline.`,
44
+ ...s.ratchet.filter((rule) => rule.baseline > 0).map((rule) => ` ${rule.rule}: ${rule.baseline}`),
45
+ `The map has ${s.map.features} features, ${s.map.withReach} with a reach script.`,
46
+ `${count(s.decisions, 'decision')} and ${count(s.rules, 'folder rule')} are kept${s.questions === null ? '.' : `; ${count(s.questions, 'question')} ${s.questions === 1 ? 'is' : 'are'} open.`}`,
47
+ ];
48
+ const busy = [...s.areas].sort((a, b) => b.commits30 - a.commits30 || byText(a.path, b.path));
49
+ if (busy.length > 0) {
50
+ lines.push('', 'Areas, busiest in the last 30 days first:');
51
+ for (const area of busy) {
52
+ const commits = `${area.commits30} ${area.commits30 === 1 ? 'commit' : 'commits'}`;
53
+ lines.push(` ${area.path}: ${commits}, last ${area.lastChange ?? 'never'}`);
54
+ }
55
+ }
56
+ lines.push('', 'This is printed for today and never kept: run it again when you need it.', '');
57
+ return lines.join('\n');
58
+ }
@@ -0,0 +1 @@
1
+ export declare const byText: (a: string, b: string) => number;
package/dist/order.js ADDED
@@ -0,0 +1 @@
1
+ export const byText = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
@@ -0,0 +1,6 @@
1
+ import type { Edge, FileFindings } from './plugin/types.ts';
2
+ export interface Origins {
3
+ of(file: string, name: string): string[];
4
+ whole(file: string): string[];
5
+ }
6
+ export declare function originsOf(files: Record<string, FileFindings>, edges: Map<string, Edge[]>): Origins;
@@ -0,0 +1,51 @@
1
+ export function originsOf(files, edges) {
2
+ const passed = (file) => (edges.get(file) ?? []).filter((edge) => !edge.type && edge.passes);
3
+ const wholes = new Map();
4
+ const whole = (file) => {
5
+ const known = wholes.get(file);
6
+ if (known)
7
+ return known;
8
+ const out = new Set([file]);
9
+ wholes.set(file, [...out]);
10
+ for (const edge of passed(file))
11
+ for (const to of whole(edge.to))
12
+ out.add(to);
13
+ const all = [...out];
14
+ wholes.set(file, all);
15
+ return all;
16
+ };
17
+ const find = (file, name, seen) => {
18
+ const key = `${file}\0${name}`;
19
+ if (seen.has(key))
20
+ return [];
21
+ seen.add(key);
22
+ const own = files[file]?.exports;
23
+ if (!own)
24
+ return null;
25
+ if (own.includes(name))
26
+ return [file];
27
+ const named = [];
28
+ const stars = [];
29
+ for (const edge of passed(file))
30
+ for (const [there, here] of edge.passes) {
31
+ if (here === '*')
32
+ stars.push(edge);
33
+ else if (here === name)
34
+ named.push(file, ...(there === '*' ? whole(edge.to) : (find(edge.to, there, seen) ?? whole(edge.to))));
35
+ }
36
+ if (named.length > 0)
37
+ return named;
38
+ if (name === 'default')
39
+ return null;
40
+ for (const edge of stars) {
41
+ const from = find(edge.to, name, seen);
42
+ if (from && from.length > 0)
43
+ named.push(file, ...from);
44
+ }
45
+ return named.length > 0 ? named : null;
46
+ };
47
+ return {
48
+ of: (file, name) => find(file, name, new Set()) ?? whole(file),
49
+ whole,
50
+ };
51
+ }
@@ -0,0 +1,5 @@
1
+ import type { Rule } from './types.ts';
2
+ export declare const noCrossProcessImport: Rule;
3
+ export declare const sharedIsPure: Rule;
4
+ export declare const publicApiOnly: Rule;
5
+ export declare const clientOnly: Rule;
@@ -0,0 +1,174 @@
1
+ import { builtinModules } from 'node:module';
2
+ import { isIndexOf, isPackageIn } from '../layout.js';
3
+ import { found, hereOf } from './here.js';
4
+ import { importVisitor, merge } from './imports.js';
5
+ import { crossProcess, MESSAGES } from './messages.js';
6
+ import { isGlobalValue } from './references.js';
7
+ const BUILTINS = new Set(builtinModules);
8
+ const IMPURE_PACKAGES = ['react', 'react-dom'];
9
+ const IMPURE_GLOBALS = new Set([
10
+ 'window',
11
+ 'document',
12
+ 'navigator',
13
+ 'location',
14
+ 'localStorage',
15
+ 'sessionStorage',
16
+ 'process',
17
+ 'Buffer',
18
+ '__dirname',
19
+ '__filename',
20
+ 'global',
21
+ ]);
22
+ const RUNS_ON_IMPORT = new Set([
23
+ 'ExpressionStatement',
24
+ 'IfStatement',
25
+ 'SwitchStatement',
26
+ 'ForStatement',
27
+ 'ForInStatement',
28
+ 'ForOfStatement',
29
+ 'WhileStatement',
30
+ 'DoWhileStatement',
31
+ 'TryStatement',
32
+ 'BlockStatement',
33
+ 'ThrowStatement',
34
+ 'LabeledStatement',
35
+ ]);
36
+ const GLOBAL_OBJECTS = new Set(['window', 'globalThis', 'self']);
37
+ function meta(description) {
38
+ return { type: 'problem', docs: { description } };
39
+ }
40
+ export const noCrossProcessImport = {
41
+ meta: meta('The two processes never import each other, not even types.'),
42
+ create(context) {
43
+ const here = hereOf(context);
44
+ const app = here?.place.app;
45
+ const side = here?.place.process;
46
+ if (!here || !app || (side !== 'main' && side !== 'renderer'))
47
+ return {};
48
+ return importVisitor(({ specifier, node }) => {
49
+ const target = here.resolve(specifier);
50
+ if (!target)
51
+ return;
52
+ const there = here.placeOf(target);
53
+ const other = there.process === 'main' || there.process === 'renderer';
54
+ if (there.app?.root === app.root && other && there.process !== side) {
55
+ found(context, here, 'no-cross-process-import', node.loc, crossProcess(app));
56
+ }
57
+ });
58
+ },
59
+ };
60
+ export const sharedIsPure = {
61
+ meta: meta('Shared code holds types and pure functions.'),
62
+ create(context) {
63
+ const here = hereOf(context);
64
+ const app = here?.place.app;
65
+ if (!here || here.test || !app || here.place.process !== 'shared')
66
+ return {};
67
+ const flag = (loc) => found(context, here, 'shared-is-pure', loc, MESSAGES['shared-is-pure']);
68
+ return merge(importVisitor(({ specifier, node, effect }) => {
69
+ const bare = specifier.startsWith('node:') ||
70
+ BUILTINS.has(specifier) ||
71
+ isPackageIn(specifier, IMPURE_PACKAGES) ||
72
+ isPackageIn(specifier, app.bridge.packages);
73
+ if (bare || effect)
74
+ return flag(node.loc);
75
+ const target = here.resolve(specifier);
76
+ if (!target)
77
+ return;
78
+ const there = here.placeOf(target);
79
+ if (there.app?.root === app.root && (there.process === 'main' || there.process === 'renderer')) {
80
+ flag(node.loc);
81
+ }
82
+ }), {
83
+ Program(node) {
84
+ for (const statement of node.body) {
85
+ if (RUNS_ON_IMPORT.has(statement.type) && !statement.directive)
86
+ flag(statement.loc);
87
+ }
88
+ },
89
+ Identifier(node) {
90
+ if (IMPURE_GLOBALS.has(node.name) && isGlobalValue(context, node))
91
+ flag(node.loc);
92
+ },
93
+ });
94
+ },
95
+ };
96
+ export const publicApiOnly = {
97
+ meta: meta('Another feature or service area is reached through its index.'),
98
+ create(context) {
99
+ const here = hereOf(context);
100
+ if (!here)
101
+ return {};
102
+ return importVisitor(({ specifier, node }) => {
103
+ const target = here.resolve(specifier);
104
+ if (!target)
105
+ return;
106
+ const unit = here.placeOf(target).unit;
107
+ if (unit && unit !== here.place.unit && !isIndexOf(target, unit)) {
108
+ found(context, here, 'public-api-only', node.loc, MESSAGES['public-api-only']);
109
+ }
110
+ });
111
+ },
112
+ };
113
+ const WRAPPERS = new Set([
114
+ 'TSAsExpression',
115
+ 'TSNonNullExpression',
116
+ 'TSSatisfiesExpression',
117
+ 'TSTypeAssertion',
118
+ 'ParenthesizedExpression',
119
+ ]);
120
+ function unwrapped(node) {
121
+ let at = node;
122
+ while (WRAPPERS.has(at.type))
123
+ at = at.expression;
124
+ return at;
125
+ }
126
+ function isGlobalObject(node) {
127
+ const object = unwrapped(node);
128
+ return object.type === 'Identifier' && GLOBAL_OBJECTS.has(object.name);
129
+ }
130
+ function isBridge(context, node) {
131
+ const parent = node.parent;
132
+ if (parent?.type === 'MemberExpression' && parent.property === node && !parent.computed) {
133
+ return isGlobalObject(parent.object);
134
+ }
135
+ return isGlobalValue(context, node);
136
+ }
137
+ function bridgeKey(node, bridge) {
138
+ const key = node.property;
139
+ const named = (key.type === 'Literal' && key.value === bridge) ||
140
+ (key.type === 'TemplateLiteral' &&
141
+ key.expressions.length === 0 &&
142
+ key.quasis[0]?.value.cooked === bridge);
143
+ return named && isGlobalObject(node.object);
144
+ }
145
+ export const clientOnly = {
146
+ meta: meta('Only the client talks to the services.'),
147
+ create(context) {
148
+ const here = hereOf(context);
149
+ const app = here?.place.app;
150
+ const role = here?.place.role;
151
+ if (!here || here.test || !app || (role !== 'features' && role !== 'primitives'))
152
+ return {};
153
+ const flag = (loc) => found(context, here, 'client-only', loc, MESSAGES['client-only']);
154
+ const bridge = app.bridge.global;
155
+ return merge(importVisitor(({ specifier, node }) => {
156
+ if (isPackageIn(specifier, app.bridge.packages))
157
+ return flag(node.loc);
158
+ const target = here.resolve(specifier);
159
+ if (target && app.bridge.modules.includes(target))
160
+ flag(node.loc);
161
+ }), bridge
162
+ ? {
163
+ Identifier(node) {
164
+ if (node.name === bridge && isBridge(context, node))
165
+ flag(node.loc);
166
+ },
167
+ MemberExpression(node) {
168
+ if (node.computed && bridgeKey(node, bridge))
169
+ flag(node.loc);
170
+ },
171
+ }
172
+ : {});
173
+ },
174
+ };
@@ -0,0 +1,2 @@
1
+ import type { Visitor } from './types.ts';
2
+ export declare function exportsVisitor(on: (name: string) => void): Visitor;