Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial support for Deno #135

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
.env
.vscode
27 changes: 11 additions & 16 deletions bin/commands/run.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from "path";
import { resolve } from "node:path";

import chalk from "chalk";
import { VERSION as OctokitVersion } from "@octoherd/octokit";
Expand All @@ -8,7 +8,7 @@ import { VERSION } from "../../version.js";
const VERSIONS = `
@octoherd/cli: v${VERSION}
@octoherd/octokit: v${OctokitVersion}
Node.js: ${process.version}, ${process.platform} ${process.arch}`.trim();
Deno: ${Deno.version.deno}`.trim();

/** @type { {[key: string]: import("yargs").Options} } */
const options = {
Expand Down Expand Up @@ -47,9 +47,10 @@ const options = {
default: false,
},
"octoherd-base-url": {
description: "When using with GitHub Enterprise Server, set to the root URL of the API. For example, if your GitHub Enterprise Server's hostname is github.acme-inc.com, then set to https://github.acme-inc.com/api/v3.",
description:
"When using with GitHub Enterprise Server, set to the root URL of the API. For example, if your GitHub Enterprise Server's hostname is github.acme-inc.com, then set to https://github.acme-inc.com/api/v3.",
type: "string",
}
},
};

/** @type import('yargs').CommandModule */
Expand Down Expand Up @@ -87,20 +88,16 @@ const runCommand = {
if (typeof script === "function") return script;

let scriptModule;
const path = resolve(process.cwd(), script);
const path = resolve(Deno.cwd(), script);

try {
scriptModule = await import(path);
} catch (error) {
if (error.code === 'ERR_MODULE_NOT_FOUND') {
throw new Error(
`[octoherd] ${path} does not exist`
);
if (error.code === "ERR_MODULE_NOT_FOUND") {
throw new Error(`[octoherd] ${path} does not exist`);
}

const err = new Error(
`[octoherd] ${error}\n at ${path}`
)
const err = new Error(`[octoherd] ${error}\n at ${path}`);
throw err;
}

Expand All @@ -113,13 +110,11 @@ const runCommand = {
handler: () => {
console.log(
`\n${chalk.bold("Running @octoherd/cli v%s")} ${chalk.gray(
"(@octoherd/octokit v%s, Node.js: %s, %s %s)"
"(@octoherd/octokit v%s, Deno: %s)"
)}\n`,
VERSION,
OctokitVersion,
process.version,
process.platform,
process.arch
Deno.version.deno
);
},
};
Expand Down
18 changes: 7 additions & 11 deletions bin/octoherd.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#!/usr/bin/env node

import yargs from "yargs";
import { hideBin } from "yargs/helpers";

import yargs from "https://deno.land/x/[email protected]/deno.ts";
import chalk from "chalk";

import { octoherd } from "../index.js";
Expand All @@ -11,18 +7,18 @@ import runCommand from "./commands/run.js";

const EPILOG = chalk.gray(`Questions? Ideas? Feedback?
https://github.com/octoherd/octoherd/discussions

