Skip to content

Instantly share code, notes, and snippets.

@wperron
Last active October 15, 2020 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wperron/7d777b9120f0e21ccd6b5b5b33d03b88 to your computer and use it in GitHub Desktop.
Save wperron/7d777b9120f0e21ccd6b5b5b33d03b88 to your computer and use it in GitHub Desktop.
migrating deno registry modules to include the repository ID
import { assert } from "https://deno.land/std@0.73.0/testing/asserts.ts";
import { pooledMap } from "https://deno.land/std@0.73.0/async/pool.ts";
import { Database } from "../utils/database.ts";
const db = new Database(Deno.env.get("MONGO_URI") ?? "");
const GH_TOKEN = Deno.env.get("GH_TOKEN");
assert(GH_TOKEN);
const all = await db._modules.find({});
await Deno.writeTextFile(
`./backup-${new Date().toISOString()}.json`,
JSON.stringify(all),
);
pooledMap(10, all, async (mod) => {
try {
const gh = await fetch(
`https://api.github.com/repos/${mod.owner}/${mod.repo}`,
{ headers: { Authorization: `Bearer ${GH_TOKEN}` } },
)
.then(async (res) => {
if (res.status === 200) {
return res.json();
}
throw new Error(`Failed ${mod._id}: ${await res.text()}`);
});
mod.repo_id = gh.id!;
await db._modules.updateOne(
// deno-lint-ignore no-explicit-any
{ _id: mod._id as any },
// deno-lint-ignore no-explicit-any
mod as any,
{ upsert: true },
);
} catch (err) {
console.log("%cfailed for " + mod._id, "color: red");
}
console.log("%cdone " + mod._id, "color: green");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment