-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·221 lines (196 loc) · 5.43 KB
/
cli.js
File metadata and controls
executable file
·221 lines (196 loc) · 5.43 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const meow = require('meow');
const userRepos = require('all-repos');
const npmCurrentUser = require('npm-current-user');
const ora = require('ora');
const inquirer = require('inquirer');
const columnify = require('columnify');
const termSize = require('term-size');
const opn = require('opn');
const Fuse = require('fuse.js');
const rimraf = require('rimraf');
let repos = [];
let isNotUpdate = true;
let prompTitle = 'Type to search repos: ';
let spinner;
const cli = meow(`
Usage
$ repos
$ repos <github-username>
Options
--update, --u Update repositories data to latest
--updateUser, --uu Update default user (Since 'npm whoami' is quite slow)
--clearCache, --cc Clear local cache
Examples
$ repos Get local repositories of current user
$ repos banminkyoz Get local repositories of 'banminkyoz'
$ repos --u Update repositories of current user to latest
$ repos --u banminkyoz Update repositories of 'banminkyoz' to latest
$ repos --uu Update npm current logged user
$ repos --cc Clear all local cache
`, {
flags: {
update: {
type: 'boolean',
alias: 'u'
},
updateUser: {
type: 'boolean',
alias: 'uu'
},
clearCache: {
type: 'boolean',
alias: 'cc'
}
}
});
if (cli.flags.clearCache || cli.flags.cc) {
rimraf(`${__dirname}/data/`, () => {
console.log(`Cleared cache !`);
});
} else {
startCLI();
}
function startCLI() {
// Start
console.clear();
spinner = ora(`Getting ${cli.input.length > 0 ? cli.input + '\'s' : 'your'} github repos infomation...`).start();
spinner.color = 'blue';
if (!fs.existsSync(`${__dirname}/data`)) {
fs.mkdirSync(`${__dirname}/data`);
}
getGithubUsername().then(githubUsername => {
const dataPath = `${__dirname}/data/${githubUsername}.json`;
if (isNotUpdate && fs.existsSync(dataPath)) {
repos = formatRepos(require(dataPath));
prompTitle = 'Type to search repos: (local): ';
initPrompt('');
return;
}
prompTitle = 'Type to search repos: (up-to-date): ';
userRepos(githubUsername).then(_repos => {
repos = formatRepos(_repos);
// Save data
fs.writeFile(dataPath, JSON.stringify(_repos), err => {
if (err) {
console.error(err);
}
});
initPrompt();
});
});
}
function initPrompt() {
spinner.stop();
console.log('');
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
inquirer.prompt([{
type: 'autocomplete',
name: 'from',
message: prompTitle,
source: searchRepos
}]).then(answers => {
opn(answers.from.url, {wait: false});
console.clear();
});
}
function getGithubUsername() {
return new Promise(resolve => {
if (cli.flags.u) {
isNotUpdate = false;
if (cli.flags.u !== true) {
resolve(cli.flags.u);
}
}
if (cli.flags.update) {
isNotUpdate = false;
if (cli.flags.update !== true) {
resolve(cli.flags.update);
}
}
if (cli.input.length > 0) {
resolve(cli.input);
}
const dataPath = `${__dirname}/data/current_user.json`;
if (cli.flags.uu || cli.flags.updateUser || !fs.existsSync(dataPath)) {
npmCurrentUser().then(info => {
fs.writeFile(dataPath, JSON.stringify({name: `${info.github}`}), err => {
if (err) {
console.error(err);
}
});
resolve(info && info.github ? info.github : null);
});
} else {
const githubUser = require(dataPath);
resolve(githubUser.name);
}
});
}
function formatRepos(repos) {
let nameSize = 10;
for (const repo of repos) {
if (repo.name.length > nameSize) {
nameSize = repo.name.length;
}
}
nameSize += 2;
const results = [];
const forkSize = 6;
const starSize = 14;
const termimalSize = termSize();
const descriptionSize = termimalSize.columns - nameSize - forkSize - starSize - 10;
for (const repo of repos) {
const description = repo.description ? repo.description.length <= descriptionSize ?
repo.description : repo.description.slice(0, descriptionSize - 3) + '...' : '';
results.push({
name: columnify([{
name: repo.name,
description: description.replace(/[^ -~]+/g, ''),
forked: repo.forkFrom ? '(Fork)' : '',
star: repo.stars + ' ⭐️'
}], {
showHeaders: false,
maxLineWidth: 180,
config: {
name: {
minWidth: nameSize,
maxWidth: nameSize
},
description: {
minWidth: descriptionSize,
maxWidth: descriptionSize
},
forked: {
minWidth: forkSize,
maxWidth: forkSize
},
star: {
minWidth: starSize,
maxWidth: starSize,
align: 'right'
}
}
}),
value: repo
});
}
return results;
}
function searchRepos(answers, input) {
input = input || '';
const options = {
shouldSort: true,
threshold: 0.6,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: ['name']
};
const fuse = new Fuse(repos, options); // "list" is the item array
const searchResult = fuse.search(input);
return Promise.resolve(searchResult.length > 0 ? searchResult : input === '' ? repos : []);
}