Skip to content

Commit

Permalink
feat: export @octoherd/cli/run (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m authored Mar 3, 2021
1 parent f45f0e2 commit bd8a34b
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 54 deletions.
23 changes: 23 additions & 0 deletions bin/cli-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const cliOptions = {
"octoherd-token": {
description:
'Requires the "public_repo" scope for public repositories, "repo" scope for private repositories.',
demandOption: true,
type: "string",
},
"octoherd-cache": {
description:
"Cache responses for debugging. Creates a ./cache folder if flag is set. Override by passing custom path",
type: "string",
},
"octoherd-debug": {
description: "Show debug logs",
type: "boolean",
default: false,
},
"octoherd-bypass-confirms": {
description: "Bypass prompts to confirm mutating requests",
type: "boolean",
default: false,
},
};
47 changes: 23 additions & 24 deletions bin/octoherd.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env node

import yargs from "yargs";
import { resolve } from "path";
import { VERSION as OctokitVersion } from "@octoherd/octokit";
import chalk from "chalk";

import { octoherd } from "../index.js";
import { VERSION } from "../version.js";
import { cliOptions } from "./cli-options.js";

const argv = yargs
.usage("Usage: $0 [options] [script] [repos...]")
Expand All @@ -24,43 +26,40 @@ const argv = yargs
default: [],
});
})
.option("octoherd-token", {
description:
'Requires the "public_repo" scope for public repositories, "repo" scope for private repositories.',
demandOption: true,
type: "string",
})
.option("octoherd-cache", {
description:
"Cache responses for debugging. Creates a ./cache folder if flag is set. Override by passing custom path",
type: "string",
})
.option("octoherd-debug", {
description: "Show debug logs",
type: "boolean",
default: false,
})
.option("octoherd-bypass-confirms", {
description: "Bypass prompts to confirm mutating requests",
type: "boolean",
default: false,
})
.epilog("copyright 2020").argv;
.options(cliOptions)
.version(VERSION)
.epilog(`copyright 2020-${new Date().getFullYear()}`).argv;

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

console.log(
`\n${chalk.bold("Running @octoherd/cli v%s")} ${chalk.gray(
"(@octoherd/octokit v%s, Node.js: %s, %s %s)"
)}\n`,
)}`,
VERSION,
OctokitVersion,
process.version,
process.platform,
process.arch
);

octoherd({ ...options, octoherdScript: script, octoherdRepos: repos }).catch(
let octoherdScript;
const path = resolve(process.cwd(), script);

console.log("Loading script at %s\n", script);

try {
octoherdScript = (await import(path)).script;
} catch (error) {
console.error(error.stack);
throw new Error(`[octoherd] ${script} script could not be found`);
}

if (!octoherdScript) {
throw new Error(`[octoherd] no "script" exported at ${path}`);
}

octoherd({ ...options, octoherdScript, octoherdRepos: repos }).catch(
(error) => {
console.error(error);
process.exit(1);
Expand Down
57 changes: 57 additions & 0 deletions bin/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import yargs from "yargs";
import { VERSION as OctokitVersion } from "@octoherd/octokit";
import chalk from "chalk";

import { octoherd } from "../index.js";
import { VERSION } from "../version.js";
import { cliOptions } from "./cli-options.js";

/**
* Function is used by Octoherd Script modules to provide a dedicated CLI binary
*
* import { script } from "./script.js";
* import { run } from "@octoherd/cli/run";
* run(script);
*
* @param {function} script Octoherd Script function
*/
export function run(script) {
const argv = yargs
.usage("Usage: $0 [options] [repos...]")
.example(
"$0 --token 0123456789012345678901234567890123456789 octokit/rest.js"
)
.command("$0 [repos...]", "", (yargs) => {
yargs.positional("repos", {
demandOption: true,
describe:
"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",
default: [],
});
})
.options(cliOptions)
.version(VERSION)
.epilog(`copyright 2020-${new Date().getFullYear()}`).argv;

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

console.log(
`\n${chalk.bold("Running @octoherd/cli v%s")} ${chalk.gray(
"(@octoherd/octokit v%s, Node.js: %s, %s %s)"
)}\n`,
VERSION,
OctokitVersion,
process.version,
process.platform,
process.arch
);

octoherd({ ...options, octoherdScript: script, octoherdRepos: repos }).catch(
(error) => {
console.error(error);
process.exit(1);
}
);
}

import { script } from "../../script-star-or-unstar/script.js";
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { components } from "@octokit/openapi-types";

export { Octokit } from "@octoherd/octokit";
export type Repository = components["schemas"]["repository"];
28 changes: 5 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { resolve } from "path";
import { appendFileSync } from "fs";

import { Octokit } from "@octoherd/octokit";
Expand All @@ -22,10 +21,10 @@ const levelColor = {
* Find all releases in a GitHub repository or organization after a specified date
*
* @param {object} options
* @param {function} options.octoherdScript Path to script to run against a repository
* @param {string[]} options.octoherdRepos Cache responses for debugging
* @param {string} options.octoherdToken Personal Access Token: Requires the "public_repo" scope for public repositories, "repo" scope for private repositories.
* @param {string} options.octoherdScript Path to script to run against a repository
* @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/*"
* @param {boolean} options.octoherdRepos Cache responses for debugging
* @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/*"
*/
export async function octoherd(
options = {
Expand Down Expand Up @@ -78,22 +77,6 @@ export async function octoherd(
},
});

let userScript;
const path = resolve(process.cwd(), octoherdScript);

octokit.log.info("Loading script at %s", octoherdScript);

try {
userScript = (await import(path)).script;
} catch (error) {
octokit.log.error(error.stack);
throw new Error(`[octoherd] ${octoherdScript} script could not be found`);
}

if (!userScript) {
throw new Error(`[octoherd] no "script" exported at ${path}`);
}

if (octoherdRepos.length === 0) {
throw new Error("[octoherd] No repositories provided");
}
Expand All @@ -110,13 +93,12 @@ export async function octoherd(
for (const repository of repositories) {
octokit.log.info(
{ octoherd: true },
"Running %s on %s...",
octoherdScript,
"Running on %s ...",
repository.full_name
);

try {
await userScript(octokit, repository, userOptions);
await octoherdScript(octokit, repository, userOptions);
} catch (error) {
if (!error.cancel) throw error;
octokit.log.debug(error.message);
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
"type": "module",
"version": "0.0.0-development",
"description": "CLI to run a custom script on one or multiple repositories",
"exports": "./bin/octoherd.js",
"exports": {
".": "./index.js",
"./run": "./bin/run.js"
},
"bin": {
"octoherd": "bin/octoherd.js"
},
"types": "inddex.d.ts",
"dependencies": {
"@octoherd/octokit": "^2.3.0",
"@octokit/openapi-types": "^5.2.2",
"chalk": "^4.1.0",
"jsonfile": "^6.0.1",
"mkdirp": "^1.0.4",
Expand All @@ -28,11 +33,12 @@
},
"keywords": [
"github",
"cli",
"maintenance"
"repository",
"maintenance",
"cli"
],
"author": "Gregor Martynus (https://twitter.com/gr2m)",
"license": "MIT",
"license": "ISC",
"repository": "https://github.com/octoherd/cli",
"release": {
"branches": [
Expand Down

0 comments on commit bd8a34b

Please sign in to comment.