Last active
October 15, 2020 01:04
-
-
Save wperron/7d777b9120f0e21ccd6b5b5b33d03b88 to your computer and use it in GitHub Desktop.
migrating deno registry modules to include the repository ID
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 { assert } from "https://deno.land/[email protected]/testing/asserts.ts"; | |
import { pooledMap } from "https://deno.land/[email protected]/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