type-rename-move 0.0.1 → 0.14.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/LICENSE +21 -0
- package/README.md +15 -0
- package/dist/bin/trmv-compliance.js +4 -0
- package/dist/bin/trmv-config.js +4 -0
- package/dist/bin/trmv-doctor.js +4 -0
- package/dist/bin/trmv-mock-lint.js +4 -0
- package/dist/bin/trmv-refactor.js +4 -0
- package/dist/bin/trmv-typecheck.js +4 -0
- package/dist/changes/index.d.ts +24 -0
- package/dist/changes/index.js +112 -0
- package/dist/cli/index.d.ts +4 -0
- package/dist/cli/index.js +36 -0
- package/dist/config/command.d.ts +20 -0
- package/dist/config/command.js +68 -0
- package/dist/config/error.d.ts +3 -0
- package/dist/config/error.js +4 -0
- package/dist/config/files.d.ts +6 -0
- package/dist/config/files.js +29 -0
- package/dist/config/format.d.ts +25 -0
- package/dist/config/format.js +172 -0
- package/dist/config/index.d.ts +73 -0
- package/dist/config/index.js +239 -0
- package/dist/config/read.d.ts +4 -0
- package/dist/config/read.js +37 -0
- package/dist/contract/data/CONTRACT_HASH +1 -0
- package/dist/contract/data/README.md +69 -0
- package/dist/contract/data/cli.yaml +239 -0
- package/dist/contract/data/config.yaml +8 -0
- package/dist/contract/data/findings.yaml +212 -0
- package/dist/contract/data/languages.yaml +146 -0
- package/dist/contract/data/regex-subset.md +28 -0
- package/dist/contract/data/schema/rename-map.schema.json +51 -0
- package/dist/contract/data/schema/trmv.schema.json +92 -0
- package/dist/contract/data/schema/word-table.schema.json +55 -0
- package/dist/contract/data/vectors/cli.yaml +216 -0
- package/dist/contract/data/vectors/command-split.yaml +69 -0
- package/dist/contract/data/vectors/diff.yaml +287 -0
- package/dist/contract/data/vectors/format.yaml +160 -0
- package/dist/contract/data/vectors/glob.yaml +148 -0
- package/dist/contract/data/vectors/materialize.yaml +51 -0
- package/dist/contract/data/vectors/os-errors.yaml +123 -0
- package/dist/contract/data/vectors/refactor-symbols.yaml +220 -0
- package/dist/contract/data/vectors/refactor-text.yaml +186 -0
- package/dist/contract/data/vectors/refactor-words.yaml +83 -0
- package/dist/contract/data/vectors/regex-subset.yaml +40 -0
- package/dist/contract/data/vectors/repr.yaml +118 -0
- package/dist/contract/data/vectors/skill-leaks.yaml +6 -0
- package/dist/contract/data/vectors/test-files.yaml +135 -0
- package/dist/contract/data/vectors/yaml.yaml +858 -0
- package/dist/contract/glob.d.ts +3 -0
- package/dist/contract/glob.js +49 -0
- package/dist/contract/index.d.ts +13 -0
- package/dist/contract/index.js +14 -0
- package/dist/contract/json.d.ts +1 -0
- package/dist/contract/json.js +43 -0
- package/dist/contract/os-error.d.ts +13 -0
- package/dist/contract/os-error.js +66 -0
- package/dist/contract/parser.d.ts +18 -0
- package/dist/contract/parser.js +207 -0
- package/dist/contract/plugin.d.ts +5 -0
- package/dist/contract/plugin.js +90 -0
- package/dist/contract/regex.d.ts +5 -0
- package/dist/contract/regex.js +116 -0
- package/dist/contract/render.d.ts +3 -0
- package/dist/contract/render.js +19 -0
- package/dist/contract/schema.d.ts +10 -0
- package/dist/contract/schema.js +139 -0
- package/dist/contract/snapshot.d.ts +47 -0
- package/dist/contract/snapshot.js +129 -0
- package/dist/contract/text.d.ts +14 -0
- package/dist/contract/text.js +56 -0
- package/dist/contract/values.d.ts +24 -0
- package/dist/contract/values.js +127 -0
- package/dist/contract/which.d.ts +4 -0
- package/dist/contract/which.js +27 -0
- package/dist/contract/yaml.d.ts +11 -0
- package/dist/contract/yaml.js +595 -0
- package/dist/doctor/index.d.ts +3 -0
- package/dist/doctor/index.js +69 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -0
- package/dist/lang/conventions.d.ts +33 -0
- package/dist/lang/conventions.js +216 -0
- package/dist/lang/edges.d.ts +2 -0
- package/dist/lang/edges.js +142 -0
- package/dist/lang/files.d.ts +28 -0
- package/dist/lang/files.js +122 -0
- package/dist/lang/imports.d.ts +3 -0
- package/dist/lang/imports.js +64 -0
- package/dist/lang/index.d.ts +13 -0
- package/dist/lang/index.js +18 -0
- package/dist/lang/lines.d.ts +21 -0
- package/dist/lang/lines.js +96 -0
- package/dist/lang/projects.d.ts +30 -0
- package/dist/lang/projects.js +115 -0
- package/dist/lang/specifiers.d.ts +7 -0
- package/dist/lang/specifiers.js +88 -0
- package/dist/lang/ts.d.ts +2 -0
- package/dist/lang/ts.js +7 -0
- package/dist/lang/tsc.d.ts +32 -0
- package/dist/lang/tsc.js +130 -0
- package/dist/lang/types.d.ts +27 -0
- package/dist/lang/types.js +4 -0
- package/dist/refactor/baseline.d.ts +13 -0
- package/dist/refactor/baseline.js +148 -0
- package/dist/refactor/bindings.d.ts +76 -0
- package/dist/refactor/bindings.js +594 -0
- package/dist/refactor/carve.d.ts +31 -0
- package/dist/refactor/carve.js +145 -0
- package/dist/refactor/compliance.d.ts +15 -0
- package/dist/refactor/compliance.js +161 -0
- package/dist/refactor/compound.d.ts +5 -0
- package/dist/refactor/compound.js +40 -0
- package/dist/refactor/diff.d.ts +2 -0
- package/dist/refactor/diff.js +170 -0
- package/dist/refactor/edits.d.ts +16 -0
- package/dist/refactor/edits.js +34 -0
- package/dist/refactor/generate.d.ts +4 -0
- package/dist/refactor/generate.js +209 -0
- package/dist/refactor/index.d.ts +11 -0
- package/dist/refactor/index.js +144 -0
- package/dist/refactor/lines.d.ts +13 -0
- package/dist/refactor/lines.js +34 -0
- package/dist/refactor/literal-sources.d.ts +20 -0
- package/dist/refactor/literal-sources.js +243 -0
- package/dist/refactor/map.d.ts +21 -0
- package/dist/refactor/map.js +45 -0
- package/dist/refactor/members.d.ts +68 -0
- package/dist/refactor/members.js +187 -0
- package/dist/refactor/mention-scope.d.ts +19 -0
- package/dist/refactor/mention-scope.js +113 -0
- package/dist/refactor/mentions.d.ts +28 -0
- package/dist/refactor/mentions.js +92 -0
- package/dist/refactor/mocks.d.ts +16 -0
- package/dist/refactor/mocks.js +139 -0
- package/dist/refactor/module-rename.d.ts +17 -0
- package/dist/refactor/module-rename.js +160 -0
- package/dist/refactor/outcomes.d.ts +42 -0
- package/dist/refactor/outcomes.js +61 -0
- package/dist/refactor/own-scope.d.ts +16 -0
- package/dist/refactor/own-scope.js +142 -0
- package/dist/refactor/position.d.ts +34 -0
- package/dist/refactor/position.js +58 -0
- package/dist/refactor/program-check.d.ts +11 -0
- package/dist/refactor/program-check.js +49 -0
- package/dist/refactor/progress.d.ts +27 -0
- package/dist/refactor/progress.js +66 -0
- package/dist/refactor/rename.d.ts +18 -0
- package/dist/refactor/rename.js +1299 -0
- package/dist/refactor/retarget.d.ts +79 -0
- package/dist/refactor/retarget.js +647 -0
- package/dist/refactor/routing.d.ts +19 -0
- package/dist/refactor/routing.js +237 -0
- package/dist/refactor/session.d.ts +81 -0
- package/dist/refactor/session.js +180 -0
- package/dist/refactor/sites.d.ts +62 -0
- package/dist/refactor/sites.js +207 -0
- package/dist/refactor/symbols.d.ts +41 -0
- package/dist/refactor/symbols.js +385 -0
- package/dist/refactor/timings.d.ts +24 -0
- package/dist/refactor/timings.js +79 -0
- package/dist/refactor/typescript.d.ts +5 -0
- package/dist/refactor/typescript.js +279 -0
- package/dist/refactor/words.d.ts +42 -0
- package/dist/refactor/words.js +114 -0
- package/dist/refactor/workspace.d.ts +59 -0
- package/dist/refactor/workspace.js +314 -0
- package/dist/refactor/writer.d.ts +10 -0
- package/dist/refactor/writer.js +110 -0
- package/dist/twin/index.d.ts +50 -0
- package/dist/twin/index.js +180 -0
- package/dist/typecheck/index.d.ts +11 -0
- package/dist/typecheck/index.js +210 -0
- package/package.json +84 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MotleyAI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# type-rename-move
|
|
2
|
+
|
|
3
|
+
Verified renames and moves for Python and TypeScript code. `trmv-refactor` rewrites every import and reference, and
|
|
4
|
+
`trmv-typecheck` checks the result against the repo's committed type-check baseline, so a refactor that missed a
|
|
5
|
+
reference fails instead of slipping through. `trmv-compliance` reports the constructs that would make a refactor
|
|
6
|
+
unverifiable, and `trmv-mock-lint` the test doubles not bound to the real type.
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
uv tool install type-rename-move # Python repos (Python >= 3.11)
|
|
10
|
+
npm install -g type-rename-move # TypeScript/JavaScript repos (Node >= 22)
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
A repo configures it in `trmv.yaml`, or in `living-architecture.yaml` when it also uses
|
|
14
|
+
[living-architecture](https://github.com/MotleyAI/living-architecture), which builds on this package. The Claude Code
|
|
15
|
+
plugin `trmv` drives the commands; see [Renames and moves](https://github.com/MotleyAI/living-architecture/blob/main/docs/rename-and-move.md).
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface ListingOptions {
|
|
2
|
+
baseRef: string;
|
|
3
|
+
repoRoot: string;
|
|
4
|
+
deletions: boolean;
|
|
5
|
+
untracked: boolean;
|
|
6
|
+
}
|
|
7
|
+
/** `origin/<base>` after a best-effort, bounded, non-prompting fetch; ConfigError when it does not resolve. */
|
|
8
|
+
export declare function resolveBaseRef(base: string, repoRoot: string, fetchTimeout?: number): string;
|
|
9
|
+
/** Changed paths as raw bytes in byte order: the merge-base diff, working-tree edits and optionally deletions and untracked files. */
|
|
10
|
+
export declare function changedPathBytes({ baseRef, repoRoot, deletions, untracked }: ListingOptions): Buffer[];
|
|
11
|
+
/** `changedPathBytes` decoded, in code-point order. */
|
|
12
|
+
export declare function changedPaths(options: ListingOptions): string[];
|
|
13
|
+
/** Every tracked and untracked, not ignored path, as raw bytes in byte order. */
|
|
14
|
+
export declare function repoPathBytes(repoRoot: string): Buffer[];
|
|
15
|
+
/** The merge base of `baseRef` and HEAD. */
|
|
16
|
+
export declare function mergeBase(baseRef: string, repoRoot: string): string;
|
|
17
|
+
/** The repo-relative paths tracked at `ref`. */
|
|
18
|
+
export declare function pathsAt(ref: string, repoRoot: string): Set<string>;
|
|
19
|
+
/** The bytes of repo-relative `path` at `ref`, null when it is not there. */
|
|
20
|
+
export declare function fileAt(ref: string, path: string, repoRoot: string): Buffer | null;
|
|
21
|
+
/** Whether `ref` names a commit; ConfigError when git fails. */
|
|
22
|
+
export declare function isCommit(ref: string, repoRoot: string): boolean;
|
|
23
|
+
/** Each path renamed since `ref` (tracked files of the working tree), mapped to its path at `ref`. */
|
|
24
|
+
export declare function basePaths(ref: string, repoRoot: string): Map<string, string>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// The change set: base-ref resolution and the NUL-delimited git change listing.
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import { ConfigError } from '../config/index.js';
|
|
4
|
+
import { baseFetchTimeout, byCodePoint, message } from '../contract/index.js';
|
|
5
|
+
/** Neither git nor ssh may prompt during the fetch: no terminal prompt and no askpass helper. */
|
|
6
|
+
const NO_PROMPT = { GIT_TERMINAL_PROMPT: '0', GIT_ASKPASS: '', SSH_ASKPASS: '', SSH_ASKPASS_REQUIRE: 'never' };
|
|
7
|
+
/** git's stdout; ConfigError with git's stderr, else the shared text. */
|
|
8
|
+
function git(args, cwd) {
|
|
9
|
+
const proc = spawnSync('git', args, { cwd, maxBuffer: 1 << 30 }); // NOSONAR(S4036) — runs the user's own git from PATH by design
|
|
10
|
+
if (proc.status !== 0) {
|
|
11
|
+
const detail = proc.stderr?.toString('utf8').trim() ?? '';
|
|
12
|
+
throw new ConfigError(detail || message('changes.git-failed', { args: args.join(' ') }));
|
|
13
|
+
}
|
|
14
|
+
return proc.stdout;
|
|
15
|
+
}
|
|
16
|
+
function nulSplit(listing) {
|
|
17
|
+
const out = [];
|
|
18
|
+
let start = 0;
|
|
19
|
+
for (let i = listing.indexOf(0); i !== -1; i = listing.indexOf(0, start)) {
|
|
20
|
+
if (i > start)
|
|
21
|
+
out.push(listing.subarray(start, i));
|
|
22
|
+
start = i + 1;
|
|
23
|
+
}
|
|
24
|
+
if (start < listing.length)
|
|
25
|
+
out.push(listing.subarray(start));
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
function uniqueSorted(paths) {
|
|
29
|
+
const seen = new Map();
|
|
30
|
+
for (const path of paths)
|
|
31
|
+
seen.set(path.toString('hex'), path);
|
|
32
|
+
return [...seen.values()].sort(Buffer.compare);
|
|
33
|
+
}
|
|
34
|
+
/** `git fetch origin <base>` in its own process group, the whole group killed after `timeout` seconds. */
|
|
35
|
+
function boundedFetch(base, repoRoot, timeout) {
|
|
36
|
+
const env = { ...process.env, ...NO_PROMPT };
|
|
37
|
+
const options = { cwd: repoRoot, stdio: 'ignore', env, detached: true, timeout: timeout * 1000, killSignal: 'SIGKILL' };
|
|
38
|
+
const proc = spawnSync('git', ['fetch', 'origin', base, '--quiet'], options); // NOSONAR(S4036) — runs the user's own git from PATH by design
|
|
39
|
+
if (proc.error?.code !== 'ETIMEDOUT' || proc.pid === undefined)
|
|
40
|
+
return;
|
|
41
|
+
try {
|
|
42
|
+
process.kill(-proc.pid, 'SIGKILL');
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// the group is already gone, or the platform has no process groups
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** `origin/<base>` after a best-effort, bounded, non-prompting fetch; ConfigError when it does not resolve. */
|
|
49
|
+
export function resolveBaseRef(base, repoRoot, fetchTimeout = baseFetchTimeout()) {
|
|
50
|
+
boundedFetch(base, repoRoot, fetchTimeout);
|
|
51
|
+
const ref = `origin/${base}`;
|
|
52
|
+
git(['rev-parse', '--verify', '--quiet', ref], repoRoot);
|
|
53
|
+
return ref;
|
|
54
|
+
}
|
|
55
|
+
/** Changed paths as raw bytes in byte order: the merge-base diff, working-tree edits and optionally deletions and untracked files. */
|
|
56
|
+
export function changedPathBytes({ baseRef, repoRoot, deletions, untracked }) {
|
|
57
|
+
const filter = deletions ? [] : ['--diff-filter=ACMR'];
|
|
58
|
+
const found = [];
|
|
59
|
+
for (const target of [`${baseRef}...HEAD`, 'HEAD']) {
|
|
60
|
+
found.push(...nulSplit(git(['diff', '-z', '--name-only', '--no-renames', ...filter, target, '--'], repoRoot)));
|
|
61
|
+
}
|
|
62
|
+
if (untracked)
|
|
63
|
+
found.push(...nulSplit(git(['ls-files', '-z', '--others', '--exclude-standard'], repoRoot)));
|
|
64
|
+
return uniqueSorted(found);
|
|
65
|
+
}
|
|
66
|
+
/** `changedPathBytes` decoded, in code-point order. */
|
|
67
|
+
export function changedPaths(options) {
|
|
68
|
+
return [...new Set(changedPathBytes(options).map((path) => path.toString('utf8')))].sort(byCodePoint);
|
|
69
|
+
}
|
|
70
|
+
/** Every tracked and untracked, not ignored path, as raw bytes in byte order. */
|
|
71
|
+
export function repoPathBytes(repoRoot) {
|
|
72
|
+
return uniqueSorted(nulSplit(git(['ls-files', '-z', '--cached', '--others', '--exclude-standard'], repoRoot)));
|
|
73
|
+
}
|
|
74
|
+
/** The merge base of `baseRef` and HEAD. */
|
|
75
|
+
export function mergeBase(baseRef, repoRoot) {
|
|
76
|
+
return git(['merge-base', baseRef, 'HEAD'], repoRoot).toString('utf8').trim();
|
|
77
|
+
}
|
|
78
|
+
/** The repo-relative paths tracked at `ref`. */
|
|
79
|
+
export function pathsAt(ref, repoRoot) {
|
|
80
|
+
return new Set(nulSplit(git(['ls-tree', '-r', '-z', '--name-only', ref], repoRoot)).map((path) => path.toString('utf8')));
|
|
81
|
+
}
|
|
82
|
+
/** The bytes of repo-relative `path` at `ref`, null when it is not there. */
|
|
83
|
+
export function fileAt(ref, path, repoRoot) {
|
|
84
|
+
const proc = spawnSync('git', ['cat-file', 'blob', `${ref}:${path}`], { cwd: repoRoot, maxBuffer: 1 << 30 }); // NOSONAR(S4036) — runs the user's own git from PATH by design
|
|
85
|
+
return proc.status === 0 ? proc.stdout : null;
|
|
86
|
+
}
|
|
87
|
+
/** Whether `ref` names a commit; ConfigError when git fails. */
|
|
88
|
+
export function isCommit(ref, repoRoot) {
|
|
89
|
+
const args = ['rev-parse', '--verify', '--quiet', '--end-of-options', `${ref}^{commit}`];
|
|
90
|
+
const proc = spawnSync('git', args, { cwd: repoRoot }); // NOSONAR(S4036) — runs the user's own git from PATH by design
|
|
91
|
+
if (proc.error !== undefined)
|
|
92
|
+
throw new ConfigError(message('changes.git-failed', { args: args.join(' ') }));
|
|
93
|
+
const detail = proc.stderr.toString('utf8').trim();
|
|
94
|
+
if (proc.status !== 0 && detail !== '')
|
|
95
|
+
throw new ConfigError(detail);
|
|
96
|
+
return proc.status === 0;
|
|
97
|
+
}
|
|
98
|
+
/** Each path renamed since `ref` (tracked files of the working tree), mapped to its path at `ref`. */
|
|
99
|
+
export function basePaths(ref, repoRoot) {
|
|
100
|
+
const fields = nulSplit(git(['diff', '-z', '--name-status', '-M', ref, '--'], repoRoot)).map((field) => field.toString('utf8'));
|
|
101
|
+
const out = new Map();
|
|
102
|
+
for (let i = 0; i < fields.length;) {
|
|
103
|
+
if ((fields[i] ?? '').startsWith('R')) {
|
|
104
|
+
out.set(fields[i + 2] ?? '', fields[i + 1] ?? '');
|
|
105
|
+
i += 3;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
i += 2;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** The exit code of the trmv command `command` run with `argv`. */
|
|
2
|
+
export declare function dispatch(command: string, argv: string[]): number;
|
|
3
|
+
/** A bin's entry: run `command` with the process argv and exit with its code once output is flushed. */
|
|
4
|
+
export declare function main(command: string): void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// The trmv commands' entry point: their handlers, run through the shared command runner.
|
|
2
|
+
import * as config from '../config/index.js';
|
|
3
|
+
import { commandRoot, optional, runCommand } from '../config/index.js';
|
|
4
|
+
import * as doctor from '../doctor/index.js';
|
|
5
|
+
import * as refactor from '../refactor/index.js';
|
|
6
|
+
import * as twin from '../twin/index.js';
|
|
7
|
+
import * as typecheck from '../typecheck/index.js';
|
|
8
|
+
/** The parsed options with null (not given) as undefined. */
|
|
9
|
+
const given = (args) => Object.fromEntries(Object.entries(args).map(([key, value]) => [key, value === null ? undefined : value]));
|
|
10
|
+
const HANDLERS = {
|
|
11
|
+
'trmv-config': (args) => {
|
|
12
|
+
const root = commandRoot(args.root);
|
|
13
|
+
return args.subcommand === 'show'
|
|
14
|
+
? config.runShow('trmv-config', root, config.trmvView)
|
|
15
|
+
: config.runGet('trmv-config', root, String(args.key), config.trmvView);
|
|
16
|
+
},
|
|
17
|
+
'trmv-doctor': (args) => {
|
|
18
|
+
if (args.twin) {
|
|
19
|
+
process.stdout.write(`${twin.identity()}\n`);
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
return doctor.run(commandRoot(args.root), optional(args.plugin), Boolean(args.contract_hash), Boolean(args.require_config));
|
|
23
|
+
},
|
|
24
|
+
'trmv-typecheck': (args) => typecheck.run(process.cwd(), Boolean(args.write_baseline), optional(args.language), optional(args.realign_from)),
|
|
25
|
+
'trmv-refactor': (args, argv) => refactor.runRefactor(argv, given(args)),
|
|
26
|
+
'trmv-compliance': (args) => refactor.runCompliance(args.paths, optional(args.select), args.attr),
|
|
27
|
+
'trmv-mock-lint': (args) => refactor.runMockLint(args.paths, optional(args.base)),
|
|
28
|
+
};
|
|
29
|
+
/** The exit code of the trmv command `command` run with `argv`. */
|
|
30
|
+
export function dispatch(command, argv) {
|
|
31
|
+
return runCommand(command, argv, HANDLERS);
|
|
32
|
+
}
|
|
33
|
+
/** A bin's entry: run `command` with the process argv and exit with its code once output is flushed. */
|
|
34
|
+
export function main(command) {
|
|
35
|
+
process.exitCode = dispatch(command, process.argv.slice(2));
|
|
36
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Args } from '../contract/parser.js';
|
|
2
|
+
import * as twin from '../twin/index.js';
|
|
3
|
+
export type Handler = (args: Args, argv: string[]) => number;
|
|
4
|
+
/** `--root` resolved, else the git root of the cwd. */
|
|
5
|
+
export declare const commandRoot: (value: unknown) => string;
|
|
6
|
+
/** A string option, or null when not given. */
|
|
7
|
+
export declare const optional: (value: unknown) => string | null;
|
|
8
|
+
/** How a package runs its commands: its manifest, the config file whose format it checks (default: the one trmv
|
|
9
|
+
* reads), its twin identity, and the runner of commands the manifest backs with a bundled script. */
|
|
10
|
+
export interface CommandSet {
|
|
11
|
+
commands?: Record<string, any>;
|
|
12
|
+
configFile?: string;
|
|
13
|
+
who?: twin.Identity;
|
|
14
|
+
shim?: (command: string, argv: string[]) => number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The exit code of `command` run with `argv`: forwarded to its native twin, or parsed, refused when both config files
|
|
18
|
+
* are present or the format is wrong (unless exempt), and dispatched to its handler.
|
|
19
|
+
*/
|
|
20
|
+
export declare function runCommand(command: string, argv: string[], handlers: Record<string, Handler>, set?: CommandSet): number;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Running a command: forward it to its native twin, or parse argv per the shared manifest, check the repo format
|
|
2
|
+
// and dispatch to its handler.
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import { manifest, message } from '../contract/index.js';
|
|
5
|
+
import { HelpRequested, UsageError, parse } from '../contract/parser.js';
|
|
6
|
+
import * as twin from '../twin/index.js';
|
|
7
|
+
import { bothFilesProblem } from './files.js';
|
|
8
|
+
import { checkFormat } from './format.js';
|
|
9
|
+
import { findRepoRoot } from './index.js';
|
|
10
|
+
/** `--root` resolved, else the git root of the cwd. */
|
|
11
|
+
export const commandRoot = (value) => typeof value === 'string' && value ? resolve(value) : findRepoRoot(process.cwd());
|
|
12
|
+
/** A string option, or null when not given. */
|
|
13
|
+
export const optional = (value) => (typeof value === 'string' ? value : null);
|
|
14
|
+
/** The repo whose format a command checks: `--root`, else the git root of `--project`, else of the cwd. */
|
|
15
|
+
const checkedRoot = (args) => {
|
|
16
|
+
const project = optional(args.project);
|
|
17
|
+
return optional(args.root) || !project ? commandRoot(args.root) : findRepoRoot(project);
|
|
18
|
+
};
|
|
19
|
+
/** Run a command another twin implements: forward the raw argv, or exit 2 with the hint. */
|
|
20
|
+
function forwarded(command, lang, argv, who) {
|
|
21
|
+
try {
|
|
22
|
+
return twin.forward(command, lang, argv, findRepoRoot(process.cwd()), who);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (!(error instanceof twin.TwinError))
|
|
26
|
+
throw error;
|
|
27
|
+
process.stderr.write(`${message('twin.error', { prog: command, error: error.message })}\n`);
|
|
28
|
+
return 2;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The exit code of `command` run with `argv`: forwarded to its native twin, or parsed, refused when both config files
|
|
33
|
+
* are present or the format is wrong (unless exempt), and dispatched to its handler.
|
|
34
|
+
*/
|
|
35
|
+
export function runCommand(command, argv, handlers, set = {}) {
|
|
36
|
+
const spec = (set.commands ?? manifest())[command];
|
|
37
|
+
const native = spec.native;
|
|
38
|
+
if (native !== undefined && !native.includes(twin.NATIVE_LANGUAGE))
|
|
39
|
+
return forwarded(command, native[0] ?? '', argv, set.who);
|
|
40
|
+
if (spec.script !== undefined && set.shim !== undefined)
|
|
41
|
+
return set.shim(command, argv);
|
|
42
|
+
let args;
|
|
43
|
+
try {
|
|
44
|
+
args = parse(command, argv, set.commands);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (error instanceof HelpRequested) {
|
|
48
|
+
process.stdout.write(error.text);
|
|
49
|
+
return 0;
|
|
50
|
+
}
|
|
51
|
+
if (!(error instanceof UsageError))
|
|
52
|
+
throw error;
|
|
53
|
+
process.stderr.write(`usage: ${command} [-h] ...\n${error.command}: error: ${error.message}\n`);
|
|
54
|
+
return error.exitCode;
|
|
55
|
+
}
|
|
56
|
+
if (spec.format !== 'exempt') {
|
|
57
|
+
const root = checkedRoot(args);
|
|
58
|
+
const problem = bothFilesProblem(root) ?? checkFormat(root, set.configFile ?? null);
|
|
59
|
+
if (problem !== null) {
|
|
60
|
+
process.stderr.write(`${problem}\n`);
|
|
61
|
+
return 2;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const handler = handlers[command];
|
|
65
|
+
if (handler === undefined)
|
|
66
|
+
throw new Error(`no handler for ${command}`);
|
|
67
|
+
return handler(args, argv);
|
|
68
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** The refusal when every committed config file is at the repo root, else null. */
|
|
2
|
+
export declare function bothFilesProblem(root: string): string | null;
|
|
3
|
+
/** The committed config file trmv reads: the first of the contract's files at the repo root, else null. */
|
|
4
|
+
export declare function configSource(root: string): string | null;
|
|
5
|
+
/** trmv's own config file; the others carry trmv's keys among their package's. */
|
|
6
|
+
export declare function ownFile(): string;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// The committed config files: which one trmv reads, and the refusal when they are all present.
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { configFiles, isOsError, isRegularFile, message } from '../contract/index.js';
|
|
4
|
+
/** A regular file, or an entry whose probe fails (reading it reports the OS error). */
|
|
5
|
+
function present(path) {
|
|
6
|
+
try {
|
|
7
|
+
return isRegularFile(path);
|
|
8
|
+
}
|
|
9
|
+
catch (error) {
|
|
10
|
+
if (isOsError(error))
|
|
11
|
+
return true;
|
|
12
|
+
throw error;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/** The refusal when every committed config file is at the repo root, else null. */
|
|
16
|
+
export function bothFilesProblem(root) {
|
|
17
|
+
const files = configFiles();
|
|
18
|
+
return files.every((name) => present(join(root, name)))
|
|
19
|
+
? message('config.both-files', { first: files[0], second: files[1] })
|
|
20
|
+
: null;
|
|
21
|
+
}
|
|
22
|
+
/** The committed config file trmv reads: the first of the contract's files at the repo root, else null. */
|
|
23
|
+
export function configSource(root) {
|
|
24
|
+
return configFiles().find((name) => present(join(root, name))) ?? null;
|
|
25
|
+
}
|
|
26
|
+
/** trmv's own config file; the others carry trmv's keys among their package's. */
|
|
27
|
+
export function ownFile() {
|
|
28
|
+
return configFiles()[0];
|
|
29
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare const FORMAT_KEY = "format";
|
|
2
|
+
/** The committed config file: its exact text and its top-level mapping, null for an empty document. */
|
|
3
|
+
export interface Committed {
|
|
4
|
+
text: string;
|
|
5
|
+
data: Map<unknown, unknown> | null;
|
|
6
|
+
}
|
|
7
|
+
/** Every released format of config file `file`, oldest first. */
|
|
8
|
+
export declare function releasedFormats(file: string): string[];
|
|
9
|
+
/** The format of config file `file` its package reads and writes: the last released one. */
|
|
10
|
+
export declare function currentFormat(file: string): string;
|
|
11
|
+
/** Three dot-separated decimal integers without leading zeros. */
|
|
12
|
+
export declare function isVersion(value: unknown): value is string;
|
|
13
|
+
/** The sign of `a - b`, component by component as integers of any size (by digit count, then digits). */
|
|
14
|
+
export declare function compareVersions(a: string, b: string): number;
|
|
15
|
+
/** Committed config file `file`, null without it; ConfigError when it is unreadable, not YAML or not a mapping. */
|
|
16
|
+
export declare function readCommitted(root: string, file: string): Committed | null;
|
|
17
|
+
/** The refusal of config file `file`'s `format`, or null when it declares `current` (default: its current format). */
|
|
18
|
+
export declare function formatProblem(data: Map<unknown, unknown>, file: string, current?: string): string | null;
|
|
19
|
+
/**
|
|
20
|
+
* The format check's refusal for config file `file` (default: the one trmv reads), or null to go on (also for a committed
|
|
21
|
+
* file it cannot judge).
|
|
22
|
+
*/
|
|
23
|
+
export declare function checkFormat(root: string, file?: string | null): string | null;
|
|
24
|
+
/** `text` declaring `current`: a `format` line inserted or its value replaced, verified to change only that key; null when it cannot be. */
|
|
25
|
+
export declare function stampFormat(text: string, current: string): string | null;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// The format of a committed config file: its `format` key, the check every gated command runs, the stamp.
|
|
2
|
+
import { YAMLError, byCodePoint, canonicalRepr, configFormats, loadYaml, message, normalize, } from '../contract/index.js';
|
|
3
|
+
import { ConfigError } from './error.js';
|
|
4
|
+
import { configSource, ownFile } from './files.js';
|
|
5
|
+
import { parseConfig, readConfigText } from './read.js';
|
|
6
|
+
export const FORMAT_KEY = 'format';
|
|
7
|
+
const VERSION_RE = /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)$/;
|
|
8
|
+
const BLANK = ' \t\r';
|
|
9
|
+
// A top-level `format` line with one single-line scalar value: its value, then its trailing whitespace and comment.
|
|
10
|
+
const FORMAT_LINE = new RegExp(String.raw `^${FORMAT_KEY}:[ \t]+` +
|
|
11
|
+
String.raw `(?<value>'(?:[^']|'')*'|"(?:[^"\\]|\\.)*"|[^\s#'"|>{}\[\],&*!%@` + '`' + String.raw `](?:[^#]|(?<![ \t])#)*?)` +
|
|
12
|
+
String.raw `(?<rest>[ \t]*(?:#.*)?)$`);
|
|
13
|
+
/** Every released format of config file `file`, oldest first. */
|
|
14
|
+
export function releasedFormats(file) {
|
|
15
|
+
return configFormats(file);
|
|
16
|
+
}
|
|
17
|
+
/** The format of config file `file` its package reads and writes: the last released one. */
|
|
18
|
+
export function currentFormat(file) {
|
|
19
|
+
return configFormats(file).at(-1);
|
|
20
|
+
}
|
|
21
|
+
/** Three dot-separated decimal integers without leading zeros. */
|
|
22
|
+
export function isVersion(value) {
|
|
23
|
+
return typeof value === 'string' && VERSION_RE.test(value);
|
|
24
|
+
}
|
|
25
|
+
/** The sign of `a - b`, component by component as integers of any size (by digit count, then digits). */
|
|
26
|
+
export function compareVersions(a, b) {
|
|
27
|
+
const ys = b.split('.');
|
|
28
|
+
for (const [i, x] of a.split('.').entries()) {
|
|
29
|
+
const y = ys[i] ?? '';
|
|
30
|
+
if (x.length !== y.length)
|
|
31
|
+
return x.length > y.length ? 1 : -1;
|
|
32
|
+
if (x !== y)
|
|
33
|
+
return x > y ? 1 : -1;
|
|
34
|
+
}
|
|
35
|
+
return 0;
|
|
36
|
+
}
|
|
37
|
+
/** Committed config file `file`, null without it; ConfigError when it is unreadable, not YAML or not a mapping. */
|
|
38
|
+
export function readCommitted(root, file) {
|
|
39
|
+
const text = readConfigText(root, file);
|
|
40
|
+
if (text === null)
|
|
41
|
+
return null;
|
|
42
|
+
const data = parseConfig(text, file);
|
|
43
|
+
if (data !== null && !(data instanceof Map))
|
|
44
|
+
throw new ConfigError(`${file}: top level must be a mapping`);
|
|
45
|
+
return { text, data };
|
|
46
|
+
}
|
|
47
|
+
/** The refusal of config file `file`'s `format`, or null when it declares `current` (default: its current format). */
|
|
48
|
+
export function formatProblem(data, file, current = currentFormat(file)) {
|
|
49
|
+
const kind = file === ownFile() ? 'format.trmv-' : 'format.';
|
|
50
|
+
if (!data.has(FORMAT_KEY))
|
|
51
|
+
return message(`${kind}missing`, { current });
|
|
52
|
+
const declared = data.get(FORMAT_KEY);
|
|
53
|
+
if (!isVersion(declared))
|
|
54
|
+
return message(`${kind}invalid`, { value: declared, current });
|
|
55
|
+
const sign = compareVersions(declared, current);
|
|
56
|
+
if (sign === 0)
|
|
57
|
+
return null;
|
|
58
|
+
return message(sign > 0 ? `${kind}newer` : `${kind}older`, { declared, current });
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The format check's refusal for config file `file` (default: the one trmv reads), or null to go on (also for a committed
|
|
62
|
+
* file it cannot judge).
|
|
63
|
+
*/
|
|
64
|
+
export function checkFormat(root, file = null) {
|
|
65
|
+
const name = file ?? configSource(root);
|
|
66
|
+
if (name === null)
|
|
67
|
+
return null;
|
|
68
|
+
let committed;
|
|
69
|
+
try {
|
|
70
|
+
committed = readCommitted(root, name);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (error instanceof ConfigError)
|
|
74
|
+
return null;
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
return committed?.data ? formatProblem(committed.data, name) : null;
|
|
78
|
+
}
|
|
79
|
+
/** The canonical repr of a normalized value, with every mapping's entries sorted. */
|
|
80
|
+
function unorderedRepr(value) {
|
|
81
|
+
if (value instanceof Map) {
|
|
82
|
+
const entries = [...value].map(([k, v]) => `${unorderedRepr(k)}: ${unorderedRepr(v)}`);
|
|
83
|
+
return `{${entries.toSorted(byCodePoint).join(', ')}}`;
|
|
84
|
+
}
|
|
85
|
+
if (Array.isArray(value))
|
|
86
|
+
return `[${value.map(unorderedRepr).join(', ')}]`;
|
|
87
|
+
return canonicalRepr(value);
|
|
88
|
+
}
|
|
89
|
+
function stripStart(text, chars) {
|
|
90
|
+
let start = 0;
|
|
91
|
+
while (start < text.length && chars.includes(text.charAt(start)))
|
|
92
|
+
start += 1;
|
|
93
|
+
return text.slice(start);
|
|
94
|
+
}
|
|
95
|
+
function stripEnd(text, chars) {
|
|
96
|
+
let end = text.length;
|
|
97
|
+
while (end > 0 && chars.includes(text.charAt(end - 1)))
|
|
98
|
+
end -= 1;
|
|
99
|
+
return text.slice(0, end);
|
|
100
|
+
}
|
|
101
|
+
/** The index of the first line after the leading comments, blank lines, directives and a `---` marker. */
|
|
102
|
+
function insertAt(lines) {
|
|
103
|
+
for (const [index, line] of lines.entries()) {
|
|
104
|
+
const content = stripEnd(line, '\r\n');
|
|
105
|
+
if (stripEnd(content, BLANK) === '---')
|
|
106
|
+
return index + 1;
|
|
107
|
+
const stripped = stripStart(content, BLANK);
|
|
108
|
+
if (stripped && !stripped.startsWith('#') && !content.startsWith('%'))
|
|
109
|
+
return index;
|
|
110
|
+
}
|
|
111
|
+
return lines.length;
|
|
112
|
+
}
|
|
113
|
+
function parsed(text) {
|
|
114
|
+
try {
|
|
115
|
+
return { value: loadYaml(text) };
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
if (error instanceof YAMLError)
|
|
119
|
+
return null;
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** `text` split after each line break; no empty last line. */
|
|
124
|
+
function linesOf(text) {
|
|
125
|
+
const lines = text.split('\n').map((line) => `${line}\n`);
|
|
126
|
+
lines[lines.length - 1] = (lines.at(-1) ?? '').slice(0, -1);
|
|
127
|
+
if (lines.at(-1) === '')
|
|
128
|
+
lines.pop();
|
|
129
|
+
return lines;
|
|
130
|
+
}
|
|
131
|
+
/** `text` with a `format: <current>` line after its leading comments, directives and document marker. */
|
|
132
|
+
function inserted(text, current) {
|
|
133
|
+
const firstBreak = text.indexOf('\n');
|
|
134
|
+
const newline = firstBreak > 0 && text[firstBreak - 1] === '\r' ? '\r\n' : '\n';
|
|
135
|
+
const lines = linesOf(text);
|
|
136
|
+
const index = insertAt(lines);
|
|
137
|
+
if (index === lines.length && lines.length > 0 && !(lines.at(-1) ?? '').endsWith('\n'))
|
|
138
|
+
lines[lines.length - 1] += newline;
|
|
139
|
+
lines.splice(index, 0, `${FORMAT_KEY}: ${current}${newline}`);
|
|
140
|
+
return lines.join('');
|
|
141
|
+
}
|
|
142
|
+
/** `text` with the value on its one top-level `format` line replaced by `current`; null without exactly one. */
|
|
143
|
+
function replaced(text, current) {
|
|
144
|
+
const lines = linesOf(text);
|
|
145
|
+
const found = [...lines.keys()].filter((i) => (lines[i] ?? '').startsWith(`${FORMAT_KEY}:`));
|
|
146
|
+
const at = found[0];
|
|
147
|
+
if (found.length !== 1 || at === undefined)
|
|
148
|
+
return null;
|
|
149
|
+
const line = lines[at] ?? '';
|
|
150
|
+
const content = stripEnd(line, '\r\n');
|
|
151
|
+
const match = FORMAT_LINE.exec(content);
|
|
152
|
+
if (match === null)
|
|
153
|
+
return null;
|
|
154
|
+
const valueAt = content.length - (match.groups?.value ?? '').length - (match.groups?.rest ?? '').length;
|
|
155
|
+
lines[at] = content.slice(0, valueAt) + current + (match.groups?.rest ?? '') + line.slice(content.length);
|
|
156
|
+
return lines.join('');
|
|
157
|
+
}
|
|
158
|
+
/** `text` declaring `current`: a `format` line inserted or its value replaced, verified to change only that key; null when it cannot be. */
|
|
159
|
+
export function stampFormat(text, current) {
|
|
160
|
+
const before = parsed(text);
|
|
161
|
+
if (before === null || (before.value !== null && !(before.value instanceof Map)))
|
|
162
|
+
return null;
|
|
163
|
+
const original = before.value ?? new Map();
|
|
164
|
+
const stamped = original.has(FORMAT_KEY) ? replaced(text, current) : inserted(text, current);
|
|
165
|
+
if (stamped === null)
|
|
166
|
+
return null;
|
|
167
|
+
const after = parsed(stamped);
|
|
168
|
+
if (after === null)
|
|
169
|
+
return null;
|
|
170
|
+
const expected = new Map([...original, [FORMAT_KEY, current]]);
|
|
171
|
+
return unorderedRepr(normalize(after.value)) === unorderedRepr(normalize(expected)) ? stamped : null;
|
|
172
|
+
}
|