Skip to content

Commit

Permalink
feat: use script export from loaded script (#4)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `mutate-github-repositories` is now `octoherd`

BREAKING CHANGE: scripts now have to expert function as `script`. Default export is no longer used
  • Loading branch information
gr2m authored Dec 16, 2020
1 parent 21a46a4 commit 8df150b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 41 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# mutate-github-repositories-cli
# Octoherd CLI

> CLI to run a custom script on one or multiple repositories
## Usage

```
$ mutate-github-repositories [script] [repos...]
$ octoherd [script] [repos...]
Positionals:
script Path to your *.js script
Expand All @@ -20,24 +20,22 @@ Options:
--cache Cache responses for debugging [boolean] [default: false]
```

The `script` must export a default function which takes three parameters:
The `script` must export a `script` function which takes three parameters:

```js
module.exports = exampleScript;

module.exports = async function myScript(octokit, repository, options) {
module.exports.script = async function myScript(octokit, repository, options) {
// do something here
};
```

- `octokit` is an instance of [`@octokit/core`](https://github.com/octokit/core.js) with the [`@octokit/plugin-paginate-rest` plugin](https://github.com/octokit/plugin-paginate-rest.js)
- `repository` is the response data of [`GET /repos/{owner}/{repo}`](https://developer.github.com/v3/repos/#get-a-repository)
- `options` are all options passed to the CLI which are not used by `mutate-github-repositories`.
- `options` are all options passed to the CLI which are not used by `octoherd`.

## Example

```
$ npx mutate-github-repositories-cli \
$ npx @octoherd/cli \
--token 0123456789012345678901234567890123456789 \
example.js \
octokit/*
Expand All @@ -53,7 +51,7 @@ See [example.js](example.js) for the syntax of a script.

## Similar projects

- [NerdWalletOSS/shepherd](https://github.com/NerdWalletOSS/shepherd) - A utility for applying code changes across many repositories.
- [NerdWalletOSS/shepherd](https://github.com/NerdWalletOSS/shepherd) - A utility for applying code changes across many repositories.
- [FormidableLabs/multibot](https://github.com/FormidableLabs/multibot) - A friendly multi-repository robot

## License
Expand Down
4 changes: 2 additions & 2 deletions bin/mutate-github-repositories.js → bin/octoherd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const yargs = require("yargs");

const mutateGithubRepositories = require("..");
const octoherd = require("..");

var argv = yargs
.usage("Usage: $0 [options] [script] [repos...]")
Expand Down Expand Up @@ -34,4 +34,4 @@ var argv = yargs
.epilog("copyright 2020").argv;

const { _, $0, ...options } = argv;
mutateGithubRepositories(options).catch(console.error);
octoherd(options).catch(console.error);
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = exampleScript;
module.exports.script = exampleScript;

/**
* Example script that stars or unstars the passed repository based on the `--unstar` CLI option
Expand Down
17 changes: 10 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = mutateGithubRepositories;
module.exports = octoherd;

const { resolve } = require("path");

Expand Down Expand Up @@ -33,7 +33,7 @@ const Octokit = OctokitCore.plugin(paginateRest, throttling, retry).defaults({
* @param {string} options.repos 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.cache Cache responses for debugging
*/
async function mutateGithubRepositories(
async function octoherd(
options = {
cache: false,
repos: [],
Expand All @@ -46,16 +46,19 @@ async function mutateGithubRepositories(
});

let userScript;
const path = resolve(process.cwd(), script);
try {
userScript = require(resolve(process.cwd(), script));
userScript = require(path).script;
} catch (error) {
throw new Error(
`[mutate-github-repositories] ${script} script could not be found`
);
throw new Error(`[octoherd] ${script} script could not be found`);
}

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

if (repos.length === 0) {
throw new Error("[mutate-github-repositories] No repositories provided");
throw new Error("[octoherd] No repositories provided");
}

state = {
Expand Down
5 changes: 1 addition & 4 deletions lib/resolve-repositories.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ async function resolveRepositories(state, repositories) {
});

if (invalidRepositories.length) {
throw new Error(
"[mutate-github-repositories] Invalid repositories: %o",
invalidRepositories
);
throw new Error("[octoherd] Invalid repositories: %o", invalidRepositories);
}

const repositoriesWithStars = repositories.filter((fullName) => {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

26 changes: 9 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "mutate-github-repositories-cli",
"version": "1.0.0",
"description": "CLI to find github releases in a repository or organization after a specified date",
"name": "@octoherd/cli",
"publishConfig": {
"access": "public"
},
"version": "0.0.0-development",
"description": "CLI to run a custom script on one or multiple repositories",
"main": "index.js",
"bin": {
"mutate-github-repositories": "bin/mutate-github-repositories.js"
"octoherd": "bin/octoherd.js"
},
"dependencies": {
"@octokit/core": "^3.1.2",
Expand All @@ -28,21 +31,10 @@
],
"author": "Gregor Martynus (https://twitter.com/gr2m)",
"license": "MIT",
"repository": "https://github.com/gr2m/mutate-github-repositories-cli",
"repository": "https://github.com/octoherd/cli",
"release": {
"branches": [
"+([0-9])?(.{+([0-9]),x}).x",
"main",
"next",
"next-major",
{
"name": "beta",
"prerelease": true
},
{
"name": "alpha",
"prerelease": true
}
"main"
]
}
}

0 comments on commit 8df150b

Please sign in to comment.