-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.js
105 lines (93 loc) · 3.05 KB
/
utils.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
const chalk = require('chalk');
const { graphql } = require("@octokit/graphql");
const argv = require('minimist')(process.argv.slice(2));
const _ = require('lodash');
const g = chalk.green;
const y = chalk.yellow;
const { owner, repo, token } = fmtArgs();
const graphqlClient = graphql.defaults({
headers: {
authorization: `token ${token}`,
},
});
module.exports = {
graphqlClient,
cmdHelp,
getDiscussionsTotal,
getIssuesTotal,
fmtJsonKey,
fmtArgs,
};
function cmdHelp() {
return console.log(`
usage: ${g`rgd`}
options:
${g`--owner`}: github username
${g`--repo`}: github repository
${g`--type`}: discussions | discussions2 | issues, default is \`discussions\`
${g`--issues-owner`}: github username(issues) - data owner
${g`--issues-repo`}: github repository(issues) - data repo
${g`--dis-owner`}: github username(discussions2) - data owner
${g`--dis-repo`}: github repository(discussions2) - data repo
${g`--issues-state`}: github issues states (issues), \`OPEN\` or \`CLOSED\`, by default no filtering
${g`--mode`}: api generates json files, rss files, etc. default ${y`rss`}
example: ${y`--mode=json,rss`}
${g`--jsonfmt`}: beautify json, default ${y`false`}
${g`--jsontype`}: \`md\` or \`html\`, default ${y`html`}
${g`--token`}: generate token -> https://github.com/settings/tokens/new
${g`--limit`}: if not set, all are requested by default, value is number, no more than 100
${g`--outdir`}: output file root directory, default \`${y`.`}\`
${g`--filename`}: rss file name, default ${y`feed.xml`}
${g`--site-title`}: default ${y`RSS`}
${g`--site-link`}: defalut ${y`/`}
${g`--site-desc`}: defalut ${y`GitHub Discussions`}`);
}
async function getDiscussionsTotal(_owner = owner, _repo = repo) {
const _data = await graphqlClient(`
query ($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
discussions {
totalCount
}
}
}
`, {
owner: _owner,
repo: _repo,
});
return _data.repository.discussions.totalCount;
}
async function getIssuesTotal() {
const _data = await graphqlClient(`
query ($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
issues {
totalCount
}
}
}
`, {
owner: argv['issues-owner'],
repo: argv['issues-repo'],
});
return _data.repository.issues.totalCount;
}
function fmtJsonKey(data) {
_.mixin({
deeply: function (map) {
return function(obj, fn) {
return map(_.mapValues(obj, function (v) {
return _.isPlainObject(v) ? _.deeply(map)(v, fn) : v;
}), fn);
}
},
});
return _.deeply(_.mapKeys)(data, function (_, key) {
return key.replace(/-/g, '_');
});
}
function fmtArgs() {
const { owner, repo, repository = '', token, ...rest } = argv;
const a = repository?.split('/');
return { owner: a[0] || owner, repo: a[1] || repo, token, ...rest };
}