Skip to content

Instantly share code, notes, and snippets.

@lambdalisue
Last active August 3, 2024 05:54
Show Gist options
  • Save lambdalisue/cd173d1f7e9787ed9842a9f1c51f48fe to your computer and use it in GitHub Desktop.
Save lambdalisue/cd173d1f7e9787ed9842a9f1c51f48fe to your computer and use it in GitHub Desktop.
unknownutil v3 to v4 migration
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);
}
}
@lambdalisue
Copy link
Author

lambdalisue commented Aug 3, 2024

deno run -A https://gist.githubusercontent.com/lambdalisue/cd173d1f7e9787ed9842a9f1c51f48fe/raw/be7f12fa222813b814b93180cf49808af06b4ea8/unknownutil-v3-to-v4-migration.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment