Skip to content

Instantly share code, notes, and snippets.

@sigmaSd
Last active February 27, 2023 14:11
Show Gist options
  • Save sigmaSd/7b2630eeb0d8ca0bb237ffaa354d9f95 to your computer and use it in GitHub Desktop.
Save sigmaSd/7b2630eeb0d8ca0bb237ffaa354d9f95 to your computer and use it in GitHub Desktop.
Update nim binaries installed through nimble
import home_dir from "https://deno.land/x/dir@1.5.1/home_dir/mod.ts";
const updateAll = Deno.args.find((a) => a === "--update-all");
const binsPath = home_dir() + "/.nimble/bin";
for (const dirEntry of Deno.readDirSync(binsPath)) {
if (!dirEntry.isSymlink) continue;
const realBinPath = Deno.realPathSync(binsPath + "/" + dirEntry.name);
// The pkg name can differ from the binary name
const pkg = realBinPath.split("/").at(-2);
if (!pkg) throw "failed to read pkg from " + realBinPath;
const [name, version] = pkg.split("-");
const pkgPath = realBinPath.split("/").slice(0, -1).join("/");
const meta = JSON.parse(
Deno.readTextFileSync(pkgPath + "/" + "nimblemeta.json"),
);
const pkgUrl = meta.url ? meta.url : meta.metaData.url;
const upstreamVersion = await getLastTag(pkgUrl);
console.log(`Pacakge ${name}`);
console.log(`Installed version: ${version}`);
console.log(`Upstream version: ${upstreamVersion}`);
if (version != upstreamVersion) {
if (updateAll || confirm("Do you want to update?")) {
await new Deno.Command("nimble", { args: ["install", name] }).spawn()
.status;
}
}
console.log();
}
async function getLastTag(url: string) {
const parsedUrl = parseGitHubUrl(url);
if (!parsedUrl) throw "incorrect github url";
const { owner, repo } = parsedUrl;
const response = await fetch(
`https://api.github.com/repos/${owner}/${repo}/tags`,
);
const tags = await response.json();
if (tags.length > 0) {
const name = tags[0].name;
// Handle tags here
if (name.startsWith("v")) return name.slice(1);
else return name;
} else {
return null;
}
}
function parseGitHubUrl(url: string) {
const match = url.match(/^https:\/\/github.com\/([^/]+)\/([^/]+)\/?$/);
if (match) {
const owner = match[1];
const repo = match[2];
return { owner, repo };
} else {
return null;
}
}
@sigmaSd
Copy link
Author

sigmaSd commented Feb 26, 2023

deno run https://gist.github.com/sigmaSd/7b2630eeb0d8ca0bb237ffaa354d9f95/raw/f0d37b50808c6a83eece0c0756bfa1c029e24d72/nimup.ts

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