Last active
August 3, 2024 05:54
-
-
Save lambdalisue/cd173d1f7e9787ed9842a9f1c51f48fe to your computer and use it in GitHub Desktop.
unknownutil v3 to v4 migration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { walk } from "jsr:@std/fs/walk"; | |
const version = "^4.0.0"; | |
function migrate(text: string): string { | |
text = text.replace( | |
/import(\s+){(.*?)}(\s+)from(\s+)"jsr:@core\/unknownutil@\^?3.*?"/sg, | |
`import$1{$2}$3from$4"jsr:@core/unknownutil@${version}"`, | |
); | |
text = text.replace(/is\.RecordLike/g, "is.Record"); | |
text = text.replace(/is\.RecordLikeOf\((.*?)\)/sg, "is.RecordOf($1)"); | |
text = text.replace( | |
/is\.ReadonlyTupleOf\((.*?)\)/sg, | |
"is.ReadonlyOf(is.Tuple($1))", | |
); | |
text = text.replace( | |
/is\.ReadonlyUniformTupleOf\((.*?)\)/sg, | |
"is.ReadonlyOf(is.UniformTuple($1))", | |
); | |
text = text.replace(/is\.OneOf\((.*?)\)/sg, "is.UnionOf($1)"); | |
text = text.replace(/is\.AllOf\((.*?)\)/sg, "is.IntersectionOf($1)"); | |
text = text.replace(/is\.BigInt/g, "is.Bigint"); | |
text = text.replace(/is\.OptionalOf/g, "as.Optional"); | |
if (text.includes("as.Optional")) { | |
text = text.replace( | |
/import(\s+){(.*?)\bis\b(.*?)}(\s+)from/sg, | |
"import$1{$2as, is$3}$4from", | |
); | |
} | |
return text; | |
} | |
for await (const entry of walk(".")) { | |
if (entry.path === import.meta.filename) continue; | |
if (entry.isFile && entry.name.endsWith(".ts")) { | |
let text = await Deno.readTextFile(entry.path); | |
text = migrate(text); | |
await Deno.writeTextFile(entry.path, text); | |
} | |
} |
Author
lambdalisue
commented
Aug 3, 2024
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment