@ontrails/trails 1.0.0-beta.14 → 1.0.0-beta.16

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 (197) hide show
  1. package/CHANGELOG.md +208 -0
  2. package/README.md +27 -0
  3. package/package.json +19 -8
  4. package/src/app.ts +17 -7
  5. package/src/clack.ts +1 -1
  6. package/src/cli.ts +304 -10
  7. package/src/completions.ts +240 -0
  8. package/src/load-app-mirror.ts +160 -0
  9. package/src/local-state-io.ts +153 -0
  10. package/src/project-writes.ts +320 -0
  11. package/src/run-collision.ts +125 -0
  12. package/src/run-completions-install.ts +179 -0
  13. package/src/run-example.ts +149 -0
  14. package/src/run-examples.ts +148 -0
  15. package/src/run-quiet.ts +75 -0
  16. package/src/run-trace.ts +273 -0
  17. package/src/run-warden.ts +39 -0
  18. package/src/run-watch.ts +432 -0
  19. package/src/scaffold-versions.generated.ts +12 -0
  20. package/src/trails/add-surface.ts +172 -0
  21. package/src/trails/add-trail.ts +73 -27
  22. package/src/trails/add-verify.ts +68 -23
  23. package/src/trails/completions-complete.ts +165 -0
  24. package/src/trails/completions.ts +47 -0
  25. package/src/trails/create-scaffold.ts +101 -35
  26. package/src/trails/create.ts +87 -74
  27. package/src/trails/dev-clean.ts +31 -22
  28. package/src/trails/dev-reset.ts +9 -3
  29. package/src/trails/dev-stats.ts +28 -20
  30. package/src/trails/dev-support.ts +109 -95
  31. package/src/trails/draft-promote.ts +351 -107
  32. package/src/trails/guide.ts +55 -38
  33. package/src/trails/load-app.ts +712 -38
  34. package/src/trails/root-dir.ts +21 -0
  35. package/src/trails/run-example.ts +482 -0
  36. package/src/trails/run-examples.ts +141 -0
  37. package/src/trails/run.ts +403 -0
  38. package/src/trails/survey.ts +517 -186
  39. package/src/trails/topo-activation.ts +385 -0
  40. package/src/trails/topo-compile.ts +55 -0
  41. package/src/trails/topo-history.ts +14 -11
  42. package/src/trails/topo-output-schemas.ts +175 -0
  43. package/src/trails/topo-pin.ts +25 -16
  44. package/src/trails/topo-read-support.ts +178 -238
  45. package/src/trails/topo-reports.ts +445 -63
  46. package/src/trails/topo-store-support.ts +67 -35
  47. package/src/trails/topo-support.ts +93 -147
  48. package/src/trails/topo-unpin.ts +17 -7
  49. package/src/trails/topo-verify.ts +19 -10
  50. package/src/trails/topo.ts +64 -31
  51. package/src/trails/warden-guide.ts +121 -0
  52. package/src/trails/warden.ts +137 -47
  53. package/src/versions.ts +28 -0
  54. package/.turbo/turbo-build.log +0 -1
  55. package/.turbo/turbo-lint.log +0 -3
  56. package/.turbo/turbo-typecheck.log +0 -1
  57. package/__tests__/examples.test.ts +0 -20
  58. package/dist/bin/trails.d.ts +0 -3
  59. package/dist/bin/trails.d.ts.map +0 -1
  60. package/dist/bin/trails.js +0 -4
  61. package/dist/bin/trails.js.map +0 -1
  62. package/dist/src/app.d.ts +0 -2
  63. package/dist/src/app.d.ts.map +0 -1
  64. package/dist/src/app.js +0 -22
  65. package/dist/src/app.js.map +0 -1
  66. package/dist/src/clack.d.ts +0 -9
  67. package/dist/src/clack.d.ts.map +0 -1
  68. package/dist/src/clack.js +0 -84
  69. package/dist/src/clack.js.map +0 -1
  70. package/dist/src/cli.d.ts +0 -2
  71. package/dist/src/cli.d.ts.map +0 -1
  72. package/dist/src/cli.js +0 -13
  73. package/dist/src/cli.js.map +0 -1
  74. package/dist/src/trails/add-surface.d.ts +0 -13
  75. package/dist/src/trails/add-surface.d.ts.map +0 -1
  76. package/dist/src/trails/add-surface.js +0 -88
  77. package/dist/src/trails/add-surface.js.map +0 -1
  78. package/dist/src/trails/add-trail.d.ts +0 -10
  79. package/dist/src/trails/add-trail.d.ts.map +0 -1
  80. package/dist/src/trails/add-trail.js +0 -77
  81. package/dist/src/trails/add-trail.js.map +0 -1
  82. package/dist/src/trails/add-trailhead.d.ts +0 -13
  83. package/dist/src/trails/add-trailhead.d.ts.map +0 -1
  84. package/dist/src/trails/add-trailhead.js +0 -88
  85. package/dist/src/trails/add-trailhead.js.map +0 -1
  86. package/dist/src/trails/add-verify.d.ts +0 -10
  87. package/dist/src/trails/add-verify.d.ts.map +0 -1
  88. package/dist/src/trails/add-verify.js +0 -67
  89. package/dist/src/trails/add-verify.js.map +0 -1
  90. package/dist/src/trails/create-scaffold.d.ts +0 -15
  91. package/dist/src/trails/create-scaffold.d.ts.map +0 -1
  92. package/dist/src/trails/create-scaffold.js +0 -288
  93. package/dist/src/trails/create-scaffold.js.map +0 -1
  94. package/dist/src/trails/create.d.ts +0 -22
  95. package/dist/src/trails/create.d.ts.map +0 -1
  96. package/dist/src/trails/create.js +0 -121
  97. package/dist/src/trails/create.js.map +0 -1
  98. package/dist/src/trails/dev-clean.d.ts +0 -9
  99. package/dist/src/trails/dev-clean.d.ts.map +0 -1
  100. package/dist/src/trails/dev-clean.js +0 -65
  101. package/dist/src/trails/dev-clean.js.map +0 -1
  102. package/dist/src/trails/dev-reset.d.ts +0 -6
  103. package/dist/src/trails/dev-reset.d.ts.map +0 -1
  104. package/dist/src/trails/dev-reset.js +0 -38
  105. package/dist/src/trails/dev-reset.js.map +0 -1
  106. package/dist/src/trails/dev-stats.d.ts +0 -7
  107. package/dist/src/trails/dev-stats.d.ts.map +0 -1
  108. package/dist/src/trails/dev-stats.js +0 -61
  109. package/dist/src/trails/dev-stats.js.map +0 -1
  110. package/dist/src/trails/dev-support.d.ts +0 -64
  111. package/dist/src/trails/dev-support.d.ts.map +0 -1
  112. package/dist/src/trails/dev-support.js +0 -178
  113. package/dist/src/trails/dev-support.js.map +0 -1
  114. package/dist/src/trails/draft-promote.d.ts +0 -18
  115. package/dist/src/trails/draft-promote.d.ts.map +0 -1
  116. package/dist/src/trails/draft-promote.js +0 -386
  117. package/dist/src/trails/draft-promote.js.map +0 -1
  118. package/dist/src/trails/guide.d.ts +0 -21
  119. package/dist/src/trails/guide.d.ts.map +0 -1
  120. package/dist/src/trails/guide.js +0 -64
  121. package/dist/src/trails/guide.js.map +0 -1
  122. package/dist/src/trails/load-app.d.ts +0 -6
  123. package/dist/src/trails/load-app.d.ts.map +0 -1
  124. package/dist/src/trails/load-app.js +0 -67
  125. package/dist/src/trails/load-app.js.map +0 -1
  126. package/dist/src/trails/project.d.ts +0 -8
  127. package/dist/src/trails/project.d.ts.map +0 -1
  128. package/dist/src/trails/project.js +0 -54
  129. package/dist/src/trails/project.js.map +0 -1
  130. package/dist/src/trails/survey.d.ts +0 -18
  131. package/dist/src/trails/survey.d.ts.map +0 -1
  132. package/dist/src/trails/survey.js +0 -212
  133. package/dist/src/trails/survey.js.map +0 -1
  134. package/dist/src/trails/topo-constants.d.ts +0 -3
  135. package/dist/src/trails/topo-constants.d.ts.map +0 -1
  136. package/dist/src/trails/topo-constants.js +0 -3
  137. package/dist/src/trails/topo-constants.js.map +0 -1
  138. package/dist/src/trails/topo-export.d.ts +0 -18
  139. package/dist/src/trails/topo-export.d.ts.map +0 -1
  140. package/dist/src/trails/topo-export.js +0 -34
  141. package/dist/src/trails/topo-export.js.map +0 -1
  142. package/dist/src/trails/topo-history.d.ts +0 -24
  143. package/dist/src/trails/topo-history.d.ts.map +0 -1
  144. package/dist/src/trails/topo-history.js +0 -33
  145. package/dist/src/trails/topo-history.js.map +0 -1
  146. package/dist/src/trails/topo-pin.d.ts +0 -21
  147. package/dist/src/trails/topo-pin.d.ts.map +0 -1
  148. package/dist/src/trails/topo-pin.js +0 -35
  149. package/dist/src/trails/topo-pin.js.map +0 -1
  150. package/dist/src/trails/topo-read-support.d.ts +0 -54
  151. package/dist/src/trails/topo-read-support.d.ts.map +0 -1
  152. package/dist/src/trails/topo-read-support.js +0 -178
  153. package/dist/src/trails/topo-read-support.js.map +0 -1
  154. package/dist/src/trails/topo-reports.d.ts +0 -50
  155. package/dist/src/trails/topo-reports.d.ts.map +0 -1
  156. package/dist/src/trails/topo-reports.js +0 -122
  157. package/dist/src/trails/topo-reports.js.map +0 -1
  158. package/dist/src/trails/topo-show.d.ts +0 -23
  159. package/dist/src/trails/topo-show.d.ts.map +0 -1
  160. package/dist/src/trails/topo-show.js +0 -53
  161. package/dist/src/trails/topo-show.js.map +0 -1
  162. package/dist/src/trails/topo-store-support.d.ts +0 -13
  163. package/dist/src/trails/topo-store-support.d.ts.map +0 -1
  164. package/dist/src/trails/topo-store-support.js +0 -55
  165. package/dist/src/trails/topo-store-support.js.map +0 -1
  166. package/dist/src/trails/topo-support.d.ts +0 -87
  167. package/dist/src/trails/topo-support.d.ts.map +0 -1
  168. package/dist/src/trails/topo-support.js +0 -165
  169. package/dist/src/trails/topo-support.js.map +0 -1
  170. package/dist/src/trails/topo-unpin.d.ts +0 -15
  171. package/dist/src/trails/topo-unpin.d.ts.map +0 -1
  172. package/dist/src/trails/topo-unpin.js +0 -39
  173. package/dist/src/trails/topo-unpin.js.map +0 -1
  174. package/dist/src/trails/topo-verify.d.ts +0 -5
  175. package/dist/src/trails/topo-verify.d.ts.map +0 -1
  176. package/dist/src/trails/topo-verify.js +0 -28
  177. package/dist/src/trails/topo-verify.js.map +0 -1
  178. package/dist/src/trails/topo.d.ts +0 -5
  179. package/dist/src/trails/topo.d.ts.map +0 -1
  180. package/dist/src/trails/topo.js +0 -67
  181. package/dist/src/trails/topo.js.map +0 -1
  182. package/dist/src/trails/warden.d.ts +0 -19
  183. package/dist/src/trails/warden.d.ts.map +0 -1
  184. package/dist/src/trails/warden.js +0 -89
  185. package/dist/src/trails/warden.js.map +0 -1
  186. package/dist/tsconfig.tsbuildinfo +0 -1
  187. package/src/__tests__/create.test.ts +0 -351
  188. package/src/__tests__/draft-promote.test.ts +0 -144
  189. package/src/__tests__/guide.test.ts +0 -91
  190. package/src/__tests__/load-app.test.ts +0 -58
  191. package/src/__tests__/survey.test.ts +0 -301
  192. package/src/__tests__/topo-dev.test.ts +0 -424
  193. package/src/__tests__/warden.test.ts +0 -74
  194. package/src/trails/add-trailhead.ts +0 -121
  195. package/src/trails/topo-export.ts +0 -39
  196. package/src/trails/topo-show.ts +0 -58
  197. package/tsconfig.json +0 -9
