Skip to content

Commit bd8a34b

Browse files
authored
feat: export @octoherd/cli/run (#16)
1 parent f45f0e2 commit bd8a34b

File tree

7 files changed

+125
-54
lines changed

7 files changed

+125
-54
lines changed

bin/cli-options.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export const cliOptions = {
2+
"octoherd-token": {
3+
description:
4+
'Requires the "public_repo" scope for public repositories, "repo" scope for private repositories.',
5+
demandOption: true,
6+
type: "string",
7+
},
8+
"octoherd-cache": {
9+
description:
10+
"Cache responses for debugging. Creates a ./cache folder if flag is set. Override by passing custom path",
11+
type: "string",
12+
},
13+
"octoherd-debug": {
14+
description: "Show debug logs",
15+
type: "boolean",
16+
default: false,
17+
},
18+
"octoherd-bypass-confirms": {
19+
description: "Bypass prompts to confirm mutating requests",
20+
type: "boolean",
21+
default: false,
22+
},
23+
};

bin/octoherd.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/env node
22

33
import yargs from "yargs";
4+
import { resolve } from "path";
45
import { VERSION as OctokitVersion } from "@octoherd/octokit";
56
import chalk from "chalk";
67

78
import { octoherd } from "../index.js";
89
import { VERSION } from "../version.js";
10+
import { cliOptions } from "./cli-options.js";
911

1012
const argv = yargs
1113
.usage("Usage: $0 [options] [script] [repos...]")
@@ -24,43 +26,40 @@ const argv = yargs
2426
default: [],
2527
});
2628
})
27-
.option("octoherd-token", {
28-
description:
29-
'Requires the "public_repo" scope for public repositories, "repo" scope for private repositories.',
30-
demandOption: true,
31-
type: "string",
32-
})
33-
.option("octoherd-cache", {
34-
description:
35-
"Cache responses for debugging. Creates a ./cache folder if flag is set. Override by passing custom path",
36-
type: "string",
37-
})
38-
.option("octoherd-debug", {
39-
description: "Show debug logs",
40-
type: "boolean",
41-
default: false,
42-
})
43-
.option("octoherd-bypass-confirms", {
44-
description: "Bypass prompts to confirm mutating requests",
45-
type: "boolean",
46-
default: false,
47-
})
48-
.epilog("copyright 2020").argv;
29+
.options(cliOptions)
30+
.version(VERSION)
31+
.epilog(`copyright 2020-${new Date().getFullYear()}`).argv;
4932

5033
const { _, $0, script, repos, ...options } = argv;
5134

