-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.mjs
executable file
·92 lines (81 loc) · 2.19 KB
/
cli.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
#!/usr/bin/env node
import chalk from "chalk";
import fuzzy from "fuzzy";
import inquirer from "inquirer";
import inquirerAutocomplete from "inquirer-autocomplete-prompt";
import meow from "meow";
import open from "open";
import ora from "ora";
import escExit from "esc-exit";
import getRepos from "./getRepos.mjs";
const cli = meow(`
Usage
$ open-gh-page <gh username>
Examples
$ open-gh-page zillding
`, {
importMeta: import.meta
});
function getHomepage(repo) {
if (repo.name === `${repo.owner.login}.github.io`) {
return `https://${repo.name}/`;
}
return repo.homepage || `https://${repo.owner.login}.github.io/${repo.name}`;
}
function filterRepos(input, repos) {
return repos.filter(o => fuzzy.test(input || "", o.short));
}
function handleUsername(username) {
const spinner = ora(
`Fetching repos of ${chalk.green.underline(username)} ...`
).start();
getRepos(username)
.then(repos =>
repos.filter(o => o.has_pages).map(o => ({
name: `${o.name} ${chalk.dim(getHomepage(o))}`,
value: getHomepage(o),
short: o.name
}))
)
.then(result => {
spinner.stop();
if (result.length === 0) {
console.warn(chalk.yellow("No public github pages available."));
return;
}
inquirer.registerPrompt(
"autocomplete",
inquirerAutocomplete
);
const question = {
type: "autocomplete",
name: "url",
message: "Select a github page:",
source: (_, input) => Promise.resolve(filterRepos(input, result))
};
inquirer.prompt([question]).then(answer => {
open(answer.url);
});
})
.catch(err => {
spinner.stop();
console.error(chalk.red("Failed to fetch repos."));
throw err;
});
}
function handleInit() {
const question = {
name: "username",
message: "Provide a github username:"
};
inquirer.prompt([question]).then(answer => {
if (answer.username) return handleUsername(answer.username);
console.error(chalk.red("Please provide a valid github username."));
});
}
function init(username) {
escExit();
if (username) return handleUsername(username);
return handleInit();
}
init(cli.input[0]);