@@ -2,50 +2,73 @@
2
2
  * `add.trail` trail -- Scaffold a new trail file with tests.
3
3
  */
4
4
 
5
- import { mkdirSync } from 'node:fs';
6
- import { dirname, join, resolve } from 'node:path';
5
+ import { resolve } from 'node:path';
7
6
 
8
7
  import { Result, trail } from '@ontrails/core';
9
8
  import { z } from 'zod';
10
9
 
10
+ import {
11
+ trailIdToExportName,
12
+ trailIdToModuleName,
13
+ TRAIL_ID_MESSAGE,
14
+ TRAIL_ID_PATTERN,
15
+ validateTrailId,
16
+ writeProjectFile,
17
+ } from '../project-writes.js';
18
+
11
19
  // ---------------------------------------------------------------------------
12
20
  // Helpers
13
21
  // ---------------------------------------------------------------------------
14
22
 
23
+ const literal = (value: string): string => JSON.stringify(value);
24
+
25
+ const deriveExampleMessage = (id: string): string => `${id} completed`;
26
+
15
27
  const generateTrailFile = (
16
28
  id: string,
29
+ description: string,
30
+ exampleName: string,
17
31
  intent: 'read' | 'write' | 'destroy'
18
32
  ): string => {
19
- const intentLine = intent === 'write' ? '' : `\n intent: '${intent}',`;
33
+ const intentLine =
34
+ intent === 'write' ? '' : `\n intent: ${literal(intent)},`;
35
+ const exampleMessage = deriveExampleMessage(id);
36
+ const trailName = trailIdToExportName(id);
20
37
 
21
38
  return `import { Result, trail } from '@ontrails/core';
22
39
  import { z } from 'zod';
23
40
 
24
- export const ${id.replaceAll('.', '_')} = trail('${id}', {
25
- description: 'TODO: describe this trail',
41
+ export const ${trailName} = trail(${literal(id)}, {
42
+ blaze: async () => {
43
+ return Result.ok({ message: ${literal(exampleMessage)} });
44
+ },
45
+ description: ${literal(description)},
26
46
  examples: [
27
47
  {
48
+ expected: { message: ${literal(exampleMessage)} },
28
49
  input: {},
29
- name: 'TODO: add example',
50
+ name: ${literal(exampleName)},
30
51
  },
31
52
  ],
32
- blaze: async (input) => {
33
- return Result.ok({ message: 'TODO' });
34
- },
35
53
  input: z.object({}),${intentLine}
36
54
  output: z.object({ message: z.string() }),
37
55
  });
38
56
  `;
39
57
  };
