-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
67 lines (54 loc) · 2.08 KB
/
Copy pathcli.js
File metadata and controls
67 lines (54 loc) · 2.08 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
const yargs = require("yargs/yargs");
const { Octokit } = require("@octokit/rest");
let { graphql } = require("@octokit/graphql");
const { hideBin } = require("yargs/helpers");
const fs = require("node:fs/promises");
const path = require("node:path");
const { GITHUB_API_URL } = require("./util/constants");
(async () => {
const scriptsDir = path.join(__dirname, "scripts");
const files = await fs.readdir(scriptsDir);
let argv = yargs(hideBin(process.argv))
.option("token", {
alias: "t",
describe: "GitHub personal access token to use when making API calls",
demandOption: true,
})
.option("api-url", {
alias: "u",
describe: "When using with GitHub Enterprise Server, set this to the root URL of the API. Example: https://github.my-domain.com/api/v3",
})
.default("token", () => process.env.GITHUB_TOKEN, "GITHUB_TOKEN environment variable")
.default("api-url", () => process.env.GITHUB_API_URL || GITHUB_API_URL, `GITHUB_API_URL environment variable, or ${GITHUB_API_URL} if not specified`);
const actions = {};
for (const file of files) {
const { description, options, action } = require(path.join(scriptsDir, file));
const { name } = path.parse(file);
actions[name] = action;
argv = argv.command(name, description, options);
}
argv = argv
.wrap(null) // eslint-disable-line unicorn/no-null
.help()
.demandCommand()
.strict()
.argv;
const octokit = new Octokit({
auth: argv.token,
baseUrl: argv["api-url"],
});
let graphqlEndPoint = "";
if (!argv["api-url"].includes("api.github.com")) {
graphqlEndPoint = argv["api-url"].slice(0, argv["api-url"].lastIndexOf("/"));
} else {
graphqlEndPoint = argv["api-url"];
}
graphql = graphql.defaults({
baseUrl: graphqlEndPoint,
headers: {
authorization: `token ${argv.token}`,
},
});
console.log();
await actions[argv._[0]](octokit, graphql, argv);
})();