readfirst 0.7.0 → 0.8.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.
- package/README.md +31 -0
- package/package.json +1 -1
- package/src/action.mjs +40 -3
- package/src/cli.mjs +78 -8
- package/src/imports.mjs +120 -5
package/README.md
CHANGED
|
@@ -50,6 +50,18 @@ npx readfirst hook --install
|
|
|
50
50
|
adds a hook to `.claude/settings.json`. Before each Edit or Write, Claude Code gets the rules, risks and checks for that
|
|
51
51
|
file as context, once per file per session. Nothing to remember, and the edit is never blocked.
|
|
52
52
|
|
|
53
|
+
Want the checks enforced, not just suggested?
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
npx readfirst hook --install --verify
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
also adds a Stop hook: before Claude Code finishes its turn, it runs the map's checks for the files it edited in that
|
|
60
|
+
session. If one fails, Claude Code cannot finish yet and gets the failure to fix. It gets one try per turn, so a check
|
|
61
|
+
it cannot fix never traps the session. It runs only check commands that the map on your main branch already lists: a
|
|
62
|
+
command someone's branch adds or changes is named, not run, until it is merged (`npx readfirst verify` runs it when you
|
|
63
|
+
choose to).
|
|
64
|
+
|
|
53
65
|
### See the map
|
|
54
66
|
|
|
55
67
|
```sh
|
|
@@ -121,10 +133,29 @@ lists what `npx readfirst fix` would change.
|
|
|
121
133
|
| `fail-on-map-problems` | `true` | `false` reports map problems without failing |
|
|
122
134
|
| `strict` | `false` | `true` also fails on new files that are only placed by their neighbours |
|
|
123
135
|
| `comment` | `true` | `false` writes only the job summary |
|
|
136
|
+
| `auto-fix` | `false` | `true` commits what `readfirst fix` settles (new files placed by their neighbours) to the PR branch |
|
|
124
137
|
| `limit` | `30` | most tests and users listed per section |
|
|
125
138
|
|
|
126
139
|
On a PR from a fork, the token is read-only. The list then goes to the job summary instead of a comment.
|
|
127
140
|
|
|
141
|
+
### Keep the map in step without thinking about it
|
|
142
|
+
|
|
143
|
+
```yaml
|
|
144
|
+
permissions:
|
|
145
|
+
contents: write # for auto-fix
|
|
146
|
+
pull-requests: write
|
|
147
|
+
# ...
|
|
148
|
+
- uses: jeonghun917/readfirst@v0
|
|
149
|
+
with:
|
|
150
|
+
auto-fix: true
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
With `auto-fix`, when a PR adds files next to ones the map already places, the Action writes them into the map and
|
|
154
|
+
commits that to the PR branch. That is the mechanical part `npx readfirst fix` does, nothing else: new risks and
|
|
155
|
+
rules stay for a person. It skips PRs from forks, and it skips the commit if the branch's map changed since the run
|
|
156
|
+
started, so a push is never overwritten. A commit made with the default `GITHUB_TOKEN` does not start new workflow
|
|
157
|
+
runs. If your branch protection requires checks, pass a GitHub App or personal token as `token`.
|
|
158
|
+
|
|
128
159
|
## The map
|
|
129
160
|
|
|
130
161
|
```json
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "readfirst",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"mcpName": "io.github.jeonghun917/readfirst",
|
|
5
5
|
"description": "What to read and check before you (or your AI agent) change a file: project rules, risks and tests tied to code.",
|
|
6
6
|
"type": "module",
|
package/src/action.mjs
CHANGED
|
@@ -30,12 +30,14 @@ async function pages(fetchImpl, env, path) {
|
|
|
30
30
|
return all;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
export function commentBody({ list, problems, removed, commit, fixes = [], settled = [] }) {
|
|
33
|
+
export function commentBody({ list, problems, removed, commit, fixes = [], settled = [], committed = null }) {
|
|
34
34
|
const parts = [MARKER, '## readfirst', ''];
|
|
35
35
|
if (problems.length) {
|
|
36
36
|
parts.push(`> [!WARNING]\n> \`${MAP_PATH}\` no longer matches the code (${problems.length} problem${problems.length > 1 ? 's' : ''}). Run \`npx readfirst check\` and fix the map in this PR.`, '');
|
|
37
37
|
parts.push(...problems.slice(0, 20).map((p) => `- ${p}`), ...(problems.length > 20 ? [`- …and ${problems.length - 20} more`] : []), '');
|
|
38
38
|
if (fixes.length) parts.push('`npx readfirst fix` would make these changes:', ...fixes.slice(0, 10).map((f) => `- ${f}`), ...(fixes.length > 10 ? [`- …and ${fixes.length - 10} more`] : []), '');
|
|
39
|
+
} else if (committed) {
|
|
40
|
+
parts.push(`<details><summary>readfirst placed ${settled.length} new file(s) in the map and committed it to this branch (${committed.slice(0, 7)})</summary>`, '', ...settled.slice(0, 20).map((f) => `- ${f}`), ...(settled.length > 20 ? [`- …and ${settled.length - 20} more`] : []), '', 'Pull before your next push.', '</details>', '');
|
|
39
41
|
} else if (settled.length) {
|
|
40
42
|
parts.push(`<details><summary>New files placed by their neighbours (${settled.length})</summary>`, '', ...settled.slice(0, 20).map((f) => `- ${f}`), ...(settled.length > 20 ? [`- …and ${settled.length - 20} more`] : []), '', '`npx readfirst fix` writes this into the map; nothing to do for this PR.', '</details>', '');
|
|
41
43
|
}
|
|
@@ -72,13 +74,16 @@ export async function runAction({ env = process.env, fetchImpl = fetch, root = e
|
|
|
72
74
|
}
|
|
73
75
|
const repo = env.GITHUB_REPOSITORY;
|
|
74
76
|
const changed = await pages(fetchImpl, env, `/repos/${repo}/pulls/${pr.number}/files`);
|
|
77
|
+
const committed = (env['INPUT_AUTO-FIX'] ?? env.INPUT_AUTO_FIX ?? 'false') === 'true' && settled.length
|
|
78
|
+
? await commitFix({ fetchImpl, env, repo, pr, raw, fixed: map, settled, log })
|
|
79
|
+
: null;
|
|
75
80
|
const removed = changed.filter((f) => f.status === 'removed').map((f) => f.filename);
|
|
76
81
|
const targets = changed.filter((f) => f.status !== 'removed').map((f) => f.filename);
|
|
77
82
|
// A broken map still gets a comment (the problems), but no read list from it.
|
|
78
83
|
const list = targets.length && !problems.length
|
|
79
84
|
? readList({ map, files, imports: importEdges(files, readText(root)), targets, commit: pr.head?.sha ?? null, limit: Number(env.INPUT_LIMIT || 30) })
|
|
80
85
|
: null;
|
|
81
|
-
const body = commentBody({ list, problems, removed, commit: pr.head?.sha ?? null, fixes, settled });
|
|
86
|
+
const body = commentBody({ list, problems, removed, commit: pr.head?.sha ?? null, fixes, settled, committed });
|
|
82
87
|
summary(body);
|
|
83
88
|
|
|
84
89
|
if ((env.INPUT_COMMENT ?? 'true') !== 'false') {
|
|
@@ -95,7 +100,39 @@ export async function runAction({ env = process.env, fetchImpl = fetch, root = e
|
|
|
95
100
|
}
|
|
96
101
|
}
|
|
97
102
|
if (problems.length) log(`${MAP_PATH} has ${problems.length} problem(s):\n- ${problems.join('\n- ')}`);
|
|
98
|
-
return { ok: !(problems.length && failOnProblems), problems, list, body };
|
|
103
|
+
return { ok: !(problems.length && failOnProblems), problems, list, body, committed };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// auto-fix: write what `readfirst fix` settles (new files placed by their neighbours, patterns for deleted files) into
|
|
107
|
+
// the map on the PR's branch, as one commit through the contents API. Only the mechanical part, never anything a person
|
|
108
|
+
// has to decide; only on a branch of this repository; only when the branch's map is the one this run read, so a push in
|
|
109
|
+
// between is never overwritten. Returns the new commit's sha, or null with a warning.
|
|
110
|
+
async function commitFix({ fetchImpl, env, repo, pr, raw, fixed, settled, log }) {
|
|
111
|
+
if (pr.head?.repo?.full_name !== repo || !pr.head?.ref) {
|
|
112
|
+
log('::warning::readfirst auto-fix: this PR comes from a fork, so the map update stays a suggestion in the comment.');
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
const path = MAP_PATH.split('/').map(encodeURIComponent).join('/');
|
|
116
|
+
try {
|
|
117
|
+
const now = await api(fetchImpl, env, 'GET', `/repos/${repo}/contents/${path}?ref=${encodeURIComponent(pr.head.ref)}`);
|
|
118
|
+
let onBranch = null;
|
|
119
|
+
try { onBranch = JSON.parse(Buffer.from(now.content ?? '', 'base64').toString('utf8')); } catch { onBranch = null; }
|
|
120
|
+
if (JSON.stringify(onBranch) !== JSON.stringify(raw)) {
|
|
121
|
+
log('::warning::readfirst auto-fix: the map on the branch differs from the one this run read (a newer push, or the base changed it); nothing committed.');
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
const res = await api(fetchImpl, env, 'PUT', `/repos/${repo}/contents/${path}`, {
|
|
125
|
+
message: `readfirst fix: place ${settled.length} new file(s) in the map`,
|
|
126
|
+
content: Buffer.from(`${JSON.stringify(fixed, null, 2)}\n`, 'utf8').toString('base64'),
|
|
127
|
+
sha: now.sha,
|
|
128
|
+
branch: pr.head.ref,
|
|
129
|
+
});
|
|
130
|
+
log(`readfirst auto-fix: committed the map update to ${pr.head.ref}`);
|
|
131
|
+
return res?.commit?.sha ?? 'committed';
|
|
132
|
+
} catch (e) {
|
|
133
|
+
log(`::warning::readfirst auto-fix could not commit (${/: 403 /u.test(e.message) ? 'the token needs contents: write' : e.message.slice(0, 200)}); the comment says what \`npx readfirst fix\` would change.`);
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
99
136
|
}
|
|
100
137
|
|
|
101
138
|
if (process.argv[1]?.endsWith('/src/action.mjs')) {
|
package/src/cli.mjs
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
// readfirst diff [--base <ref>] the read list for the files changed since <ref> (default: origin/main, else main)
|
|
9
9
|
// readfirst verify [--base <ref>] run the checks the read list names for the changed files; exit 1 if one fails
|
|
10
10
|
// readfirst hook [--install] a Claude Code PreToolUse hook: the read list for the file an Edit or Write is about to change
|
|
11
|
+
// (--install --verify also adds a Stop hook, `hook --stop`, that runs verify before Claude Code finishes)
|
|
11
12
|
// readfirst mcp an MCP server on stdio: read_list, changed_read_list and check_map as tools for any agent
|
|
12
13
|
// readfirst view [--out <file>] the map as a city in one HTML page (default .readfirst/local/view.html)
|
|
13
14
|
// Options: --root <dir> (default: the current git repository), --json, --limit <n>.
|
|
@@ -33,7 +34,8 @@ const HELP = `readfirst — what to read and check before changing a file
|
|
|
33
34
|
readfirst read <file>... read list for files you are about to change
|
|
34
35
|
readfirst diff [--base <ref>] read list for files changed since <ref>
|
|
35
36
|
readfirst verify [--base <ref>] run the checks for the files changed since <ref> (--dry-run lists them)
|
|
36
|
-
readfirst hook [--install] Claude Code hook: the read list before each edit (--install adds it to .claude/settings.json
|
|
37
|
+
readfirst hook [--install] Claude Code hook: the read list before each edit (--install adds it to .claude/settings.json;
|
|
38
|
+
--install --verify also runs the checks for edited files before Claude Code finishes)
|
|
37
39
|
readfirst view [--out <file>] the map as a city in one HTML page
|
|
38
40
|
readfirst mcp MCP server on stdio for Cursor, Codex, Windsurf and other agents
|
|
39
41
|
readfirst prompt --labels <lang> brief for your agent to write personal labels for the view (e.g. ko, en)
|
|
@@ -51,6 +53,8 @@ function parse(argv) {
|
|
|
51
53
|
else if (a === '--install') args.install = true;
|
|
52
54
|
else if (a === '--dry-run') args.dryRun = true;
|
|
53
55
|
else if (a === '--strict') args.strict = true;
|
|
56
|
+
else if (a === '--verify') args.verify = true;
|
|
57
|
+
else if (a === '--stop') args.stop = true;
|
|
54
58
|
else if (a === '-h' || a === '--help') args.help = true;
|
|
55
59
|
else if (a.startsWith('--')) { const v = argv[++i]; if (v === undefined) throw new Error(`${a} needs a value`); args[a.slice(2)] = v; }
|
|
56
60
|
else args._.push(a);
|
|
@@ -164,6 +168,9 @@ export function brief(list) {
|
|
|
164
168
|
return lines;
|
|
165
169
|
}
|
|
166
170
|
|
|
171
|
+
// Which files the PreToolUse hook has seen in a session: the Stop hook verifies those.
|
|
172
|
+
const seenFile = (stateDir, session) => join(stateDir, `readfirst-hook-${String(session ?? 'none').replace(/[^\w-]/gu, '')}.json`);
|
|
173
|
+
|
|
167
174
|
// Claude Code PreToolUse hook. Reads the hook's JSON on stdin; for an Edit, Write or MultiEdit of a file the map covers,
|
|
168
175
|
// prints the read list as additionalContext, once per file per session. Never blocks the edit: any problem means no output.
|
|
169
176
|
export function runHook(input, { now = Date.now(), stateDir = tmpdir() } = {}) {
|
|
@@ -175,7 +182,7 @@ export function runHook(input, { now = Date.now(), stateDir = tmpdir() } = {}) {
|
|
|
175
182
|
const root = git(cwd, 'rev-parse', '--show-toplevel').trim();
|
|
176
183
|
const path = (isAbsolute(file) ? relative(root, file) : file).replace(/\\/gu, '/');
|
|
177
184
|
if (path.startsWith('..') || path === MAP_PATH || !existsSync(join(root, MAP_PATH))) return null;
|
|
178
|
-
const seenAt =
|
|
185
|
+
const seenAt = seenFile(stateDir, event.session_id);
|
|
179
186
|
let seen = {};
|
|
180
187
|
try { seen = JSON.parse(readFileSync(seenAt, 'utf8')); } catch { seen = {}; }
|
|
181
188
|
if (seen[path] && now - seen[path] < 6 * 3600_000) return null;
|
|
@@ -195,17 +202,78 @@ export function runHook(input, { now = Date.now(), stateDir = tmpdir() } = {}) {
|
|
|
195
202
|
}
|
|
196
203
|
}
|
|
197
204
|
|
|
205
|
+
// The check commands of the map as the base branch has it. The Stop hook runs a command without anyone asking, so it
|
|
206
|
+
// runs only these: a command a branch adds or changes in the map is repository content nobody has merged yet.
|
|
207
|
+
function trustedRuns(root) {
|
|
208
|
+
try {
|
|
209
|
+
const { ref } = changedFiles(root);
|
|
210
|
+
const since = git(root, 'merge-base', ref, 'HEAD').trim();
|
|
211
|
+
const map = JSON.parse(git(root, 'show', `${since}:${MAP_PATH}`));
|
|
212
|
+
return new Set([...(map.groups ?? []), ...(map.rules ?? [])].flatMap((x) => (x.checks ?? []).map((c) => c.run)));
|
|
213
|
+
} catch {
|
|
214
|
+
return new Set();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Claude Code Stop hook (opt-in, `hook --install --verify`): before Claude Code ends its turn, run the map's checks for the
|
|
219
|
+
// files it edited in this session that are still changed. A failing check blocks the stop once, with the failure as the
|
|
220
|
+
// reason, so the agent fixes it; on the second stop of the same turn it lets go, so a check it cannot fix never traps
|
|
221
|
+
// the session. Only commands the base branch's map already lists run; others are named for `readfirst verify`, which
|
|
222
|
+
// someone runs on purpose. Nothing edited, nothing changed or no checks: no output.
|
|
223
|
+
export function runStopHook(input, { stateDir = tmpdir(), timeout = 600 } = {}) {
|
|
224
|
+
let event;
|
|
225
|
+
try { event = JSON.parse(input); } catch { return null; }
|
|
226
|
+
if (event.stop_hook_active) return null;
|
|
227
|
+
try {
|
|
228
|
+
const root = git(event.cwd ?? process.cwd(), 'rev-parse', '--show-toplevel').trim();
|
|
229
|
+
if (!existsSync(join(root, MAP_PATH))) return null;
|
|
230
|
+
let seen = {};
|
|
231
|
+
try { seen = JSON.parse(readFileSync(seenFile(stateDir, event.session_id), 'utf8')); } catch { return null; }
|
|
232
|
+
const dirty = new Set(git(root, 'status', '--porcelain', '-z', '--untracked-files=all').split('\0').filter(Boolean).map((l) => l.slice(3)));
|
|
233
|
+
let changed = [];
|
|
234
|
+
try { changed = changedFiles(root).files; } catch { /* no base branch: the working tree is enough */ }
|
|
235
|
+
const targets = Object.keys(seen).filter((f) => dirty.has(f) || changed.includes(f)).sort();
|
|
236
|
+
if (!targets.length) return null;
|
|
237
|
+
const files = repoFiles(root);
|
|
238
|
+
const map = readMapFile(root);
|
|
239
|
+
if (mapProblems(map, files, { placement: false }).length) return null;
|
|
240
|
+
const list = readList({ map, files, imports: importEdges(files, readText(root)), targets, limit: 1 });
|
|
241
|
+
if (!list.checks.length) return null;
|
|
242
|
+
const trusted = trustedRuns(root);
|
|
243
|
+
const skipped = list.checks.filter((c) => !trusted.has(c.run)).map((c) => c.run);
|
|
244
|
+
const result = verifyChecks(root, list.checks.filter((c) => trusted.has(c.run)), { timeout });
|
|
245
|
+
const failed = result.filter((c) => c.status !== 'pass');
|
|
246
|
+
const notRun = skipped.length ? ` Not run, because the base branch's map does not list them yet: ${skipped.map((r) => `\`${r}\``).join(', ')}; run \`npx readfirst verify\` to run them on purpose.` : '';
|
|
247
|
+
if (!failed.length) return JSON.stringify({ systemMessage: `readfirst verify: ${result.length} check(s) passed for ${targets.length} edited file(s).${notRun}` });
|
|
248
|
+
const reason = [
|
|
249
|
+
`readfirst verify: ${failed.length} of ${result.length} check(s) the map lists for the files you edited failed. Fix them before you finish.${notRun}`,
|
|
250
|
+
...failed.map((c) => `\n--- ${c.run} (${c.status}${c.code != null ? `, exit ${c.code}` : ''})${c.why ? ` covers ${c.why}` : ''}; last lines:\n${c.output ?? ''}`),
|
|
251
|
+
].join('\n');
|
|
252
|
+
return JSON.stringify({ decision: 'block', reason });
|
|
253
|
+
} catch {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
198
258
|
export const HOOK_COMMAND = 'npx --yes readfirst hook';
|
|
199
|
-
|
|
200
|
-
|
|
259
|
+
export const STOP_HOOK_COMMAND = 'npx --yes readfirst hook --stop';
|
|
260
|
+
// Adds the hook to the project's .claude/settings.json, keeping everything already there. With verify, also the Stop
|
|
261
|
+
// hook that runs the checks before Claude Code finishes.
|
|
262
|
+
export function installHook(root, { verify = false } = {}) {
|
|
201
263
|
const at = join(root, '.claude', 'settings.json');
|
|
202
264
|
let settings = {};
|
|
203
265
|
if (existsSync(at)) {
|
|
204
266
|
try { settings = JSON.parse(readFileSync(at, 'utf8')); } catch { throw new Error('.claude/settings.json is not valid JSON; fix it first'); }
|
|
205
267
|
}
|
|
206
268
|
const pre = ((settings.hooks ??= {}).PreToolUse ??= []);
|
|
207
|
-
|
|
208
|
-
|
|
269
|
+
const has = (list) => list.some((m) => (m.hooks ?? []).some((h) => /readfirst hook/u.test(h.command ?? '')));
|
|
270
|
+
let added = false;
|
|
271
|
+
if (!has(pre)) { pre.push({ matcher: 'Edit|Write|MultiEdit', hooks: [{ type: 'command', command: HOOK_COMMAND, timeout: 15 }] }); added = true; }
|
|
272
|
+
if (verify) {
|
|
273
|
+
const stop = (settings.hooks.Stop ??= []);
|
|
274
|
+
if (!has(stop)) { stop.push({ hooks: [{ type: 'command', command: STOP_HOOK_COMMAND, timeout: 1800 }] }); added = true; }
|
|
275
|
+
}
|
|
276
|
+
if (!added) return false;
|
|
209
277
|
mkdirSync(join(root, '.claude'), { recursive: true });
|
|
210
278
|
writeFileSync(at, `${JSON.stringify(settings, null, 2)}\n`);
|
|
211
279
|
return true;
|
|
@@ -216,7 +284,7 @@ export function main(argv, io = { out: (s) => process.stdout.write(s), err: (s)
|
|
|
216
284
|
const [cmd, ...rest] = args._;
|
|
217
285
|
if (args.help || !cmd) { io.out(`${HELP}\n`); return cmd || args.help ? 0 : 2; }
|
|
218
286
|
if (cmd === 'hook' && !args.install) {
|
|
219
|
-
const out = runHook(stdin());
|
|
287
|
+
const out = args.stop ? runStopHook(stdin()) : runHook(stdin());
|
|
220
288
|
if (out) io.out(`${out}\n`);
|
|
221
289
|
return 0;
|
|
222
290
|
}
|
|
@@ -294,7 +362,9 @@ export function main(argv, io = { out: (s) => process.stdout.write(s), err: (s)
|
|
|
294
362
|
return problems.length ? 1 : 0;
|
|
295
363
|
}
|
|
296
364
|
if (cmd === 'hook') {
|
|
297
|
-
io.out(installHook(root
|
|
365
|
+
io.out(installHook(root, { verify: args.verify })
|
|
366
|
+
? `added the readfirst hook to .claude/settings.json: Claude Code now sees the read list before each edit${args.verify ? ', and cannot finish while a check for a file it edited fails (it gets one try to fix it)' : ''}.\n`
|
|
367
|
+
: '.claude/settings.json already has the readfirst hook.\n');
|
|
298
368
|
return 0;
|
|
299
369
|
}
|
|
300
370
|
if (cmd === 'fix') {
|
package/src/imports.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
// Which tracked file imports which, read from the code. Only imports that resolve to a tracked file count: packages
|
|
2
|
-
// unresolved specifiers are left out. JavaScript/TypeScript (import, export from, import(),
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// Which tracked file imports which, read from the code. Only imports that resolve to a tracked file count: packages from
|
|
2
|
+
// outside the repository and unresolved specifiers are left out. JavaScript/TypeScript (import, export from, import(),
|
|
3
|
+
// require; relative paths, the repository's own workspace packages and tsconfig/jsconfig "paths" aliases) and Python
|
|
4
|
+
// (import a.b, from a.b import c, relative from . imports, and "a.b.c" strings that name a module of the repository,
|
|
5
|
+
// as hooks and settings do) are read here; Go, Rust, Elixir, C#, Java/Kotlin, Ruby and PHP in langs.mjs.
|
|
5
6
|
import { posix } from 'node:path';
|
|
6
7
|
import { moreEdges } from './langs.mjs';
|
|
7
8
|
|
|
@@ -13,6 +14,87 @@ const PY_IMPORT = /^[ \t]*(?:from[ \t]+(\.*[\w.]*)[ \t]+import[ \t]+(\([^)]*\)|[
|
|
|
13
14
|
const JS_EXT = ['', '.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '.mts', '.vue', '.svelte', '/index.js', '/index.ts', '/index.tsx', '/index.mjs'];
|
|
14
15
|
|
|
15
16
|
const lineAt = (body, index) => body.slice(0, index).split('\n').length;
|
|
17
|
+
const dirOf = (f) => (posix.dirname(f) === '.' ? '' : posix.dirname(f));
|
|
18
|
+
const within = (dir, file) => dir === '' || file.startsWith(`${dir}/`);
|
|
19
|
+
|
|
20
|
+
// tsconfig.json allows comments and trailing commas.
|
|
21
|
+
export function parseJsonc(text) {
|
|
22
|
+
if (text === null || text === undefined) return null;
|
|
23
|
+
let out = '';
|
|
24
|
+
for (let i = 0; i < text.length; i++) {
|
|
25
|
+
const c = text[i];
|
|
26
|
+
if (c === '"') {
|
|
27
|
+
const end = text.slice(i).match(/^"(?:[^"\\\n]|\\.)*"/u);
|
|
28
|
+
if (!end) return null;
|
|
29
|
+
out += end[0]; i += end[0].length - 1;
|
|
30
|
+
} else if (c === '/' && text[i + 1] === '/') {
|
|
31
|
+
while (i < text.length && text[i] !== '\n') i++;
|
|
32
|
+
out += '\n';
|
|
33
|
+
} else if (c === '/' && text[i + 1] === '*') {
|
|
34
|
+
const close = text.indexOf('*/', i + 2);
|
|
35
|
+
if (close < 0) return null;
|
|
36
|
+
i = close + 1;
|
|
37
|
+
} else out += c;
|
|
38
|
+
}
|
|
39
|
+
try { return JSON.parse(out.replace(/,(\s*[}\]])/gu, '$1')); } catch { return null; }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Bare specifiers that point back into the repository: a workspace package by the name in its package.json
|
|
43
|
+
// ("@acme/db", "@acme/db/schema"), or a "paths" alias from the tsconfig/jsconfig that covers the importing file ("@/lib/x").
|
|
44
|
+
function jsResolver(files, tracked, read) {
|
|
45
|
+
const jsCandidates = (base) => [...JS_EXT.map((e) => base + e), base.replace(/\.(m|c)?js$/u, '.$1ts'), base.replace(/\.js$/u, '.tsx')];
|
|
46
|
+
const firstTracked = (bases) => {
|
|
47
|
+
for (const b of bases) {
|
|
48
|
+
const hit = jsCandidates(posix.normalize(b)).find((c) => tracked.has(c));
|
|
49
|
+
if (hit) return hit;
|
|
50
|
+
}
|
|
51
|
+
return undefined;
|
|
52
|
+
};
|
|
53
|
+
const pkgs = [];
|
|
54
|
+
for (const f of files) {
|
|
55
|
+
if (!/(?:^|\/)package\.json$/u.test(f) || f.includes('node_modules/')) continue;
|
|
56
|
+
let j;
|
|
57
|
+
try { j = JSON.parse(read(f) ?? ''); } catch { continue; }
|
|
58
|
+
if (typeof j?.name !== 'string' || !j.name || dirOf(f) === '') continue;
|
|
59
|
+
const dot = typeof j.exports === 'string' ? j.exports : j.exports?.['.'];
|
|
60
|
+
const exp = typeof dot === 'string' ? [dot] : dot && typeof dot === 'object' ? ['types', 'import', 'default', 'require'].map((k) => dot[k]).filter((v) => typeof v === 'string') : [];
|
|
61
|
+
// An entry pointing at build output (dist/index.js) is read as the source next to it (src/index.ts).
|
|
62
|
+
const entries = [j.source, ...exp, j.types, j.typings, j.module, j.main].filter((v) => typeof v === 'string')
|
|
63
|
+
.flatMap((e) => [e, e.replace(/^\.?\/?(?:dist|build|lib|out)\//u, 'src/').replace(/\.d\.ts$/u, '.ts')]);
|
|
64
|
+
pkgs.push({ name: j.name, dir: dirOf(f), entries: [...entries, 'src/index', 'index'] });
|
|
65
|
+
}
|
|
66
|
+
pkgs.sort((a, b) => b.name.length - a.name.length);
|
|
67
|
+
const aliases = [];
|
|
68
|
+
for (const f of files) {
|
|
69
|
+
if (!/(?:^|\/)(?:tsconfig|jsconfig)(?:\.[\w-]+)?\.json$/u.test(f) || f.includes('node_modules/')) continue;
|
|
70
|
+
const opts = parseJsonc(read(f))?.compilerOptions;
|
|
71
|
+
if (!opts?.paths || typeof opts.paths !== 'object') continue;
|
|
72
|
+
const base = posix.join(dirOf(f), opts.baseUrl ?? '.');
|
|
73
|
+
for (const [pattern, targets] of Object.entries(opts.paths)) {
|
|
74
|
+
if (Array.isArray(targets)) aliases.push({ scope: dirOf(f), pattern, targets: targets.filter((t) => typeof t === 'string').map((t) => posix.join(base, t)) });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
// The nearest config wins, as it would for the compiler.
|
|
78
|
+
aliases.sort((a, b) => b.scope.length - a.scope.length);
|
|
79
|
+
return (from, spec) => {
|
|
80
|
+
for (const a of aliases) {
|
|
81
|
+
if (!within(a.scope, from)) continue;
|
|
82
|
+
const star = a.pattern.indexOf('*');
|
|
83
|
+
let rest = null;
|
|
84
|
+
if (star < 0) rest = spec === a.pattern ? '' : null;
|
|
85
|
+
else if (spec.startsWith(a.pattern.slice(0, star)) && spec.endsWith(a.pattern.slice(star + 1))) rest = spec.slice(star, spec.length - (a.pattern.length - star - 1));
|
|
86
|
+
if (rest === null) continue;
|
|
87
|
+
const hit = firstTracked(a.targets.map((t) => t.replace('*', rest)));
|
|
88
|
+
if (hit) return hit;
|
|
89
|
+
}
|
|
90
|
+
const pkg = pkgs.find((p) => spec === p.name || spec.startsWith(`${p.name}/`));
|
|
91
|
+
if (!pkg) return undefined;
|
|
92
|
+
const sub = spec.slice(pkg.name.length + 1);
|
|
93
|
+
return sub
|
|
94
|
+
? firstTracked([posix.join(pkg.dir, sub), posix.join(pkg.dir, 'src', sub)])
|
|
95
|
+
: firstTracked(pkg.entries.map((e) => posix.join(pkg.dir, e.replace(/\.(?:m|c)?js$|\.tsx?$/u, ''))));
|
|
96
|
+
};
|
|
97
|
+
}
|
|
16
98
|
|
|
17
99
|
export function importEdges(files, read) {
|
|
18
100
|
const tracked = new Set(files);
|
|
@@ -27,13 +109,19 @@ export function importEdges(files, read) {
|
|
|
27
109
|
const base = posix.join(baseDir, ...dotted.split('.').filter(Boolean));
|
|
28
110
|
return [`${base}.py`, `${base}/__init__.py`].find((c) => tracked.has(c));
|
|
29
111
|
};
|
|
112
|
+
let bare = null;
|
|
30
113
|
for (const from of [...files].sort()) {
|
|
31
114
|
if (JS.test(from)) {
|
|
32
115
|
const body = read(from);
|
|
33
116
|
if (body === null) continue;
|
|
34
117
|
for (const m of body.matchAll(JS_IMPORT)) {
|
|
35
118
|
const spec = (m[1] ?? m[2] ?? m[3]).split('?')[0];
|
|
36
|
-
if (!spec.startsWith('.'))
|
|
119
|
+
if (!spec.startsWith('.')) {
|
|
120
|
+
if (spec.startsWith('node:') || /^[a-z]+:/u.test(spec)) continue;
|
|
121
|
+
bare ??= jsResolver(files, tracked, read);
|
|
122
|
+
add(from, bare(from, spec), lineAt(body, m.index));
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
37
125
|
const base = posix.normalize(posix.join(posix.dirname(from), spec));
|
|
38
126
|
// TypeScript sources import "./x.js" for the file "./x.ts".
|
|
39
127
|
const candidates = [...JS_EXT.map((e) => base + e), base.replace(/\.(m|c)?js$/u, '.$1ts'), base.replace(/\.js$/u, '.tsx')];
|
|
@@ -64,6 +152,33 @@ export function importEdges(files, read) {
|
|
|
64
152
|
}
|
|
65
153
|
}
|
|
66
154
|
}
|
|
155
|
+
dottedEdges(files, tracked, read, pyModule, add);
|
|
67
156
|
moreEdges([...files].sort(), read, add);
|
|
68
157
|
return out;
|
|
69
158
|
}
|
|
159
|
+
|
|
160
|
+
// Python projects name code by dotted path in strings: Frappe hooks.py ("app.module.function"), frappe.call from
|
|
161
|
+
// JavaScript, Django settings and urls, Celery task names. A string counts when its first part is a top-level Python
|
|
162
|
+
// package of the repository and the longest leading part names one of its modules.
|
|
163
|
+
function dottedEdges(files, tracked, read, pyModule, add) {
|
|
164
|
+
const top = new Map();
|
|
165
|
+
for (const f of files) {
|
|
166
|
+
const m = f.match(/^(?:(src)\/)?([A-Za-z_]\w*)\/__init__\.py$/u);
|
|
167
|
+
if (m) top.set(m[2], m[1] ?? '');
|
|
168
|
+
}
|
|
169
|
+
if (!top.size) return;
|
|
170
|
+
const DOTTED = /["'`]([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)+)["'`]/gu;
|
|
171
|
+
for (const from of files) {
|
|
172
|
+
if (!/\.(?:py|m?js|tsx?|json)$/u.test(from) || from.includes('node_modules/')) continue;
|
|
173
|
+
const body = read(from);
|
|
174
|
+
if (body === null || body.length > 2_000_000) continue;
|
|
175
|
+
for (const m of body.matchAll(DOTTED)) {
|
|
176
|
+
const parts = m[1].split('.');
|
|
177
|
+
if (!top.has(parts[0])) continue;
|
|
178
|
+
for (let n = parts.length; n >= 2; n--) {
|
|
179
|
+
const to = pyModule(parts.slice(0, n).join('.'), top.get(parts[0]));
|
|
180
|
+
if (to) { add(from, to, lineAt(body, m.index)); break; }
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|