@onrails/codemod 0.1.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 +57 -0
- package/package.json +47 -0
- package/src/index.ts +137 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alan R. Soares
|
|
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,57 @@
|
|
|
1
|
+
# @onrails/codemod
|
|
2
|
+
|
|
3
|
+
Bun-based codemods for migrating onto `@onrails/result`.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add -d @onrails/codemod
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or run without installing:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bunx @onrails/codemod /path/to/target-repo --dry
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## What it does
|
|
18
|
+
|
|
19
|
+
Rewrites every `from "neverthrow"` import and every `neverthrow` `package.json` dep to point at `@onrails/result/compat/neverthrow`.
|
|
20
|
+
|
|
21
|
+
- Walks `.ts` / `.tsx` / `.mts` / `.cts`. Skips `node_modules`, `dist`, `.git`, `.next`, `.turbo`, `coverage`, `build`.
|
|
22
|
+
- Rewrites only the specifier in `import` / `from` / `import(...)`; string mentions of `"neverthrow"` in regular code are left alone.
|
|
23
|
+
- Updates `dependencies` / `devDependencies` / `peerDependencies` in every nested `package.json` that lists `neverthrow`, replacing it with `@onrails/result` at a path relative to that `package.json`.
|
|
24
|
+
- Idempotent. Safe to re-run.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
# dry run
|
|
30
|
+
bunx @onrails/codemod /path/to/target-repo --dry
|
|
31
|
+
|
|
32
|
+
# apply
|
|
33
|
+
bunx @onrails/codemod /path/to/target-repo
|
|
34
|
+
|
|
35
|
+
# point at a local @onrails/result checkout (file: install)
|
|
36
|
+
bunx @onrails/codemod /path/to/target-repo \
|
|
37
|
+
--onrails=/Users/me/dev/onrails/packages/result
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
After applying:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
cd target-repo
|
|
44
|
+
rm -rf node_modules/.bun/@onrails* node_modules/@onrails # force-refresh file: deps if using a local checkout
|
|
45
|
+
bun install
|
|
46
|
+
bun typecheck
|
|
47
|
+
bun test
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Roadmap
|
|
51
|
+
|
|
52
|
+
- Stage-2 codemod (compat → native `@onrails/result` API): planned, not shipped. Tracking in the repo issues.
|
|
53
|
+
- Generalized rename codemod (any `from "A"` → `from "B"` + deps): planned.
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@onrails/codemod",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Codemods for migrating onto @onrails/result (neverthrow → compat shim, and beyond)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Alan R. Soares",
|
|
7
|
+
"homepage": "https://github.com/alanrsoares/onrails/tree/main/packages/codemod#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/alanrsoares/onrails.git",
|
|
11
|
+
"directory": "packages/codemod"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/alanrsoares/onrails/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"codemod",
|
|
18
|
+
"neverthrow",
|
|
19
|
+
"migration",
|
|
20
|
+
"result",
|
|
21
|
+
"onrails",
|
|
22
|
+
"bun"
|
|
23
|
+
],
|
|
24
|
+
"engines": {
|
|
25
|
+
"bun": ">=1.0"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"provenance": true
|
|
30
|
+
},
|
|
31
|
+
"type": "module",
|
|
32
|
+
"sideEffects": false,
|
|
33
|
+
"bin": {
|
|
34
|
+
"onrails-codemod-neverthrow": "./src/index.ts"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"src",
|
|
38
|
+
"README.md",
|
|
39
|
+
"LICENSE"
|
|
40
|
+
],
|
|
41
|
+
"exports": {
|
|
42
|
+
".": "./src/index.ts"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"typecheck": "tsc --noEmit"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { relative, resolve } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Codemod: migrate `neverthrow` imports + package.json deps to
|
|
5
|
+
* `@onrails/result/compat/neverthrow`.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* bun run packages/result/scripts/codemod-neverthrow.ts <target-dir> [--dry] [--onrails=<abs-path>]
|
|
9
|
+
*
|
|
10
|
+
* - Rewrites import specifiers `"neverthrow"` -> `"@onrails/result/compat/neverthrow"`
|
|
11
|
+
* in `.ts` / `.tsx` / `.mts` / `.cts` files.
|
|
12
|
+
* - Updates every `package.json` that lists `neverthrow` under
|
|
13
|
+
* `dependencies` / `devDependencies` / `peerDependencies`:
|
|
14
|
+
* - removes the `neverthrow` entry
|
|
15
|
+
* - adds `@onrails/result` with a `file:` path relative to the package.json
|
|
16
|
+
* - Skips `node_modules`, `dist`, `.git`, `.next`, `.turbo`, `coverage`.
|
|
17
|
+
* - Idempotent. Safe to re-run.
|
|
18
|
+
*/
|
|
19
|
+
import { Glob } from "bun";
|
|
20
|
+
|
|
21
|
+
type Args = { target: string; dry: boolean; onrails: string };
|
|
22
|
+
|
|
23
|
+
const SKIP = new Set(["node_modules", "dist", ".git", ".next", ".turbo", "coverage", "build"]);
|
|
24
|
+
const CODE_EXT = /\.(ts|tsx|mts|cts)$/;
|
|
25
|
+
const IMPORT_RE = /(from\s+|import\s*\(\s*)(['"])neverthrow\2/g;
|
|
26
|
+
const NEW_SPEC = "@onrails/result/compat/neverthrow";
|
|
27
|
+
const DEP_KEYS = ["dependencies", "devDependencies", "peerDependencies"] as const;
|
|
28
|
+
|
|
29
|
+
function parseArgs(argv: string[]): Args {
|
|
30
|
+
const positional: string[] = [];
|
|
31
|
+
let dry = false;
|
|
32
|
+
let onrails = resolve(import.meta.dir, "../../..", "packages/result");
|
|
33
|
+
for (const a of argv) {
|
|
34
|
+
if (a === "--dry" || a === "-n") dry = true;
|
|
35
|
+
else if (a.startsWith("--onrails=")) onrails = resolve(a.slice("--onrails=".length));
|
|
36
|
+
else if (!a.startsWith("--")) positional.push(a);
|
|
37
|
+
}
|
|
38
|
+
if (positional.length !== 1) {
|
|
39
|
+
console.error(
|
|
40
|
+
"usage: bun run codemod-neverthrow.ts <target-dir> [--dry] [--onrails=<abs-path>]",
|
|
41
|
+
);
|
|
42
|
+
process.exit(2);
|
|
43
|
+
}
|
|
44
|
+
return { target: resolve(positional[0] ?? "."), dry, onrails };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function shouldSkip(path: string): boolean {
|
|
48
|
+
return path.split("/").some((seg) => SKIP.has(seg));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function* walk(root: string): AsyncGenerator<string> {
|
|
52
|
+
const glob = new Glob("**/*");
|
|
53
|
+
for await (const rel of glob.scan({ cwd: root, dot: false, onlyFiles: true })) {
|
|
54
|
+
if (!shouldSkip(rel)) yield `${root}/${rel}`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type FileChange = { path: string; before: number; after: number };
|
|
59
|
+
|
|
60
|
+
async function rewriteCode(path: string, dry: boolean): Promise<FileChange | null> {
|
|
61
|
+
const src = await Bun.file(path).text();
|
|
62
|
+
if (!src.includes("neverthrow")) return null;
|
|
63
|
+
const before = (src.match(IMPORT_RE) ?? []).length;
|
|
64
|
+
if (before === 0) return null;
|
|
65
|
+
const next = src.replace(IMPORT_RE, (_, lead, quote) => `${lead}${quote}${NEW_SPEC}${quote}`);
|
|
66
|
+
if (next === src) return null;
|
|
67
|
+
if (!dry) await Bun.write(path, next);
|
|
68
|
+
return { path, before, after: 0 };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
type PkgChange = { path: string; removed: string[]; addedAs: string };
|
|
72
|
+
|
|
73
|
+
function reorderDeps(deps: Record<string, string>): Record<string, string> {
|
|
74
|
+
return Object.fromEntries(Object.entries(deps).sort(([a], [b]) => a.localeCompare(b)));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function rewritePkg(
|
|
78
|
+
path: string,
|
|
79
|
+
onrailsAbs: string,
|
|
80
|
+
dry: boolean,
|
|
81
|
+
): Promise<PkgChange | null> {
|
|
82
|
+
const raw = await Bun.file(path).text();
|
|
83
|
+
let json: Record<string, unknown>;
|
|
84
|
+
try {
|
|
85
|
+
json = JSON.parse(raw) as Record<string, unknown>;
|
|
86
|
+
} catch {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
const removed: string[] = [];
|
|
90
|
+
let touched = false;
|
|
91
|
+
const pkgDir = path.replace(/\/package\.json$/, "");
|
|
92
|
+
const filePath = relative(pkgDir, onrailsAbs);
|
|
93
|
+
const fileSpec = `file:${filePath}`;
|
|
94
|
+
for (const key of DEP_KEYS) {
|
|
95
|
+
const deps = json[key] as Record<string, string> | undefined;
|
|
96
|
+
if (!deps || typeof deps !== "object") continue;
|
|
97
|
+
if (!("neverthrow" in deps)) continue;
|
|
98
|
+
delete deps.neverthrow;
|
|
99
|
+
removed.push(key);
|
|
100
|
+
deps["@onrails/result"] = fileSpec;
|
|
101
|
+
json[key] = reorderDeps(deps);
|
|
102
|
+
touched = true;
|
|
103
|
+
}
|
|
104
|
+
if (!touched) return null;
|
|
105
|
+
const out = `${JSON.stringify(json, null, 2)}\n`;
|
|
106
|
+
if (!dry) await Bun.write(path, out);
|
|
107
|
+
return { path, removed, addedAs: fileSpec };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function main() {
|
|
111
|
+
const { target, dry, onrails } = parseArgs(Bun.argv.slice(2));
|
|
112
|
+
const codeChanges: FileChange[] = [];
|
|
113
|
+
const pkgChanges: PkgChange[] = [];
|
|
114
|
+
for await (const file of walk(target)) {
|
|
115
|
+
if (file.endsWith("/package.json")) {
|
|
116
|
+
const c = await rewritePkg(file, onrails, dry);
|
|
117
|
+
if (c) pkgChanges.push(c);
|
|
118
|
+
} else if (CODE_EXT.test(file)) {
|
|
119
|
+
const c = await rewriteCode(file, dry);
|
|
120
|
+
if (c) codeChanges.push(c);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const label = dry ? "DRY" : "APPLY";
|
|
124
|
+
console.log(`[${label}] target=${target}`);
|
|
125
|
+
console.log(`[${label}] onrails=${onrails}`);
|
|
126
|
+
console.log(`[${label}] code files rewritten: ${codeChanges.length}`);
|
|
127
|
+
for (const c of codeChanges) {
|
|
128
|
+
console.log(` ${relative(target, c.path)} (${c.before} import${c.before === 1 ? "" : "s"})`);
|
|
129
|
+
}
|
|
130
|
+
console.log(`[${label}] package.json updated: ${pkgChanges.length}`);
|
|
131
|
+
for (const c of pkgChanges) {
|
|
132
|
+
console.log(` ${relative(target, c.path)} [${c.removed.join(", ")}] -> ${c.addedAs}`);
|
|
133
|
+
}
|
|
134
|
+
if (dry) console.log(`[${label}] no files written. re-run without --dry to apply.`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
await main();
|