Skip to content

Commit

Permalink
fix: make CLI work (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Mar 3, 2021
1 parent 6161957 commit 2f549f0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 56 deletions.
9 changes: 6 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { resolve } from "path";
process.argv.splice(2, 0, resolve("script.js"));
await import("@octoherd/cli");
#!/usr/bin/env node

import { script } from "./script.js";
import { run } from "@octoherd/cli/run";

run(script);
58 changes: 18 additions & 40 deletions package-lock.json

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

9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
],
"author": "Gregor Martynus (https://twitter.com/gr2m)",
"license": "ISC",
"devDependencies": {
"@octoherd/octokit": "^2.2.0",
"@octokit/openapi-types": "^2.0.0"
},
"devDependencies": {},
"dependencies": {
"@octoherd/cli": "^2.6.0"
"@octoherd/cli": "^2.7.1"
},
"exports": "./script.js",
"bin": {
"octoherd-script-star-or-unstar": "cli.js"
"octoherd-script-star-or-unstar": "./cli.js"
},
"release": {
"branches": [
Expand Down
9 changes: 2 additions & 7 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/**
* stars or unstars the passed repository based on the `unstar` option
*
* @param {import('@octoherd/octokit').Octokit} octokit
* @param {import('@octokit/openapi-types').components["schemas"]["repository"]} repository
* @param {import('@octoherd/cli').Octokit} octokit
* @param {import('@octoherd/cli').Repository} repository
* @param { {unstar?: boolean} } options Custom user options passed to the CLI
*/
export async function script(octokit, repository, options) {
const method = options.unstar ? "DELETE" : "PUT";

const id = repository.id;
const owner = repository.owner.login;
const repo = repository.name;

// https://docs.github.com/en/rest/reference/activity#check-if-a-repository-is-starred-by-the-authenticated-user
const isStarred = await octokit
.request("GET /user/starred/{owner}/{repo}", {
Expand All @@ -22,7 +20,6 @@ export async function script(octokit, repository, options) {
() => true,
() => false
);

if ((options.unstar && !isStarred) || (!options.unstar && isStarred)) {
octokit.log.debug(
{
Expand All @@ -36,15 +33,13 @@ export async function script(octokit, repository, options) {
);
return;
}

// https://docs.github.com/en/rest/reference/activity#star-a-repository-for-the-authenticated-user
// https://docs.github.com/en/rest/reference/activity#unstar-a-repository-for-the-authenticated-user
await octokit.request("/user/starred/{owner}/{repo}", {
method,
owner,
repo,
});

octokit.log.info(
{
change: options.unstar ? -1 : 1,
Expand Down

0 comments on commit 2f549f0

Please sign in to comment.