-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (46 loc) · 1.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import * as child_process from "child_process";
import * as path from "path";
import { Octokit } from "octokit";
async function githubBackup(octokit, destination) {
const repositories = await octokit.paginate("GET /user/repos", {
type: "owner",
sort: "full_name"
});
console.log(`Found ${repositories.length} repositories.`);
for (const repository of repositories) {
await cloneRepository(repository, destination);
}
console.log("Done.");
}
async function cloneRepository(repository, destination) {
console.log(`Cloning ${repository.full_name} to ${destination} ...`);
// Sanitize input
const cloneURL = new URL(repository.clone_url);
const cloneDirectory = path.resolve(destination, repository.name);
child_process.execSync(`git clone ${cloneURL} ${cloneDirectory}`);
}
const USAGE = "gh-backup DESTINATION";
async function main(args) {
try {
// Get the destination directory.
if (args.length !== 3) {
throw new Error(USAGE);
}
const destination = args[2];
// Get the user's personal access token.
const pat = process.env["GITHUB_TOKEN"];
if (!pat) {
throw new Error("The `GITHUB_TOKEN` environment variable must be set to a valid personal access token.");
}
const octokit = new Octokit({
auth: pat
});
// Run the backup.
await githubBackup(octokit, destination);
return 0;
} catch (e) {
console.error(e.message);
return 1;
}
}
await main(process.argv);