Skip to content

Commit

Permalink
feat(Octocat): add EventsManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ijsKoud committed Jun 21, 2023
1 parent 5222259 commit eb1b34f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
76 changes: 76 additions & 0 deletions packages/octocat/src/lib/managers/EventManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type { Octokit } from "@ijsblokje/octokit";
import type { Octocat } from "../Octocat.js";
import { fileURLToPath } from "node:url";
import { join } from "node:path";
import { glob } from "glob";
import { Collection } from "@discordjs/collection";
import { GitHubEvent } from "../structures/GithubEvent.js";

const __dirname = fileURLToPath(new URL(".", import.meta.url));

export class EventManager {
/** The octocat instance */
public readonly octocat: Octocat;

/** The octokit instance */
public readonly octokit: Octokit;

/** A collection of all the loaded events */
public events = new Collection<string, GitHubEvent<any>>();

/** The base directory where all event handlers are located */
private readonly directory: string;

/**
* @param octocat The octokit instance
* @param directory The base directory where all the event handlers are located
*/
public constructor(octocat: Octocat, directory: string) {
this.octocat = octocat;
this.octokit = octocat.octokit;
this.directory = directory;
}

/** Loads all the event handlers */
public async loadAll() {
const coreDirectory = join(__dirname, "..", "..", "core");
const coreFiles = await this.getFiles(coreDirectory);
const externalFiles = await this.getFiles(this.directory);
const files = [...externalFiles, ...coreFiles];

await Promise.all(files.map(this.load.bind(this)));
}

/**
* Loads an event handler file
* @param path The path to the event handler
*/
public async load(path: string) {
const { default: construct } = await import(path);
if (typeof construct === "function" && typeof construct.prototype === "object") {
const event = new construct();
if (!(event instanceof GitHubEvent)) return;

this.events.set(event.event, event);
this.octocat.server.on(event.event, (data) => this.handleEvent(data, event));
event.load(this.octocat);
}
}

private handleEvent(event: any, handler: GitHubEvent<any>) {
const installationId = event.payload.installation.id;
const installation = this.octocat.installations.cache.get(installationId);
if (!installation) return;

void handler.run(event, installation);
}

/**
* Returns a list of JavaScript file paths from a provided directory
* @param directory The directory to read
*/
private async getFiles(directory: string) {
const files = await glob("**/*.js", { cwd: directory, nodir: true, withFileTypes: true });
return files.map((path) => path.fullpath());
}
}
4 changes: 2 additions & 2 deletions packages/octocat/src/lib/managers/InstallationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class InstallationManager {
public readonly octocat: Octocat;

/** Collection containing cached github installations */
public readonly cache = new Collection<string, GitHubInstallation>();
public readonly cache = new Collection<number, GitHubInstallation>();

/** An array of accounts which are allowed to be loaded */
public readonly allowedInstallations?: string[];
Expand Down Expand Up @@ -65,7 +65,7 @@ export class InstallationManager {
if (!repositories) return;

const instance = new GitHubInstallation(installation, { manager: this, readme, labels });
this.cache.set(instance.name, instance);
this.cache.set(instance.installationId, instance);

await request("DELETE /installation/token", { headers: { authorization: `Bearer ${token.token}` } }).catch(() => void 0);
}
Expand Down

0 comments on commit eb1b34f

Please sign in to comment.