-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgistpush-list.js
129 lines (107 loc) · 3.11 KB
/
gistpush-list.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
117
118
119
120
121
122
123
124
125
126
127
128
129
const Gists = require("gists");
const chalk = require("chalk");
const program = require("commander");
const prompt = require("prompt-sync")(); // Sync cli input
const CLI = require("clui");
const Spinner = CLI.Spinner;
const { utils } = require("./utils");
const logError = error => {
console.log(error);
console.error(chalk.red(error));
process.exit(1);
};
const checkAndPrintGists = gists => {
if (!gists || gists.length === 0) console.log(chalk.green("Nothing to show"));
else {
gists.forEach(utils.printGists);
console.log(chalk.green("Done"));
}
};
const initializeLoader = () => {
const countdown = new Spinner("Fetching gists... ", [
"⣾",
"⣽",
"⣻",
"⢿",
"⡿",
"⣟",
"⣯",
"⣷"
]);
console.log("Fetching... ");
countdown.start();
return countdown;
};
const getGists = async ({ credentials, username }) => {
const areCredentials = credentials && Object.keys(credentials).length > 0;
const gist = new Gists(areCredentials ? { ...credentials } : undefined);
const { body } = await gist
.list(username ? username : undefined)
.catch(logError);
return body;
};
program
.option(
"-t, --token <token>",
"Set token to log in into GitHub (use it when you have two-factor auth activated in your GitHub account)"
)
.option(
"--no-login",
"Do not log in into GitHub. If token provided, this will be ignored. This option only shows public repos"
)
.parse(process.argv);
const usernameGists = program.args[0];
const login = program.login;
const secret = program.token ? program.token.trim() : undefined;
if (!login) {
(async function() {
const username =
usernameGists || prompt(chalk.yellow("Username to get gists: "));
if (!username) logError("Username not provided");
const spinner = initializeLoader();
const gists = await getGists({ username }).catch(logError);
checkAndPrintGists(gists);
spinner.stop();
console.log("\n");
process.exit(0);
})();
} else if (secret) {
(async function() {
console.log(chalk.cyan("Getting Gists..."));
const spinner = initializeLoader();
const gists = await getGists({
credentials: { token: secret },
username: usernameGists ? usernameGists : undefined
}).catch(logError);
checkAndPrintGists(gists);
spinner.stop();
console.log("\n");
process.exit(0);
})();
} else {
const fromPropt = prompt(
chalk.yellow(
"Username to log in (push enter to use the username argument): "
)
);
const username = fromPropt ? fromPropt : usernameGists;
if (!username) {
console.error(chalk.red("Username not provided"));
process.exit(1);
}
const password = prompt.hide(chalk.yellow("Password: "));
if (!password) {
console.error(chalk.red("Password or Token not provided"));
process.exit(1);
}
(async function() {
const spinner = initializeLoader();
const gists = await getGists({
credentials: { username, password },
username: usernameGists ? usernameGists : username
}).catch(logError);
checkAndPrintGists(gists);
spinner.stop();
process.exit(0);
})();
}