5235
console.log(
5336
`\n${chalk.bold("Running @octoherd/cli v%s")} ${chalk.gray(
5437
"(@octoherd/octokit v%s, Node.js: %s, %s %s)"
55-
)}\n`,
38+
)}`,
5639
VERSION,
5740
OctokitVersion,
5841
process.version,
5942
process.platform,
6043
process.arch
6144
);
6245

63-
octoherd({ ...options, octoherdScript: script, octoherdRepos: repos }).catch(
46+
let octoherdScript;
47+
const path = resolve(process.cwd(), script);
48+
49+
console.log("Loading script at %s\n", script);
50+
51+
try {
52+
octoherdScript = (await import(path)).script;
53+
} catch (error) {
54+
console.error(error.stack);
55+
throw new Error(`[octoherd] ${script} script could not be found`);
56+
}
57+
58+
if (!octoherdScript) {
59+
throw new Error(`[octoherd] no "script" exported at ${path}`);
60+
}
61+
62+
octoherd({ ...options, octoherdScript, octoherdRepos: repos }).catch(
6463
(error) => {
6564
console.error(error);
6665
process.exit(1);

bin/run.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import yargs from "yargs";
2+
import { VERSION as OctokitVersion } from "@octoherd/octokit";
3+
import chalk from "chalk";
4+
5+
import { octoherd } from "../index.js";
6+
import { VERSION } from "../version.js";
7+
import { cliOptions } from "./cli-options.js";
8+
9+
/**
10+
* Function is used by Octoherd Script modules to provide a dedicated CLI binary
11+
*
12+
* import { script } from "./script.js";
13+
* import { run } from "@octoherd/cli/run";
14+
* run(script);
15+
*
16+
* @param {function} script Octoherd Script function
17+
*/
18+
export function run(script) {
19+
const argv = yargs
20+
.usage("Usage: $0 [options] [repos...]")
21+
.example(
22+
"$0 --token 0123456789012345678901234567890123456789 octokit/rest.js"
23+
)
24+
.command("$0 [repos...]", "", (yargs) => {
25+
yargs.positional("repos", {
26+
demandOption: true,
27+
describe:
28+
"One or multiple arrays in the form of 'repo-owner/repo-name'. 'repo-owner/*' will find all repositories for one owner. '*' will find all repositories the user has access to",
29+
default: [],
30+
});
31+
})
32+
.options(cliOptions)
33+
.version(VERSION)
34+
.epilog(`copyright 2020-${new Date().getFullYear()}`).argv;
35+
36+
const { _, $0, repos, ...options } = argv;
37+
38+
console.log(
39+
`\n${chalk.bold("Running @octoherd/cli v%s")} ${chalk.gray(
40+
"(@octoherd/octokit v%s, Node.js: %s, %s %s)"
41+
)}\n`,
42+
VERSION,
43+
OctokitVersion,
44+
process.version,
45+
process.platform,
46+
process.arch
47+
);
48+
49+
octoherd({ ...options, octoherdScript: script, octoherdRepos: repos }).catch(
50+
(error) => {
51+
console.error(error);
52+
process.exit(1);
53+
}
54+
);
55+
}
56+
57+
import { script } from "../../script-star-or-unstar/script.js";

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { components } from "@octokit/openapi-types";
2+
3+
export { Octokit } from "@octoherd/octokit";
4+
export type Repository = components["schemas"]["repository"];

index.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { resolve } from "path";
21
import { appendFileSync } from "fs";
32

43
import { Octokit } from "@octoherd/octokit";
@@ -22,10 +21,10 @@ const levelColor = {
2221
* Find all releases in a GitHub repository or organization after a specified date
2322
*
2423
* @param {object} options
24+
* @param {function} options.octoherdScript Path to script to run against a repository
25+
* @param {string[]} options.octoherdRepos Cache responses for debugging
2526
* @param {string} options.octoherdToken Personal Access Token: Requires the "public_repo" scope for public repositories, "repo" scope for private repositories.
26-
* @param {string} options.octoherdScript Path to script to run against a repository
27-
* @param {string} options.octoherdCache Array of repository names in the form of "repo-owner/repo-name". To match all repositories for an owner, pass "repo-owner/*"
28-
* @param {boolean} options.octoherdRepos Cache responses for debugging
27+
* @param {boolean} options.octoherdCache Array of repository names in the form of "repo-owner/repo-name". To match all repositories for an owner, pass "repo-owner/*"
2928
*/
3029
export async function octoherd(
3130
options = {
@@ -78,22 +77,6 @@ export async function octoherd(
7877
},
7978
});
8079

81-
let userScript;
82-
const path = resolve(process.cwd(), octoherdScript);
83-
84-
octokit.log.info("Loading script at %s", octoherdScript);
85-
86-
try {
87-
userScript = (await import(path)).script;
88-
} catch (error) {
89-
octokit.log.error(error.stack);
90-
throw new Error(`[octoherd] ${octoherdScript} script could not be found`);
91-
}
92-
93-
if (!userScript) {
94-
throw new Error(`[octoherd] no "script" exported at ${path}`);
95-
}
96-
9780
if (octoherdRepos.length === 0) {
9881
throw new Error("[octoherd] No repositories provided");
9982
}
@@ -110,13 +93,12 @@ export async function octoherd(
11093
for (const repository of repositories) {
11194
octokit.log.info(
11295
{ octoherd: true },
113-
"Running %s on %s...",
114-
octoherdScript,
96+
"Running on %s ...",
11597
repository.full_name
11698
);
11799

118100
try {
119-
await userScript(octokit, repository, userOptions);
101+
await octoherdScript(octokit, repository, userOptions);
120102
} catch (error) {
121103
if (!error.cancel) throw error;
122104
octokit.log.debug(error.message);

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
"type": "module",
77
"version": "0.0.0-development",
88
"description": "CLI to run a custom script on one or multiple repositories",
9-
"exports": "./bin/octoherd.js",
9+
"exports": {
10+
".": "./index.js",
11+
"./run": "./bin/run.js"
12+
},
1013
"bin": {
1114
"octoherd": "bin/octoherd.js"
1215
},
16+
"types": "inddex.d.ts",
1317
"dependencies": {
1418
"@octoherd/octokit": "^2.3.0",
19+
"@octokit/openapi-types": "^5.2.2",
1520
"chalk": "^4.1.0",
1621
"jsonfile": "^6.0.1",
1722
"mkdirp": "^1.0.4",
@@ -28,11 +33,12 @@
2833
},
2934
"keywords": [
3035
"github",
31-
"cli",
32-
"maintenance"
36+
"repository",
37+
"maintenance",
38+
"cli"
3339
],
3440
"author": "Gregor Martynus (https://twitter.com/gr2m)",
35-
"license": "MIT",
41+
"license": "ISC",
3642
"repository": "https://github.com/octoherd/cli",
3743
"release": {
3844
"branches": [

0 commit comments

Comments
 (0)