40
58
 
41
- const generateTestFile = (id: string): string => {
42
- const moduleName = id.replaceAll('.', '-');
43
- const trailName = id.replaceAll('.', '_');
59
+ const generateTestFile = (id: string, exampleName: string): string => {
60
+ const moduleName = trailIdToModuleName(id);
61
+ const trailName = trailIdToExportName(id);
62
+ const exampleMessage = deriveExampleMessage(id);
44
63
  return `import { testTrail } from '@ontrails/testing';
45
64
  import { ${trailName} } from '../src/trails/${moduleName}.js';
46
65
 
47
66
  testTrail(${trailName}, [
48
- { description: 'basic test', input: {}, expectOk: true },
67
+ {
68
+ description: ${literal(exampleName)},
69
+ expectValue: { message: ${literal(exampleMessage)} },
70
+ input: {},
71
+ },
49
72
  ]);
50
73
  `;
51
74
  };
@@ -54,35 +77,58 @@ testTrail(${trailName}, [
54
77
  // Trail definition
55
78
  // ---------------------------------------------------------------------------
56
79
 
57
- /** Write a file, creating parent directories as needed. */
58
- const writeWithDirs = async (
59
- filePath: string,
60
- content: string
61
- ): Promise<void> => {
62
- mkdirSync(dirname(filePath), { recursive: true });
63
- await Bun.write(filePath, content);
64
- };
65
-
66
80
  export const addTrail = trail('add.trail', {
81
+ args: ['id'],
67
82
  blaze: async (input, ctx) => {
68
83
  const { id } = input;
69
- const moduleName = id.replaceAll('.', '-');
84
+ const validated = validateTrailId(id);
85
+ if (validated.isErr()) {
86
+ return Result.err(validated.error);
87
+ }
88
+
89
+ const moduleName = trailIdToModuleName(validated.value);
70
90
  const cwd = resolve(ctx.cwd ?? '.');
71
91
 
72
92
  const files = new Map<string, string>([
73
- [`src/trails/${moduleName}.ts`, generateTrailFile(id, input.intent)],
74
- [`__tests__/${moduleName}.test.ts`, generateTestFile(id)],
93
+ [
94
+ `src/trails/${moduleName}.ts`,
95
+ generateTrailFile(
96
+ id,
97
+ input.description,
98
+ input.exampleName,
99
+ input.intent
100
+ ),
101
+ ],
102
+ [
103
+ `__tests__/${moduleName}.test.ts`,
104
+ generateTestFile(id, input.exampleName),
105
+ ],
75
106
  ]);
76
107
 
77
108
  for (const [relativePath, content] of files) {
78
- await writeWithDirs(join(cwd, relativePath), content);
109
+ const written = await writeProjectFile(cwd, relativePath, content);
110
+ if (written.isErr()) {
111
+ return Result.err(written.error);
112
+ }
79
113
  }
80
114
 
81
115
  return Result.ok({ created: [...files.keys()] });
82
116
  },
83
117
  description: 'Scaffold a new trail with tests and examples',
84
118
  input: z.object({
85
- id: z.string().describe('Trail ID (e.g., entity.update)'),
119
+ description: z
120
+ .string()
121
+ .min(1, 'Trail description is required')
122
+ .describe('Trail description'),
123
+ exampleName: z
124
+ .string()
125
+ .min(1, 'Starter example name is required')
126
+ .describe('Starter example name'),
127
+ id: z
128
+ .string()
129
+ .min(1, 'Trail ID is required')
130
+ .regex(TRAIL_ID_PATTERN, TRAIL_ID_MESSAGE)
131
+ .describe('Trail ID (e.g., entity.update)'),
86
132
  intent: z
87
133
  .enum(['read', 'write', 'destroy'])
88
134
  .default('write')
@@ -2,36 +2,47 @@
2
2
  * `add.verify` trail -- Add testing + warden setup to a project.
3
3
  */
4
4
 
5
- import { existsSync, mkdirSync } from 'node:fs';
6
- import { dirname, join, resolve } from 'node:path';
5
+ import { existsSync } from 'node:fs';
7
6
 
8
7
  import { Result, trail } from '@ontrails/core';
9
8
  import { z } from 'zod';
10
9
 
10
+ import {
11
+ PROJECT_NAME_MESSAGE,
12
+ PROJECT_NAME_PATTERN,
13
+ resolveProjectDir,
14
+ resolveProjectPath,
15
+ writeProjectFile,
16
+ } from '../project-writes.js';
17
+ import {
18
+ ontrailsPackageRange,
19
+ scaffoldDependencyVersions,
20
+ } from '../versions.js';
21
+
11
22
  // ---------------------------------------------------------------------------
12
23
  // Content generators
13
24
  // ---------------------------------------------------------------------------
14
25
 
15
26
  const generateTestFile = (): string =>
16
- `import { testAll } from '@ontrails/testing';
27
+ `import { testAllEstablished } from '@ontrails/testing';
17
28
  import { app } from '../src/app.js';
18
29
 
19
- testAll(app);
30
+ testAllEstablished(app);
20
31
  `;
21
32
 
22
33
  const generateLefthookYml = (): string =>
23
34
  `pre-push:
24
35
  commands:
25
36
  warden:
26
- run: bunx trails warden --exit-code
37
+ run: bunx trails warden
27
38
  `;
28
39
 
29
40
  /** Add testing and warden devDependencies to package.json when present. */
30
41
  const patchVerifyDeps = (pkg: Record<string, unknown>): void => {
31
42
  const devDeps = (pkg['devDependencies'] ?? {}) as Record<string, string>;
32
- devDeps['@ontrails/testing'] = 'workspace:*';
33
- devDeps['@ontrails/warden'] = 'workspace:*';
34
- devDeps['lefthook'] = '^2.1.1';
43
+ devDeps['@ontrails/testing'] = ontrailsPackageRange;
44
+ devDeps['@ontrails/warden'] = ontrailsPackageRange;
45
+ devDeps['lefthook'] = scaffoldDependencyVersions.lefthook;
35
46
  pkg['devDependencies'] = Object.fromEntries(
36
47
  Object.entries(devDeps).toSorted(([a], [b]) => a.localeCompare(b))
37
48
  );
@@ -40,14 +51,24 @@ const patchVerifyDeps = (pkg: Record<string, unknown>): void => {
40
51
  /** Update package.json in the target project with verify dependencies. */
41
52
  const updatePackageJsonForVerify = async (
42
53
  projectDir: string
43
- ): Promise<void> => {
44
- const pkgPath = join(projectDir, 'package.json');
54
+ ): Promise<Result<void, Error>> => {
55
+ const pkgPathResult = resolveProjectPath(projectDir, 'package.json');
56
+ if (pkgPathResult.isErr()) {
57
+ return Result.err(pkgPathResult.error);
58
+ }
59
+
60
+ const pkgPath = pkgPathResult.value;
45
61
  if (!existsSync(pkgPath)) {
46
- return;
62
+ return Result.ok();
47
63
  }
48
64
  const pkg = (await Bun.file(pkgPath).json()) as Record<string, unknown>;
49
65
  patchVerifyDeps(pkg);
50
- await Bun.write(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`);
66
+ const written = await writeProjectFile(
67
+ projectDir,
68
+ 'package.json',
69
+ `${JSON.stringify(pkg, null, 2)}\n`
70
+ );
71
+ return written.isErr() ? Result.err(written.error) : Result.ok();
51
72
  };
52
73
 
53
74
  // ---------------------------------------------------------------------------
@@ -56,32 +77,56 @@ const updatePackageJsonForVerify = async (
56
77
 
57
78
  export const addVerify = trail('add.verify', {
58
79
  blaze: async (input) => {
59
- const projectDir = resolve(input.dir ?? '.', input.name);
80
+ const projectDirResult = resolveProjectDir(input.dir ?? '.', input.name);
81
+ if (projectDirResult.isErr()) {
82
+ return Result.err(projectDirResult.error);
83
+ }
84
+
85
+ const projectDir = projectDirResult.value;
60
86
  const files: string[] = [];
61
87
 
62
88
  const writeFile = async (
63
89
  relativePath: string,
64
90
  content: string
65
- ): Promise<void> => {
66
- const fullPath = join(projectDir, relativePath);
67
- mkdirSync(dirname(fullPath), { recursive: true });
68
- await Bun.write(fullPath, content);
69
- files.push(relativePath);
91
+ ): Promise<Result<void, Error>> => {
92
+ const written = await writeProjectFile(projectDir, relativePath, content);
93
+ if (written.isErr()) {
94
+ return Result.err(written.error);
95
+ }
96
+ files.push(written.value);
97
+ return Result.ok();
70
98
  };
71
99
 
72
- await writeFile('__tests__/examples.test.ts', generateTestFile());
73
- await writeFile('lefthook.yml', generateLefthookYml());
74
- await updatePackageJsonForVerify(projectDir);
100
+ const testFile = await writeFile(
101
+ '__tests__/examples.test.ts',
102
+ generateTestFile()
103
+ );
104
+ if (testFile.isErr()) {
105
+ return Result.err(testFile.error);
106
+ }
107
+
108
+ const lefthookFile = await writeFile('lefthook.yml', generateLefthookYml());
109
+ if (lefthookFile.isErr()) {
110
+ return Result.err(lefthookFile.error);
111
+ }
112
+
113
+ const packageResult = await updatePackageJsonForVerify(projectDir);
114
+ if (packageResult.isErr()) {
115
+ return Result.err(packageResult.error);
116
+ }
75
117
 
76
118
  return Result.ok({ created: files });
77
119
  },
78
120
  description: 'Add testing and warden verification',
79
121
  input: z.object({
80
122
  dir: z.string().optional().describe('Parent directory'),
81
- name: z.string().describe('Project name'),
123
+ name: z
124
+ .string()
125
+ .regex(PROJECT_NAME_PATTERN, PROJECT_NAME_MESSAGE)
126
+ .describe('Project name'),
82
127
  }),
83
- meta: { internal: true },
84
128
  output: z.object({
85
129
  created: z.array(z.string()),
86
130
  }),
131
+ visibility: 'internal',
87
132
  });
@@ -0,0 +1,165 @@
1
+ /**
2
+ * `completions __complete` internal trail -- dynamic completion suggestions.
3
+ *
4
+ * The static shell scripts emitted by {@link completionsTrail} delegate to this
5
+ * trail at tab-press time. The trail receives the partial argv that the user
6
+ * has typed (after the binary name) and returns newline-delimited suggestions
7
+ * the shell should offer.
8
+ *
9
+ * Today the trail knows about two `run` positions:
10
+ *
11
+ * - `trails run <prefix>` — return matching trail IDs.
12
+ * - `trails run example <trail-id> <prefix>` — return matching example names
13
+ * defined on the resolved trail.
14
+ *
15
+ * The `run example` branch loads the trail's owning app at tab-press time so the
16
+ * suggestions reflect the live trail definition. Unknown trails and no
17
+ * examples naturally collapse to an empty list; recoverable load failures are
18
+ * suppressed here because completion must never surface errors back to the
19
+ * shell mid-keystroke.
20
+ */
21
+
22
+ import { Result, trail } from '@ontrails/core';
23
+ import { z } from 'zod';
24
+
25
+ import {
26
+ renderTrailExampleCompletions,
27
+ renderTrailIdCompletions,
28
+ } from '../completions.js';
29
+ import { resolveTrailRootDir } from './root-dir.js';
30
+
31
+ const EMPTY_SUGGESTIONS = '';
32
+
33
+ interface CompleteContext {
34
+ readonly args: readonly string[];
35
+ readonly rootDir: string;
36
+ }
37
+
38
+ type CompletionHandler = (ctx: CompleteContext) => Promise<readonly string[]>;
39
+
40
+ /**
41
+ * Detect whether the user is completing the example-name positional on a
42
+ * `trails run example` invocation.
43
+ *
44
+ * The shell hands us the partial argv with the **last element** as the token
45
+ * being completed. We recognize the `run example <trail-id> <TAB>` shape when:
46
+ *
47
+ * - the command family is `run example`, and
48
+ * - a non-flag positional (the trail ID) sits at `args[2]`.
49
+ *
50
+ * Returns the trail ID + prefix to complete, or `null` if the cursor is not
51
+ * in an example-name value position.
52
+ */
53
+ const detectExampleValueCompletion = (
54
+ args: readonly string[]
55
+ ): { readonly trailId: string; readonly prefix: string } | null => {
56
+ if (args.length < 4) {
57
+ return null;
58
+ }
59
+ const [, subcommand, trailId] = args;
60
+ if (subcommand !== 'example') {
61
+ return null;
62
+ }
63
+ if (trailId === undefined || trailId.startsWith('-')) {
64
+ return null;
65
+ }
66
+ const prefix = args[3] ?? '';
67
+ return { prefix, trailId };
68
+ };
69
+
70
+ /**
71
+ * Handler for the `trails run` subcommand.
72
+ *
73
+ * Two completion positions are recognized:
74
+ *
75
+ * - `trails run example <trail-id> <prefix>` — return example names defined
76
+ * on the resolved trail (matching `prefix`, sorted).
77
+ * - `trails run <prefix>` — return matching trail IDs.
78
+ *
79
+ * Anything else (unknown flag context, a cursor beyond the trail ID, etc.)
80
+ * returns no suggestions so completed positional values are not suggested
81
+ * again.
82
+ */
83
+ const completeRunPosition: CompletionHandler = async ({ args, rootDir }) => {
84
+ const exampleContext = detectExampleValueCompletion(args);
85
+ if (exampleContext !== null) {
86
+ const suggestionsResult = await renderTrailExampleCompletions(
87
+ rootDir,
88
+ exampleContext.trailId,
89
+ exampleContext.prefix
90
+ );
91
+ return suggestionsResult.unwrapOr([]);
92
+ }
93
+ if (args.length !== 2) {
94
+ return [];
95
+ }
96
+ const prefix = args[1] ?? '';
97
+ return await renderTrailIdCompletions(rootDir, prefix);
98
+ };
99
+
100
+ const renderSuggestions = (suggestions: readonly string[]): string =>
101
+ suggestions.join('\n');
102
+
103
+ /**
104
+ * Subcommand → handler dispatch table.
105
+ *
106
+ * Keep this a pure lookup so adding a new completion target (`run example`,
107
+ * `--app`, etc.) is a new entry rather than a new branch.
108
+ *
109
+ * @remarks As more handlers grow per-token-shape logic (e.g. distinguishing
110
+ * `--app <TAB>` vs `<trail-id> <TAB>` for the same subcommand), expect this
111
+ * table to evolve into a sub-table of (token-pattern → completion-fn) per
112
+ * subcommand or a small parser yielding a discriminated `CompletionContext`
113
+ * union. Today the single `'run'` entry is small enough that explicit
114
+ * branching inside `completeRunPosition` is cleaner.
115
+ */
116
+ const SUBCOMMAND_HANDLERS: Readonly<Record<string, CompletionHandler>> = {
117
+ run: completeRunPosition,
118
+ };
119
+
120
+ export const completionsCompleteTrail = trail('completions.__complete', {
121
+ blaze: async (input, ctx) => {
122
+ const rootDirResult = resolveTrailRootDir(input.rootDir, ctx.cwd);
123
+ if (rootDirResult.isErr()) {
124
+ return Result.err(rootDirResult.error);
125
+ }
126
+ const rootDir = rootDirResult.value;
127
+
128
+ const [subcommand] = input.args;
129
+ if (subcommand === undefined) {
130
+ return Result.ok(EMPTY_SUGGESTIONS);
131
+ }
132
+
133
+ const handler = SUBCOMMAND_HANDLERS[subcommand];
134
+ if (handler === undefined) {
135
+ return Result.ok(EMPTY_SUGGESTIONS);
136
+ }
137
+
138
+ const suggestions = await handler({ args: input.args, rootDir });
139
+ return Result.ok(renderSuggestions(suggestions));
140
+ },
141
+ description:
142
+ 'Internal: emit dynamic completion suggestions for the current partial argv. Invoked by the static shell completion script at tab-press time.',
143
+ examples: [
144
+ {
145
+ description: 'Empty argv yields no suggestions',
146
+ input: { args: [] },
147
+ name: 'Empty args',
148
+ },
149
+ ],
150
+ input: z.object({
151
+ args: z
152
+ .array(z.string())
153
+ .readonly()
154
+ .describe(
155
+ 'Partial argv after the binary name; the last element is the token being completed'
156
+ ),
157
+ rootDir: z.string().optional().describe('Workspace root directory'),
158
+ }),
159
+ intent: 'read',
160
+ output: z
161
+ .string()
162
+ .describe(
163
+ 'Newline-delimited suggestions the shell should offer for the current token'
164
+ ),
165
+ });
@@ -0,0 +1,47 @@
1
+ /**
2
+ * `completions` trail -- Print a shell completion script for the `trails` CLI.
3
+ *
4
+ * The trail's responsibility is small: render a static shell script that, when
5
+ * sourced by the user's shell, registers a tab-completion handler that
6
+ * delegates to `trails completions __complete <args...>` for the live
7
+ * suggestions. See {@link renderCompletionScript} for the per-shell shape.
8
+ */
9
+
10
+ import { trail } from '@ontrails/core';
11
+ import { z } from 'zod';
12
+
13
+ import { renderCompletionScript } from '../completions.js';
14
+
15
+ const COMPLETIONS_BIN_NAME = 'trails';
16
+
17
+ export const completionsTrail = trail('completions', {
18
+ args: ['shell'],
19
+ blaze: async (input) =>
20
+ renderCompletionScript(input.shell, COMPLETIONS_BIN_NAME),
21
+ description:
22
+ 'Print a shell completion script for the trails CLI; pipe into your shell rc to register tab-completion',
23
+ examples: [
24
+ {
25
+ description: 'Render a bash completion script',
26
+ input: { shell: 'bash' },
27
+ name: 'Render bash completion',
28
+ },
29
+ {
30
+ description: 'Render a zsh completion script',
31
+ input: { shell: 'zsh' },
32
+ name: 'Render zsh completion',
33
+ },
34
+ {
35
+ description: 'Render a fish completion script',
36
+ input: { shell: 'fish' },
37
+ name: 'Render fish completion',
38
+ },
39
+ ],
40
+ input: z.object({
41
+ shell: z
42
+ .enum(['bash', 'zsh', 'fish'])
43
+ .describe('Target shell flavor for the completion script'),
44
+ }),
45
+ intent: 'read',
46
+ output: z.string(),
47
+ });