diff options
| author | Morgan Lunt <morganl@anthropic.com> | 2026-09-24 18:07:14 -0700 |
|---|---|---|
| committer | Morgan Lunt <morganl@anthropic.com> | 2026-09-24 18:11:07 -0700 |
| commit | c9e990089c6c87cb96e227e998edbada1df82861 (patch) | |
| tree | 623eb2b217930fd1e56558946443cd2ddeb5b849 /plugins/code-modernization/hooks/paths.ts | |
| parent | 70caddcb673ac8995d6a5191e02ac51de579d6c3 (diff) | |
code-modernization: add an optional live progress pane
Adds a hooks module, off unless the CLI's early-access function hooks
are enabled, that draws a pane beside the transcript while the commands
run: the system, which steps are done, a map of its code, each unit of
work and where it stands, what needs attention, and the next command.
It reads only the artifacts the commands write and the legacy tree, so
it works for any language and for a rewrite, a same-stack uplift or a
greenfield rebuild. The map comes from topology.json when there is one
and from the source tree when there is not. In an uplift each module is
set against the baseline; tests are read from JUnit XML, .trx reports or
the runs a session sees; a button shows and hides the pane, and
/modernize-panel does the same from the keyboard.
The same module adds x-ray context on legacy reads, a review deck for
business rules (/modernize-review), a sign-off dialog (/modernize-sign)
and a fleet view that spots one failure repeating across agents. It
only watches: it never blocks, rewrites or restores an edit, and it runs
no program inside the trees it reads. An uplift's changed modules are
found by comparing the working copy with the tree by file name and size
through the file API. Its options are the plugin's userConfig. Without
the flag nothing loads and the commands, agents and workflows behave as
before.
91 tests in tests/ run with "claude plugin test"; plugin validate
passes on the release build with the flag on and off.
Diffstat (limited to 'plugins/code-modernization/hooks/paths.ts')
| -rw-r--r-- | plugins/code-modernization/hooks/paths.ts | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/plugins/code-modernization/hooks/paths.ts b/plugins/code-modernization/hooks/paths.ts new file mode 100644 index 00000000..ce34edd1 --- /dev/null +++ b/plugins/code-modernization/hooks/paths.ts @@ -0,0 +1,94 @@ +/** + * Path helpers. A hooks module has no `path` module: these are the few + * string operations the hooks need, all over forward-slash paths. + */ + +/** Forward slashes, no doubled separators, no trailing slash (root stays `/`). */ +export function norm(path: string): string { + const clean = path.replace(/\\/g, '/').replace(/\/{2,}/g, '/') + + return clean.length > 1 && clean.endsWith('/') ? clean.slice(0, -1) : clean +} + +/** Resolves `.` and `..` segments lexically; absolute stays absolute. */ +export function resolveDots(path: string): string { + const isAbsolute = path.startsWith('/') + const out: string[] = [] + + for (const part of norm(path).split('/')) { + if (part === '' || part === '.') { + continue + } + + if (part === '..') { + if (out.length > 0 && out.at(-1) !== '..') { + out.pop() + } else if (!isAbsolute) { + out.push('..') + } + + continue + } + + out.push(part) + } + + return (isAbsolute ? '/' : '') + out.join('/') +} + +/** `path` made absolute against `cwd` when it is relative. */ +export function absOf(cwd: string, path: string): string { + return resolveDots(path.startsWith('/') ? path : `${norm(cwd)}/${path}`) +} + +/** + * `path` relative to `cwd` when it lies inside it, else null. Both may be + * given with or without the macOS `/private` prefix the engine sometimes adds. + */ +export function relTo(cwd: string, path: string): string | null { + const strip = (p: string) => p.replace(/^\/private(?=\/(?:tmp|var)\/)/, '') + const base = strip(norm(cwd)) + const full = strip(absOf(cwd, path)) + + if (full === base) { + return '' + } + + return full.startsWith(`${base}/`) ? full.slice(base.length + 1) : null +} + +/** True when relative path `rel` is `dir` or lies beneath it. */ +export function isUnder(rel: string, dir: string): boolean { + const d = norm(dir) + + return rel === d || rel.startsWith(`${d}/`) +} + +/** The last segment. */ +export function baseName(path: string): string { + const parts = norm(path).split('/') + + return parts.at(-1) ?? '' +} + +/** The last segment without its final extension. */ +export function stemOf(path: string): string { + const base = baseName(path) + const dot = base.lastIndexOf('.') + + return dot > 0 ? base.slice(0, dot) : base +} + +/** Everything but the last segment (`''` for a bare name). */ +export function dirName(path: string): string { + const parts = norm(path).split('/') + + parts.pop() + + return parts.join('/') +} + +/** Joins segments with single slashes. */ +export function join(...parts: string[]): string { + return norm(parts.filter(part => part !== '').join('/')) +} |
