-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (93 loc) · 2.54 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env node
import chalk from "chalk";
import inquirer from "inquirer";
import { exec } from "node:child_process";
import { Octokit } from "octokit";
import ora from "ora";
let GitHubToken = "";
const { fetchToken } = await inquirer.prompt([
{
type: "confirm",
name: "fetchToken",
message:
"Do you give me permission to fetch your GitHub token via `gh token`?",
},
]);
let spinner = ora("Fetching your GitHub token...");
if (fetchToken) {
spinner.start();
GitHubToken = await new Promise((resolve, reject) => {
exec("gh auth token", (error, stdout, stderr) => {
if (error || stderr) {
console.error(error || stderr);
reject(error || stderr);
}
resolve(stdout.trim());
});
});
} else {
const { token } = await inquirer.prompt([
{
type: "input",
name: "token",
message:
"What is your GitHub token? (must have permissions for fetching and archiving repos)",
},
]);
GitHubToken = token;
}
const octokit = new Octokit({ auth: GitHubToken });
const {
data: { login },
} = await octokit.rest.users.getAuthenticated();
spinner.succeed(
`Fetched your GitHub token and successfully authenticated as ${chalk.green(
login
)}!`
);
spinner = ora("Fetching your repos...").start();
const { data: repos } = await octokit.rest.repos.listForAuthenticatedUser({
sort: "updated",
per_page: 100,
type: "owner",
});
const frepos = repos //
.reverse()
.filter((repo) => repo.private === false)
.filter((repo) => !repo.archived)
.filter((repo) => !repo.fork);
spinner.succeed(
`Fetched ${chalk.green(frepos.length)} public repos! ${chalk.red(
"(excluding forks and archived repos)"
)}!`
);
let archived = 0;
process.on("exit", () => {
console.log(`Archived ${chalk.green(archived)} repos!`);
});
for (const repo of frepos) {
const { name, size, stargazers_count } = repo;
const { archiveRepo } = await inquirer.prompt([
{
type: "confirm",
name: "archiveRepo",
message: `${login}/${name} (${size}kb, ${stargazers_count} stars)`,
},
]);
if (archiveRepo) {
spinner = ora(`Archiving ${login}/${name}...`).start();
try {
await octokit.rest.repos.update({
owner: login,
repo: name,
archived: true,
});
archived += 1;
spinner.succeed(`Archived ${chalk.green(`${login}/${name}`)}!`);
} catch (error) {
spinner.fail(`Failed to archive ${chalk.red(`${login}/${name}`)}!`);
console.error(error);
console.log(chalk.gray("continuing..."));
}
}
}