Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"build": "tsc -p tsconfig.json",
"test": "node --test test/*.test.mjs"
},
"dependencies": {
"agent-gov-core": "github:Conalh/agent-gov-core#v0.1.2"

Check warning on line 14 in package.json

View workflow job for this annotation

GitHub Actions / scope-review

TaskBound medium scope creep

Added dependency agent-gov-core@github:Conalh/agent-gov-core#v0.1.2. Recommendation: Confirm the dependency is required for the stated task.
},
"devDependencies": {
"@types/node": "^24.0.0",
"typescript": "^5.9.3"
Expand Down
22 changes: 8 additions & 14 deletions src/discovery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import {
lineOfJsonKey as coreLineOfJsonKey,
lineOfJsonStringValue as coreLineOfJsonStringValue,
} from 'agent-gov-core';

export async function readJsonObject(path: string): Promise<Record<string, unknown>> {
return (await readJsonObjectWithSource(path)).json;
Expand Down Expand Up @@ -33,23 +37,13 @@ export function isRecord(value: unknown): value is Record<string, unknown> {
}

export function lineOfJsonKey(text: string, key: string): number | undefined {
const keyPattern = new RegExp(`"${escapeRegExp(key)}"\\s*:`);
return lineOfPattern(text, keyPattern);
const line = coreLineOfJsonKey(text, key);
return line === 0 ? undefined : line;
}

export function lineOfJsonStringValue(text: string, value: string): number | undefined {
const encoded = JSON.stringify(value);
return lineOfPattern(text, new RegExp(escapeRegExp(encoded)));
}

function lineOfPattern(text: string, pattern: RegExp): number | undefined {
const lines = text.split(/\r?\n/);
const index = lines.findIndex((line) => pattern.test(line));
return index === -1 ? undefined : index + 1;
}

function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const line = coreLineOfJsonStringValue(text, value);
return line === 0 ? undefined : line;
}

function isNodeError(error: unknown): error is NodeJS.ErrnoException {
Expand Down