Copyright 2020-${new Date().getFullYear()} Octoherd Contributors`);

const argv = await yargs(hideBin(process.argv))
const { argv } = yargs(Deno.args)
.command(runCommand)
.demandCommand()
.version(VERSION)
.epilog(EPILOG).argv;
.epilog(EPILOG);

try {
await octoherd(argv);
octoherd(argv);
} catch (error) {
console.error(error);
process.exit(1);
console.error({ error });
Deno.exit(1);
}
6 changes: 3 additions & 3 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import runCommand from "./commands/run.js";
* @param {function} script Octoherd Script function
*/
export async function run(script) {
const argv = await yargs(["run", ...hideBin(process.argv)])
const argv = await yargs(["run", ...hideBin(Deno.args)])
.command(runCommand)
.default("octoherd-script", () => script).argv;

try {
await octoherd(argv);
octoherd(argv);
} catch (error) {
console.error(error);
process.exit(1);
Deno.exit(1);
}
}
4 changes: 4 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { script } from "./scripts/script.ts";
import { run } from "./bin/run.js";

run(script);
5 changes: 5 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tasks": {
"dev": "deno run -A cli.js -T $GITHUB_TOKEN -R 'kurtaking/octoherd-cli'"
}
}
659 changes: 659 additions & 0 deletions deno.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { octoherd } from "./index.js";
import { components } from "@octokit/openapi-types";

export { Octokit } from "@octoherd/octokit";
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { appendFileSync } from "fs";
import { appendFileSync } from "node:fs";

import { Octokit } from "@octoherd/octokit";
import { createOAuthDeviceAuth } from "@octokit/auth-oauth-device";
Expand Down
4 changes: 2 additions & 2 deletions lib/octokit-plugin-cache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { URL } from "url";
import { dirname, join } from "path";
import { URL } from "node:url";
import { dirname, join } from "node:path";

import { mkdirp } from "mkdirp";
import jsonfile from "jsonfile";
Expand Down
1 change: 0 additions & 1 deletion lib/octokit-plugin-request-confirm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// import Confirm from "prompt-confirm";
import enquirer from "enquirer";
import chalk from "chalk";

Expand Down
2 changes: 1 addition & 1 deletion lib/octokit-plugin-request-log.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function requestLog(octokit) {
octokit.hook.wrap("request", async (request, options) => {
octokit.hook.wrap("request", (request, options) => {
const start = Date.now();
const requestOptions = octokit.request.endpoint.parse(options);
const path = requestOptions.url.replace(options.baseUrl, "");
Expand Down
31 changes: 18 additions & 13 deletions lib/resolve-repositories.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
export async function resolveRepositories(state, repositories) {
const ignoreRepositories = repositories.reduce((memo, fullName) => {
if (fullName.startsWith('!')) {
memo.push(fullName.slice(1))
if (fullName.startsWith("!")) {
memo.push(fullName.slice(1));
}
return memo;
}, []);

const repositoriesWithStars = repositories.filter((fullName) => {
return /^[a-z0-9_.-]+\/(\*|[a-z0-9_.-]+\*|\*[a-z0-9_.-]+|[a-z0-9_.-]+\*[a-z0-9_.-]+)$/i.test(
fullName
) && !fullName.startsWith('!');
return (
/^[a-z0-9_.-]+\/(\*|[a-z0-9_.-]+\*|\*[a-z0-9_.-]+|[a-z0-9_.-]+\*[a-z0-9_.-]+)$/i.test(
fullName
) && !fullName.startsWith("!")
);
});

const repositoriesWithoutStars = repositories.filter((fullName) => {
return !/\*/i.test(fullName) && !fullName.startsWith('!');
return !/\*/i.test(fullName) && !fullName.startsWith("!");
});
const allRepositories = !!repositories.find((name) => name === "*");

Expand All @@ -32,7 +34,7 @@ export async function resolveRepositories(state, repositories) {

state.octokit.log.warn(`Repository ${owner}/${repo} could not be found`);
}
process.stdout.write(".");
Deno.stdout.write(new TextEncoder().encode("."));
}

for (const name of repositoriesWithStars) {
Expand Down Expand Up @@ -68,7 +70,7 @@ export async function resolveRepositories(state, repositories) {
}

resolvedRepositories.push(...selectedRepositories);
process.stdout.write(".");
Deno.stdout.write(new TextEncoder().encode("."));
}
}

Expand All @@ -80,18 +82,21 @@ export async function resolveRepositories(state, repositories) {
}
)) {
resolvedRepositories.push(...response.data);
process.stdout.write(".");
Deno.stdout.write(new TextEncoder().encode("."));
}
}

process.stdout.write("\n");
Deno.stdout.write(new TextEncoder().encode("\n"));

function includesRepository(list, repo) {
return list.some((x) => x.toLowerCase() === repo.full_name.toLowerCase())
return list.some((x) => x.toLowerCase() === repo.full_name.toLowerCase());
}

// return array with unique repositories based by id (https://stackoverflow.com/a/56757215)
return resolvedRepositories
.filter((repo, index, repoList) => repoList.findIndex(v2 => (v2.id === repo.id)) === index)
.filter(repo => !includesRepository(ignoreRepositories, repo))
.filter(
(repo, index, repoList) =>
repoList.findIndex((v2) => v2.id === repo.id) === index
)
.filter((repo) => !includesRepository(ignoreRepositories, repo));
}
2 changes: 1 addition & 1 deletion lib/run-script-against-repositories.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function runScriptAgainstRepositories(state, octoherdRepos = []) {
}
} catch (error) {
state.octokit.log.error(error);
process.exitCode = 1;
Deno.exitCode = 1;
}

await runScriptAgainstRepositories(state);
Expand Down
3 changes: 1 addition & 2 deletions scripts/generate-endpoints.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { writeFile } from "fs/promises";

import { writeFile } from "node:fs/promises";
import prettier from "prettier";
import OpenAPI from "@octokit/openapi";

Expand Down
32 changes: 32 additions & 0 deletions scripts/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
source code from:
https://github.com/octoherd/script-rename-master-branch-to-main/blob/main/script.js
*/

export async function script(octokit, repository) {
if (repository.default_branch !== "master") {
octokit.log.info(
`Default branch is not "master" but "${repository.default_branch}", ignoring`
);
return;
}

if (!repository.permissions.admin) {
octokit.log.warn(`You don't have admin permission, ignoring`);
return;
}

if (repository.archived) {
octokit.log.info(`Repository is archived, ignoring`);
return;
}

await octokit.request("POST /repos/{owner}/{repo}/branches/{branch}/rename", {
owner: repository.owner.login,
repo: repository.name,
branch: "master",
new_name: "main",
});

octokit.log.info(`Default branch renamed to "main"`);
}
32 changes: 32 additions & 0 deletions scripts/script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
source code from:
https://github.com/octoherd/script-rename-master-branch-to-main/blob/main/script.js
*/

export async function script(octokit, repository) {
if (repository.default_branch !== "master") {
octokit.log.info(
`Default branch is not "master" but "${repository.default_branch}", ignoring`
);
return;
}

if (!repository.permissions.admin) {
octokit.log.warn(`You don't have admin permission, ignoring`);
return;
}

if (repository.archived) {
octokit.log.info(`Repository is archived, ignoring`);
return;
}

await octokit.request("POST /repos/{owner}/{repo}/branches/{branch}/rename", {
owner: repository.owner.login,
repo: repository.name,
branch: "master",
new_name: "main",
});

octokit.log.info(`Default branch renamed to "main"`);
}
Loading