@mwillbanks/ast-mcp 0.1.3 → 0.2.1

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 (58) hide show
  1. package/README.md +59 -20
  2. package/dist/ast-mcp.js +232 -130
  3. package/dist/ast-mcp.js.map +107 -27
  4. package/package.json +2 -1
  5. package/src/ast-bro/capability.ts +3 -8
  6. package/src/ast-bro/client.ts +18 -2
  7. package/src/ast-bro/result.ts +5 -12
  8. package/src/cli.ts +70 -95
  9. package/src/config-cli.ts +54 -0
  10. package/src/config.ts +886 -0
  11. package/src/helpers/aider.ts +252 -0
  12. package/src/helpers/ast-bro.ts +64 -0
  13. package/src/helpers/config.ts +149 -0
  14. package/src/helpers/hook.ts +60 -0
  15. package/src/helpers/installer.ts +249 -0
  16. package/src/helpers/json.ts +19 -0
  17. package/src/helpers/mcp-schema.ts +31 -0
  18. package/src/helpers/process.ts +31 -0
  19. package/src/helpers/string.ts +97 -0
  20. package/src/hook.ts +135 -40
  21. package/src/http-entry.ts +1 -1
  22. package/src/http.ts +118 -103
  23. package/src/installer-transport.ts +190 -0
  24. package/src/installer.ts +604 -247
  25. package/src/mcp-cli.ts +104 -0
  26. package/src/patch/aider.ts +6 -199
  27. package/src/patch/engine.ts +271 -197
  28. package/src/runtime/dependencies.ts +48 -29
  29. package/src/runtime/file-delete.ts +13 -9
  30. package/src/runtime/file-read.ts +91 -57
  31. package/src/runtime/file-rename.ts +158 -0
  32. package/src/runtime/format.ts +57 -34
  33. package/src/runtime/paths.ts +30 -16
  34. package/src/runtime/policy.ts +21 -0
  35. package/src/runtime/process-input.ts +14 -4
  36. package/src/service.ts +217 -0
  37. package/src/shell-policy.ts +331 -0
  38. package/src/stdio.ts +47 -35
  39. package/src/tools/ast-bro.ts +12 -6
  40. package/src/tools/configured.ts +28 -0
  41. package/src/tools/files.ts +24 -27
  42. package/src/tools/index.ts +5 -3
  43. package/src/tools/lifecycle.ts +69 -55
  44. package/templates/skills/ast-mcp/SKILL.md +7 -6
  45. package/templates/skills/ast-mcp/evals/batch.evals.json +48 -0
  46. package/templates/skills/ast-mcp/evals/evals.json +1 -0
  47. package/templates/skills/ast-mcp/evals/score.test.ts +175 -0
  48. package/templates/skills/ast-mcp/evals/score.ts +304 -267
  49. package/templates/skills/ast-mcp/evals/semantic.test.ts +322 -0
  50. package/templates/skills/ast-mcp/evals/semantic.ts +390 -135
  51. package/templates/skills/ast-mcp/evals/shell-policy.ts +252 -0
  52. package/templates/skills/ast-mcp/evals/source.test.ts +46 -0
  53. package/templates/skills/ast-mcp/evals/source.ts +164 -0
  54. package/templates/skills/ast-mcp/evals/trigger-queries.json +1 -1
  55. package/templates/skills/ast-mcp/references/installation.md +9 -8
  56. package/templates/skills/ast-mcp/references/patch-state-machine.md +5 -5
  57. package/templates/skills/ast-mcp/references/tool-catalog.md +4 -2
  58. package/templates/skills/ast-mcp/scripts/check-install.ts +323 -131
@@ -22,6 +22,7 @@ import { formatContent } from "../runtime/format";
22
22
  import { sha256 } from "../runtime/hash";
23
23
  import { withFileLock } from "../runtime/locks";
24
24
  import { resolveWritablePath } from "../runtime/paths";
25
+ import { requireExpectedHash, verifyExpectedHash } from "../runtime/policy";
25
26
 
26
27
  import { applyAiderBlock } from "./aider";
