|
6 | 6 | plumbing, |
7 | 7 | SemVer, |
8 | 8 | semver, |
| 9 | + utils, |
9 | 10 | } from "https://deno.land/x/libpkgx@v0.20.3/mod.ts"; |
10 | 11 | import { dirname, fromFileUrl, join } from "jsr:@std/path@^1"; |
11 | 12 | import { ensureDir, existsSync, walk } from "jsr:@std/fs@^1"; |
@@ -86,7 +87,13 @@ if (parsedArgs.help) { |
86 | 87 | break; |
87 | 88 | case "up": |
88 | 89 | case "update": |
| 90 | + case "upgrade": |
| 91 | + await update(); |
| 92 | + break; |
| 93 | + |
89 | 94 | case "pin": |
| 95 | + console.error("%cU EARLY! soz, not implemented", "color:red"); |
| 96 | + Deno.exit(1); |
90 | 97 | break; |
91 | 98 | case "outdated": |
92 | 99 | await outdated(); |
@@ -650,3 +657,66 @@ async function* walk_pkgs() { |
650 | 657 | } |
651 | 658 | } |
652 | 659 | } |
| 660 | + |
| 661 | +async function update() { |
| 662 | + const pkgs: Installation[] = []; |
| 663 | + for await (const pkg of walk_pkgs()) { |
| 664 | + pkgs.push(pkg); |
| 665 | + } |
| 666 | + |
| 667 | + const { pkgs: raw_graph } = await hydrate( |
| 668 | + pkgs.map((x) => ({ |
| 669 | + project: x.pkg.project, |
| 670 | + constraint: new semver.Range(`^${x.pkg.version}`), |
| 671 | + })), |
| 672 | + ); |
| 673 | + const graph: Record<string, semver.Range> = {}; |
| 674 | + for (const { project, constraint } of raw_graph) { |
| 675 | + graph[project] = constraint; |
| 676 | + } |
| 677 | + |
| 678 | + const local_update_list = []; |
| 679 | + const system_update_list = []; |
| 680 | + |
| 681 | + for (const { path, pkg } of pkgs) { |
| 682 | + const versions = await hooks.useInventory().get(pkg); |
| 683 | + // console.log(pkg, graph[pkg.project]); |
| 684 | + const constrained_versions = versions.filter((x) => |
| 685 | + graph[pkg.project].satisfies(x) && x.gt(pkg.version) |
| 686 | + ); |
| 687 | + |
| 688 | + if (constrained_versions.length) { |
| 689 | + const pkgspec = `${pkg.project}=${constrained_versions.slice(-1)[0]}`; |
| 690 | + if (path.string.startsWith("/usr/local")) { |
| 691 | + system_update_list.push(pkgspec); |
| 692 | + } else { |
| 693 | + local_update_list.push(pkgspec); |
| 694 | + } |
| 695 | + } |
| 696 | + } |
| 697 | + |
| 698 | + for (const pkgspec of local_update_list) { |
| 699 | + const pkg = utils.pkg.parse(pkgspec); |
| 700 | + console.log( |
| 701 | + "updating:", |
| 702 | + Path.home().join(".local/pkgs", pkg.project), |
| 703 | + "to", |
| 704 | + pkg.constraint.single(), |
| 705 | + ); |
| 706 | + } |
| 707 | + for (const pkgspec of system_update_list) { |
| 708 | + const pkg = utils.pkg.parse(pkgspec); |
| 709 | + console.log( |
| 710 | + "updating:", |
| 711 | + new Path("/usr/local/pkgs").join(pkg.project), |
| 712 | + "to", |
| 713 | + pkg.constraint.single(), |
| 714 | + ); |
| 715 | + } |
| 716 | + |
| 717 | + if (local_update_list.length) { |
| 718 | + await install(local_update_list, Path.home().join(".local/bin").string); |
| 719 | + } else { |
| 720 | + await install(system_update_list, "/usr/local"); |
| 721 | + } |
| 722 | +} |
0 commit comments