-
Notifications
You must be signed in to change notification settings - Fork 1
/
etiquette.mjs
executable file
·113 lines (92 loc) · 2.74 KB
/
etiquette.mjs
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
#! /usr/bin/env -S zx --install
import { $, fs, echo, spinner, chalk } from 'zx'
import repoUrl from 'get-repository-url';
$.verbose = false;
let repositories = [];
let failed = []
try {
await checkGHAuth();
const flatdeps = await getDependencies();
await fetchRepositories(flatdeps);
const uniqueRepos = getUniqueRepos(repositories);
echoRepoCount(uniqueRepos);
for (let repo of uniqueRepos) {
await starRepo(repo);
}
echoFailedRepos(failed);
} catch (p) {
handleError(p);
}
async function checkGHAuth() {
await $`gh && gh auth status`;
}
async function getDependencies() {
let {
dependencies = {},
devDependencies = {},
peerDependencies = {}
} = await fs.readJson('./package.json')
return [
...Object.keys(dependencies),
...Object.keys(devDependencies),
...Object.keys(peerDependencies)
];
}
async function fetchRepositories(flatdeps) {
await spinner(
`Getting the url for ${flatdeps.length} repositor${ flatdeps.length > 1 ? 'ies' : 'y' }.`,
async () => {
repositories = await Promise.all(flatdeps.map(async (dep) => {
try {
const resp = await repoUrl(dep)
if (resp !== 'https://github.com/null') {
return resp
}
} catch {}
failed.push(dep)
return null
}))
}
);
}
function getUniqueRepos(repositories) {
return [...new Set(repositories)].filter(Boolean).map((it) => it.split('/').slice(-2))
}
function echoRepoCount(uniqueRepos) {
if (uniqueRepos.length) {
echo`Found ${uniqueRepos.length} unique repositor${ uniqueRepos.length > 1 ? 'ies' : 'y' }.`
} else {
echo`No unique repositories found.`
}
}
async function starRepo(repo) {
let [owner, name] = repo;
try {
await spinner(
`Starring ${chalk.bold(owner)}/${chalk.bold(name)}...`,
async () => $`gh api \
--method PUT \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/user/starred/${owner}/${name}`
)
echo`${chalk.yellowBright('★')} ${chalk.bold(owner)}/${chalk.bold(name)}`
await new Promise(resolve => setTimeout(resolve, 1000));
} catch (e) {
echo`${chalk.red('☆')} ${chalk.bold(owner)}/${chalk.bold(name)}`
}
}
function echoFailedRepos(failed) {
if (failed.length) {
failed.forEach(it => echo`${chalk.red('⚠')} ${chalk.bold(it)}`)
}
}
function handleError(p) {
if (p.message.includes('command not found')) {
echo`Are you sure ${chalk.bold('gh')} is installed? Download at ${chalk.underline('https://cli.github.com/')}.`
} else if (p.message.includes('package.json')) {
echo`Are you sure you're in the root of a ${chalk.bold('Node project')}? (package.json not found)`
} else {
echo(p.stderr || p);
}
}