27
28
  import { detectAstLanguage } from "./languages";
@@ -38,7 +39,7 @@ export interface AiderBlock {
38
39
  export interface PatchRequest {
39
40
  aiderBlock?: AiderBlock;
40
41
  astRule?: AstRule;
41
- expectedSha256: string;
42
+ expectedSha256?: string;
42
43
  filePath: string;
43
44
  patchStrategy: "ast" | "aider_block";
44
45
  preview?: boolean;
@@ -48,7 +49,7 @@ export interface PatchBatchRequest {
48
49
  aiderBlocks?: AiderBlock[];
49
50
  astRules?: AstRule[];
50
51
  chattr?: FileChattr;
51
- expectedSha256: string;
52
+ expectedSha256?: string;
52
53
  patchStrategy: "ast" | "aider_block";
53
54
  preview?: boolean;
54
55
  }
@@ -161,191 +162,269 @@ function previewDiff(filePath: string, before: string, after: string): string {
161
162
  : lines.join("\n");
162
163
  }
163
164
 
164
- async function applyPatchBatch(
165
- inputPath: string,
165
+ interface PatchContext {
166
+ actual: string;
167
+ aiderBlocks: AiderBlock[];
168
+ astRules: AstRule[];
169
+ filePath: string;
170
+ language: ReturnType<typeof detectAstLanguage>;
171
+ original: string;
172
+ request: PatchBatchRequest;
173
+ rewritable: boolean;
174
+ }
175
+
176
+ interface PreviewMetadata {
177
+ matches?: number;
178
+ matchMethods?: string[];
179
+ operations: number;
180
+ strategy: "ast" | "aider_block";
181
+ }
182
+
183
+ async function createPatchContext(
184
+ filePath: string,
166
185
  request: PatchBatchRequest,
167
- ): Promise<Record<string, unknown>> {
168
- const filePath = await resolveWritablePath(inputPath);
169
- return withFileLock(filePath, async () => {
170
- const original = await readFile(filePath, "utf8");
171
- const actual = sha256(original);
172
- if (actual !== request.expectedSha256)
173
- throw new Error(
174
- "Stale file context: expected " +
175
- request.expectedSha256 +
176
- ", found " +
177
- actual,
178
- );
186
+ ): Promise<PatchContext> {
187
+ const original = await readFile(filePath, "utf8");
188
+ const actual = sha256(original);
189
+ await requireExpectedHash(request.expectedSha256, "file_patch");
190
+ verifyExpectedHash(request.expectedSha256, actual);
191
+ const language = detectAstLanguage(filePath);
192
+ return {
193
+ actual,
194
+ aiderBlocks: request.aiderBlocks ?? [],
195
+ astRules: request.astRules ?? [],
196
+ filePath,
197
+ language,
198
+ original,
199
+ request,
200
+ rewritable: await astRewritable(filePath, language),
201
+ };
202
+ }
179
203
 
180
- const astRules = request.astRules ?? [];
181
- const aiderBlocks = request.aiderBlocks ?? [];
182
- const language = detectAstLanguage(filePath);
183
- const rewritable = await astRewritable(filePath, language);
184
- const previewResult = async (
185
- candidate: string,
186
- metadata: {
187
- operations: number;
188
- strategy: "ast" | "aider_block";
189
- matches?: number;
190
- matchMethods?: string[];
191
- },
192
- ) => {
193
- const formatted = await formatContent(filePath, candidate);
194
- return {
195
- changed: formatted !== original,
196
- diff: previewDiff(filePath, original, formatted),
197
- filePath,
198
- matches: metadata.matches,
199
- matchMethods: metadata.matchMethods,
200
- operations: metadata.operations,
201
- preview: true,
202
- sha256: sha256(formatted),
203
- strategy: metadata.strategy,
204
- };
205
- };
204
+ async function patchPreview(
205
+ context: PatchContext,
206
+ candidate: string,
207
+ metadata: PreviewMetadata,
208
+ ) {
209
+ const formatted = await formatContent(context.filePath, candidate);
210
+ return {
211
+ changed: formatted !== context.original,
212
+ diff: previewDiff(context.filePath, context.original, formatted),
213
+ filePath: context.filePath,
214
+ matches: metadata.matches,
215
+ matchMethods: metadata.matchMethods,
216
+ operations: metadata.operations,
217
+ preview: true,
218
+ sha256: sha256(formatted),
219
+ strategy: metadata.strategy,
220
+ };
221
+ }
206
222
 
207
- if (request.patchStrategy === "ast") {
208
- if (!rewritable || !language)
209
- throw new Error(
210
- "REJECTED: non-structurally-rewritable files require patchStrategy 'aider_block' with aiderBlocks",
211
- );
212
- if (astRules.length === 0 || aiderBlocks.length > 0)
213
- throw new Error(
214
- "REJECTED: patchStrategy 'ast' requires astRules and no aiderBlocks",
215
- );
223
+ function validateAstStrategy(
224
+ context: PatchContext,
225
+ ): asserts context is PatchContext & {
226
+ language: NonNullable<PatchContext["language"]>;
227
+ } {
228
+ if (!context.rewritable || !context.language)
229
+ throw new Error(
230
+ "REJECTED: non-structurally-rewritable files require patchStrategy 'aider_block' with aiderBlocks",
231
+ );
232
+ if (context.astRules.length === 0 || context.aiderBlocks.length > 0)
233
+ throw new Error(
234
+ "REJECTED: patchStrategy 'ast' requires astRules and no aiderBlocks",
235
+ );
236
+ }
216
237
 
217
- const next = temporary(filePath);
218
- const metadata = await lstat(filePath);
219
- try {
220
- await writeFile(next, original, {
221
- encoding: "utf8",
222
- flag: "wx",
223
- mode: metadata.mode,
224
- });
225
- let totalMatches = 0;
226
- for (const rule of astRules) {
227
- const preview = parseAstBroJson(
228
- await callAstBro(
229
- "run",
230
- {
231
- json: true,
232
- lang: language,
233
- paths: [next],
234
- pattern: rule.pattern,
235
- },
236
- path.dirname(filePath),
237
- ),
238
- );
239
- if (preview.error_count)
240
- throw new Error(
241
- `ast-bro preview failed with ${preview.error_count} errors`,
242
- );
243
- const matches = Array.isArray(preview.matches)
244
- ? preview.matches.length
245
- : 0;
246
- const expected = rule.expectedMatches ?? 1;
247
- if (matches !== expected)
248
- throw new Error(
249
- `AST rule matched ${matches} nodes; expected ${expected}`,
250
- );
251
- if (expected !== 1)
252
- throw new Error(
253
- "ast-bro run rewrites only the first match per file; narrow the AST rule to exactly one node",
254
- );
238
+ type AstPatchContext = PatchContext & {
239
+ language: NonNullable<PatchContext["language"]>;
240
+ };
255
241
 
256
- const rewritten = parseAstBroJson(
257
- await callAstBro(
258
- "run",
259
- {
260
- json: true,
261
- lang: language,
262
- paths: [next],
263
- pattern: rule.pattern,
264
- rewrite: rule.fix,
265
- write: true,
266
- },
267
- path.dirname(filePath),
268
- ),
269
- );
270
- if (
271
- rewritten.error_count ||
272
- rewritten.rewrite_count !== 1 ||
273
- rewritten.files?.[0]?.status !== "rewritten"
274
- )
275
- throw new Error("ast-bro run did not rewrite exactly one file");
276
- totalMatches += matches;
277
- }
242
+ async function previewAstRule(
243
+ context: AstPatchContext,
244
+ next: string,
245
+ rule: AstRule,
246
+ ) {
247
+ const preview = parseAstBroJson(
248
+ await callAstBro(
249
+ "run",
250
+ {
251
+ json: true,
252
+ lang: context.language,
253
+ paths: [next],
254
+ pattern: rule.pattern,
255
+ },
256
+ path.dirname(context.filePath),
257
+ ),
258
+ );
259
+ if (preview.error_count)
260
+ throw new Error(
261
+ `ast-bro preview failed with ${preview.error_count} errors`,
262
+ );
263
+ const matches = Array.isArray(preview.matches) ? preview.matches.length : 0;
264
+ const expected = rule.expectedMatches ?? 1;
265
+ if (matches !== expected)
266
+ throw new Error(`AST rule matched ${matches} nodes; expected ${expected}`);
267
+ if (expected !== 1)
268
+ throw new Error(
269
+ "ast-bro run rewrites only the first match per file; narrow the AST rule to exactly one node",
270
+ );
271
+ return matches;
272
+ }
278
273
 
279
- const candidate = await readFile(next, "utf8");
280
- if (request.preview)
281
- return previewResult(candidate, {
282
- matches: totalMatches,
283
- operations: astRules.length,
284
- strategy: "ast",
285
- });
274
+ async function rewriteAstRule(
275
+ context: AstPatchContext,
276
+ next: string,
277
+ rule: AstRule,
278
+ ) {
279
+ const rewritten = parseAstBroJson(
280
+ await callAstBro(
281
+ "run",
282
+ {
283
+ json: true,
284
+ lang: context.language,
285
+ paths: [next],
286
+ pattern: rule.pattern,
287
+ rewrite: rule.fix,
288
+ write: true,
289
+ },
290
+ path.dirname(context.filePath),
291
+ ),
292
+ );
293
+ if (
294
+ rewritten.error_count ||
295
+ rewritten.rewrite_count !== 1 ||
296
+ rewritten.files?.[0]?.status !== "rewritten"
297
+ )
298
+ throw new Error("ast-bro run did not rewrite exactly one file");
299
+ }
286
300
 
287
- await commit(
288
- filePath,
289
- candidate,
290
- metadata.mode,
291
- actual,
292
- request.chattr,
293
- );
294
- const updated = await readFile(filePath, "utf8");
295
- return {
296
- engine: "ast-bro.run",
297
- filePath,
298
- matches: totalMatches,
299
- operations: astRules.length,
300
- preview: false,
301
- sha256: sha256(updated),
302
- strategy: "ast",
303
- };
304
- } finally {
305
- await removeTemporary(next);
306
- }
307
- }
301
+ async function applyAstRules(context: AstPatchContext, next: string) {
302
+ let totalMatches = 0;
303
+ for (const rule of context.astRules) {
304
+ totalMatches += await previewAstRule(context, next, rule);
305
+ await rewriteAstRule(context, next, rule);
306
+ }
307
+ return totalMatches;
308
+ }
308
309
 
309
- if (rewritable)
310
- throw new Error(
311
- "REJECTED: structurally rewritable files require patchStrategy 'ast' with astRules",
312
- );
313
- if (aiderBlocks.length === 0 || astRules.length > 0)
314
- throw new Error(
315
- "REJECTED: patchStrategy 'aider_block' requires aiderBlocks and no astRules",
316
- );
310
+ async function astPatchResult(
311
+ context: AstPatchContext,
312
+ candidate: string,
313
+ mode: number,
314
+ totalMatches: number,
315
+ ) {
316
+ if (context.request.preview)
317
+ return patchPreview(context, candidate, {
318
+ matches: totalMatches,
319
+ operations: context.astRules.length,
320
+ strategy: "ast",
321
+ });
322
+ await commit(
323
+ context.filePath,
324
+ candidate,
325
+ mode,
326
+ context.actual,
327
+ context.request.chattr,
328
+ );
329
+ const updated = await readFile(context.filePath, "utf8");
330
+ return {
331
+ engine: "ast-bro.run",
332
+ filePath: context.filePath,
333
+ matches: totalMatches,
334
+ operations: context.astRules.length,
335
+ preview: false,
336
+ sha256: sha256(updated),
337
+ strategy: "ast",
338
+ };
339
+ }
317
340
 
318
- let content = original;
319
- const methods: string[] = [];
320
- for (const block of aiderBlocks) {
321
- const result = applyAiderBlock(content, block.search, block.replace);
322
- content = result.content;
323
- methods.push(result.method);
324
- }
325
- if (request.preview)
326
- return previewResult(content, {
327
- matchMethods: methods,
328
- operations: aiderBlocks.length,
329
- strategy: "aider_block",
330
- });
341
+ async function applyAstPatch(context: PatchContext) {
342
+ validateAstStrategy(context);
343
+ const next = temporary(context.filePath);
344
+ const metadata = await lstat(context.filePath);
345
+ try {
346
+ await writeFile(next, context.original, {
347
+ encoding: "utf8",
348
+ flag: "wx",
349
+ mode: metadata.mode,
350
+ });
351
+ const totalMatches = await applyAstRules(context, next);
352
+ const candidate = await readFile(next, "utf8");
353
+ return await astPatchResult(
354
+ context,
355
+ candidate,
356
+ metadata.mode,
357
+ totalMatches,
358
+ );
359
+ } finally {
360
+ await removeTemporary(next);
361
+ }
362
+ }
331
363
 
332
- await commit(
333
- filePath,
334
- content,
335
- (await lstat(filePath)).mode,
336
- actual,
337
- request.chattr,
364
+ function validateAiderStrategy(context: PatchContext) {
365
+ if (context.rewritable)
366
+ throw new Error(
367
+ "REJECTED: structurally rewritable files require patchStrategy 'ast' with astRules",
338
368
  );
339
- const updated = await readFile(filePath, "utf8");
340
- return {
341
- filePath,
369
+ if (context.aiderBlocks.length === 0 || context.astRules.length > 0)
370
+ throw new Error(
371
+ "REJECTED: patchStrategy 'aider_block' requires aiderBlocks and no astRules",
372
+ );
373
+ }
374
+
375
+ function applyAiderBlocks(content: string, blocks: AiderBlock[]) {
376
+ const methods: string[] = [];
377
+ for (const block of blocks) {
378
+ const result = applyAiderBlock(content, block.search, block.replace);
379
+ content = result.content;
380
+ methods.push(result.method);
381
+ }
382
+ return { content, methods };
383
+ }
384
+
385
+ async function applyAiderPatch(context: PatchContext) {
386
+ validateAiderStrategy(context);
387
+ const { content, methods } = applyAiderBlocks(
388
+ context.original,
389
+ context.aiderBlocks,
390
+ );
391
+ if (context.request.preview)
392
+ return patchPreview(context, content, {
342
393
  matchMethods: methods,
343
- operations: aiderBlocks.length,
344
- preview: false,
345
- sha256: sha256(updated),
394
+ operations: context.aiderBlocks.length,
346
395
  strategy: "aider_block",
347
- };
348
- });
396
+ });
397
+ await commit(
398
+ context.filePath,
399
+ content,
400
+ (await lstat(context.filePath)).mode,
401
+ context.actual,
402
+ context.request.chattr,
403
+ );
404
+ const updated = await readFile(context.filePath, "utf8");
405
+ return {
406
+ filePath: context.filePath,
407
+ matchMethods: methods,
408
+ operations: context.aiderBlocks.length,
409
+ preview: false,
410
+ sha256: sha256(updated),
411
+ strategy: "aider_block",
412
+ };
413
+ }
414
+
415
+ async function applyLockedPatch(filePath: string, request: PatchBatchRequest) {
416
+ const context = await createPatchContext(filePath, request);
417
+ return request.patchStrategy === "ast"
418
+ ? applyAstPatch(context)
419
+ : applyAiderPatch(context);
420
+ }
421
+
422
+ async function applyPatchBatch(
423
+ inputPath: string,
424
+ request: PatchBatchRequest,
425
+ ): Promise<Record<string, unknown>> {
426
+ const filePath = await resolveWritablePath(inputPath);
427
+ return withFileLock(filePath, () => applyLockedPatch(filePath, request));
349
428
  }
350
429
 
351
430
  export async function patchFile(
@@ -360,20 +439,28 @@ export async function patchFile(
360
439
  });
361
440
  }
362
441
 
363
- export async function patchFiles(
364
- requests: PatchBatch,
442
+ async function processFileBatch<T>(
443
+ tool: "file_patch" | "file_write",
444
+ requests: Record<string, T>,
445
+ handler: (filePath: string, request: T) => Promise<Record<string, unknown>>,
365
446
  ): Promise<Record<string, unknown>> {
366
447
  const entries = Object.entries(requests);
367
448
  if (entries.length < 1 || entries.length > FILE_READ_MAX_BATCH)
368
449
  throw new Error(
369
- `file_patch requires between 1 and ${FILE_READ_MAX_BATCH} files`,
450
+ `${tool} requires between 1 and ${FILE_READ_MAX_BATCH} files`,
370
451
  );
371
452
  const files: Record<string, unknown> = {};
372
453
  for (const [filePath, request] of entries)
373
- files[filePath] = await applyPatchBatch(filePath, request);
454
+ files[filePath] = await handler(filePath, request);
374
455
  return { files };
375
456
  }
376
457
 
458
+ export async function patchFiles(
459
+ requests: PatchBatch,
460
+ ): Promise<Record<string, unknown>> {
461
+ return processFileBatch("file_patch", requests, applyPatchBatch);
462
+ }
463
+
377
464
  export async function writeFileSafely(args: {
378
465
  chattr?: FileChattr;
379
466
  filePath: string;
@@ -385,16 +472,9 @@ export async function writeFileSafely(args: {
385
472
  const existing = await readFile(filePath, "utf8").catch(() => undefined);
386
473
  let createdDirectories: string[];
387
474
  if (existing !== undefined) {
388
- if (!args.expectedSha256)
389
- throw new Error("Overwriting an existing file requires expectedSha256");
475
+ await requireExpectedHash(args.expectedSha256, "file_write overwrite");
390
476
  const actual = sha256(existing);
391
- if (actual !== args.expectedSha256)
392
- throw new Error(
393
- "Stale file context: expected " +
394
- args.expectedSha256 +
395
- ", found " +
396
- actual,
397
- );
477
+ verifyExpectedHash(args.expectedSha256, actual);
398
478
  const language = detectAstLanguage(filePath);
399
479
  if (await astRewritable(filePath, language))
400
480
  throw new Error(
@@ -429,18 +509,12 @@ export async function writeFileSafely(args: {
429
509
  export async function writeFilesSafely(
430
510
  requests: FileWriteBatch,
431
511
  ): Promise<Record<string, unknown>> {
432
- const entries = Object.entries(requests);
433
- if (entries.length < 1 || entries.length > FILE_READ_MAX_BATCH)
434
- throw new Error(
435
- `file_write requires between 1 and ${FILE_READ_MAX_BATCH} files`,
436
- );
437
- const files: Record<string, unknown> = {};
438
- for (const [filePath, request] of entries)
439
- files[filePath] = await writeFileSafely({
512
+ return processFileBatch("file_write", requests, (filePath, request) =>
513
+ writeFileSafely({
440
514
  chattr: request.chattr,
441
515
  content: request.content,
442
516
  expectedSha256: request.expectedSha256,
443
517
  filePath,
444
- });
445
- return { files };
518
+ }),
519
+ );
446
520
  }
@@ -2,8 +2,9 @@ import { accessSync, constants, readFileSync } from "node:fs";
2
2
  import { createRequire } from "node:module";
3
3
  import os from "node:os";
4
4
  import path from "node:path";
5
+ import { currentConfig } from "../config";
5
6
 
6
- export const PACKAGE_ROOT = path.resolve(
7
+ const PACKAGE_ROOT = path.resolve(
7
8
  import.meta.dir,
8
9
  path.basename(import.meta.dir) === "dist" ? ".." : "../..",
9
10
  );
@@ -97,50 +98,60 @@ function globalBinDirectories(binaryName: string, platform: NodeJS.Platform) {
97
98
  return [...directories];
98
99
  }
99
100
 
100
- export function resolveDependencyBinary(
101
- binaryName: string,
102
- packageName = binaryName,
103
- options: BinaryResolutionOptions = {},
104
- ) {
105
- const platform = options.platform ?? process.platform;
106
- const packageRoot = options.packageRoot ?? PACKAGE_ROOT;
107
- const names = executableNames(binaryName, platform);
108
- const candidates: string[] = [];
101
+ function executableCandidate(candidates: string[], platform: NodeJS.Platform) {
102
+ return candidates.find((candidate) => isExecutable(candidate, platform));
103
+ }
109
104
 
105
+ function ancestorBinaryCandidates(packageRoot: string, names: string[]) {
106
+ const candidates: string[] = [];
110
107
  for (
111
108
  let current = packageRoot;
112
109
  path.dirname(current) !== current;
113
110
  current = path.dirname(current)
114
- )
111
+ ) {
115
112
  for (const name of names)
116
113
  candidates.push(path.join(current, "node_modules/.bin", name));
114
+ }
115
+ return candidates;
116
+ }
117
+
118
+ function directoryBinaryCandidates(directories: string[], names: string[]) {
119
+ const candidates: string[] = [];
120
+ for (const directory of directories) {
121
+ if (!directory) continue;
122
+ for (const name of names) candidates.push(path.join(directory, name));
123
+ }
124
+ return candidates;
125
+ }
117
126
 
127
+ export function resolveDependencyBinary(
128
+ binaryName: string,
129
+ packageName = binaryName,
130
+ options: BinaryResolutionOptions = {},
131
+ ) {
132
+ const platform = options.platform ?? process.platform;
133
+ const packageRoot = options.packageRoot ?? PACKAGE_ROOT;
134
+ const names = executableNames(binaryName, platform);
135
+ const localCandidates = ancestorBinaryCandidates(packageRoot, names);
118
136
  const directPackageBinary =
119
137
  options.packageBinary ?? packageBinary(packageName, binaryName);
120
- if (directPackageBinary) candidates.push(directPackageBinary);
121
- const local = candidates.find((candidate) =>
122
- isExecutable(candidate, platform),
123
- );
138
+ if (directPackageBinary) localCandidates.push(directPackageBinary);
139
+ const local = executableCandidate(localCandidates, platform);
124
140
  if (local) return local;
125
-
126
141
  const globalDirectories =
127
142
  options.globalBinDirectories ?? globalBinDirectories(binaryName, platform);
128
- candidates.length = 0;
129
- for (const directory of globalDirectories)
130
- for (const name of names) candidates.push(path.join(directory, name));
131
- const global = candidates.find((candidate) =>
132
- isExecutable(candidate, platform),
143
+ const global = executableCandidate(
144
+ directoryBinaryCandidates(globalDirectories, names),
145
+ platform,
133
146
  );
134
147
  if (global) return global;
135
-
136
- candidates.length = 0;
137
- for (const directory of (options.pathValue ?? process.env.PATH ?? "").split(
148
+ const pathDirectories = (options.pathValue ?? process.env.PATH ?? "").split(
138
149
  path.delimiter,
139
- ))
140
- if (directory)
141
- for (const name of names) candidates.push(path.join(directory, name));
142
-
143
- return candidates.find((candidate) => isExecutable(candidate, platform));
150
+ );
151
+ return executableCandidate(
152
+ directoryBinaryCandidates(pathDirectories, names),
153
+ platform,
154
+ );
144
155
  }
145
156
 
146
157
  export const AST_BRO_VERSION = "3.0.0";
@@ -186,3 +197,11 @@ export const DPRINT_BINARY =
186
197
  process.env.DPRINT_BINARY ??
187
198
  resolveDependencyBinary("dprint", "dprint") ??
188
199
  "dprint";
200
+
201
+ export async function configuredAstBroBinary(): Promise<string> {
202
+ return (await currentConfig()).dependencies.astBroBinary ?? AST_BRO_BINARY;
203
+ }
204
+
205
+ export async function configuredDprintBinary(): Promise<string> {
206
+ return (await currentConfig()).dependencies.dprintBinary ?? DPRINT_BINARY;
207
+ }