Skip to content

Commit

Permalink
chore: Prepare code for ESM (#1921)
Browse files Browse the repository at this point in the history
This prepares the code to compile properly in ESM to keep the code changes for full ESM later minimal.
This does not convert Probot to ESM.

Co-authored-by: wolfy1339 <4595477+wolfy1339@users.noreply.github.com>
  • Loading branch information
AaronDewes and wolfy1339 committed Nov 20, 2023
1 parent 5116b5e commit f5ee0de
Show file tree
Hide file tree
Showing 49 changed files with 151 additions and 145 deletions.
6 changes: 3 additions & 3 deletions src/apps/default.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ApplicationFunctionOptions, Probot } from "../index";
import { loadPackageJson } from "../helpers/load-package-json";
import type { ApplicationFunctionOptions, Probot } from "../index.js";
import { loadPackageJson } from "../helpers/load-package-json.js";
import { resolve } from "path";

import { probotView } from "../views/probot";
import { probotView } from "../views/probot.js";

export function defaultApp(
_app: Probot,
Expand Down
18 changes: 9 additions & 9 deletions src/apps/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { exec } from "child_process";
import type { Request, Response } from "express";
import updateDotenv from "update-dotenv";

import { Probot } from "../probot";
import { ManifestCreation } from "../manifest-creation";
import { getLoggingMiddleware } from "../server/logging-middleware";
import type { ApplicationFunctionOptions } from "../types";
import { isProduction } from "../helpers/is-production";

import { importView } from "../views/import";
import { setupView } from "../views/setup";
import { successView } from "../views/success";
import { Probot } from "../probot.js";
import { ManifestCreation } from "../manifest-creation.js";
import { getLoggingMiddleware } from "../server/logging-middleware.js";
import type { ApplicationFunctionOptions } from "../types.js";
import { isProduction } from "../helpers/is-production.js";

import { importView } from "../views/import.js";
import { setupView } from "../views/setup.js";
import { successView } from "../views/success.js";

export const setupAppFactory = (
host: string | undefined,
Expand Down
6 changes: 3 additions & 3 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getAuthenticatedOctokit } from "./octokit/get-authenticated-octokit";
import { ProbotOctokit } from "./octokit/probot-octokit";
import type { State } from "./types";
import { getAuthenticatedOctokit } from "./octokit/get-authenticated-octokit.js";
import { ProbotOctokit } from "./octokit/probot-octokit.js";
import type { State } from "./types.js";

/**
* Authenticate and get a GitHub client that can be used to make API calls.
Expand Down
6 changes: 3 additions & 3 deletions src/bin/probot-receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import path from "path";
import { randomUUID as uuidv4 } from "crypto";
import { program } from "commander";
import { getPrivateKey } from "@probot/get-private-key";
import { getLog } from "../helpers/get-log";
import { getLog } from "../helpers/get-log.js";

import { Probot, type ApplicationFunctionOptions } from "../";
import { resolveAppFunction } from "../helpers/resolve-app-function";
import { Probot, type ApplicationFunctionOptions } from "../index.js";
import { resolveAppFunction } from "../helpers/resolve-app-function.js";

async function main() {
program
Expand Down
2 changes: 1 addition & 1 deletion src/bin/probot-run.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { run } from "../";
import { run } from "../index.js";

run(process.argv);
15 changes: 9 additions & 6 deletions src/bin/probot.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { program } from "commander";
import { isSupportedNodeVersion } from "../helpers/is-supported-node-version";
import { isSupportedNodeVersion } from "../helpers/is-supported-node-version.js";
import { loadPackageJson } from "../helpers/load-package-json.js";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
/*import { dirname } from 'path';
import { fileURLToPath } from 'url';*/

dotenvConfig();

const pkg = require("../../package");
//const __dirname = dirname(fileURLToPath(import.meta.url));
const pkg = loadPackageJson(resolve(__dirname, "package.json"));

if (!isSupportedNodeVersion()) {
console.log(
`Node.js version ${pkg.engines.node} is required. You have ${process.version}.`,
);
console.log(`Node.js version 18 is required. You have ${process.version}.`);
process.exit(1);
}

program
.version(pkg.version)
.version(pkg.version || "0.0.0-dev")
.usage("<command> [options]")
.command("run", "run the bot")
.command("receive", "Receive a single event and payload")
Expand Down
2 changes: 1 addition & 1 deletion src/bin/read-cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { program } from "commander";
import { getPrivateKey } from "@probot/get-private-key";
import type { Options as PinoOptions } from "@probot/pino";

import type { Options } from "../types";
import type { Options } from "../types.js";

export function readCliOptions(
argv: string[],
Expand Down
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import merge from "deepmerge";

import type { EmitterWebhookEvent as WebhookEvent } from "@octokit/webhooks";
import type { Logger } from "pino";
import type { ProbotOctokit } from "./octokit/probot-octokit";
import type { EmitterWebhookEventName as WebhookEvents } from "@octokit/webhooks/dist-types/types";
import type { ProbotOctokit } from "./octokit/probot-octokit.js";
import type { EmitterWebhookEventName as WebhookEvents } from "@octokit/webhooks/dist-types/types.js";

export type MergeOptions = merge.Options;

Expand Down
6 changes: 3 additions & 3 deletions src/create-node-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { RequestListener } from "http";
import { createNodeMiddleware as createWebhooksMiddleware } from "@octokit/webhooks";

import type { ApplicationFunction, MiddlewareOptions } from "./types";
import { defaultWebhooksPath } from "./server/server";
import { createProbot } from ".";
import type { ApplicationFunction, MiddlewareOptions } from "./types.js";
import { defaultWebhooksPath } from "./server/server.js";
import { createProbot } from "./create-probot.js";

export function createNodeMiddleware(
appFn: ApplicationFunction,
Expand Down
8 changes: 4 additions & 4 deletions src/create-probot.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { LogLevel, Options as PinoOptions } from "@probot/pino";
import { getPrivateKey } from "@probot/get-private-key";

import { getLog } from "./helpers/get-log";
import type { Options } from "./types";
import { Probot } from "./probot";
import { defaultWebhooksPath } from "./server/server";
import { getLog } from "./helpers/get-log.js";
import type { Options } from "./types.js";
import { Probot } from "./probot.js";
import { defaultWebhooksPath } from "./server/server.js";

type CreateProbotOptions = {
overrides?: Options;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/get-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* app.log.fatal("Goodbye, cruel world!");
* ```
*/
import pino from "pino";
import { pino } from "pino";
import type { Logger, LoggerOptions } from "pino";
import { getTransformStream, type Options, type LogLevel } from "@probot/pino";

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/load-package-json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import path from "path";
import type { PackageJson } from "../types";
import type { PackageJson } from "../types.js";

export function loadPackageJson(
filepath = path.join(process.cwd(), "package.json"),
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/resolve-app-function.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sync } from "resolve";
import resolveModule from "resolve";

const defaultOptions: ResolveOptions = {};

Expand All @@ -9,7 +9,7 @@ export const resolveAppFunction = async (
opts = opts || defaultOptions;
// These are mostly to ease testing
const basedir = opts.basedir || process.cwd();
const resolver: Resolver = opts.resolver || sync;
const resolver: Resolver = opts.resolver || resolveModule.sync;
const appFnPath = resolver(appFnId, { basedir });
// On windows, an absolute path may start with a drive letter, e.g. C:/path/to/file.js
// This can be interpreted as a protocol, so ensure it's prefixed with file://
Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
export type { Logger } from "pino";

export { Context } from "./context";
export { Context } from "./context.js";

export { Probot } from "./probot";
export { Server } from "./server/server";
export { ProbotOctokit } from "./octokit/probot-octokit";
export { run } from "./run";
export { createNodeMiddleware } from "./create-node-middleware";
export { createProbot } from "./create-probot";
export { Probot } from "./probot.js";
export { Server } from "./server/server.js";
export { ProbotOctokit } from "./octokit/probot-octokit.js";
export { run } from "./run.js";
export { createNodeMiddleware } from "./create-node-middleware.js";
export { createProbot } from "./create-probot.js";

/** NOTE: exported types might change at any point in time */
export type {
Options,
ApplicationFunction,
ApplicationFunctionOptions,
} from "./types";
} from "./types.js";

declare global {
namespace NodeJS {
Expand Down
6 changes: 3 additions & 3 deletions src/manifest-creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import fs from "fs";
import yaml from "js-yaml";
import path from "path";
import updateDotenv from "update-dotenv";
import { ProbotOctokit } from "./octokit/probot-octokit";
import { loadPackageJson } from "./helpers/load-package-json";
import type { Env, Manifest, OctokitOptions, PackageJson } from "./types";
import { ProbotOctokit } from "./octokit/probot-octokit.js";
import { loadPackageJson } from "./helpers/load-package-json.js";
import type { Env, Manifest, OctokitOptions, PackageJson } from "./types.js";
import type { RequestParameters } from "@octokit/types";

export class ManifestCreation {
Expand Down
6 changes: 3 additions & 3 deletions src/octokit/get-authenticated-octokit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { State } from "../types";
import type { ProbotOctokit } from "./probot-octokit";
import type { OctokitOptions } from "../types";
import type { State } from "../types.js";
import type { ProbotOctokit } from "./probot-octokit.js";
import type { OctokitOptions } from "../types.js";
import type { LogFn, Level } from "pino";

type FactoryOptions = {
Expand Down
6 changes: 3 additions & 3 deletions src/octokit/get-probot-octokit-with-defaults.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { LRUCache } from "lru-cache";
import { ProbotOctokit } from "./probot-octokit";
import { ProbotOctokit } from "./probot-octokit.js";
import type { RedisOptions } from "ioredis";

import { getOctokitThrottleOptions } from "./get-octokit-throttle-options";
import { getOctokitThrottleOptions } from "./get-octokit-throttle-options.js";

import type { Logger } from "pino";
import type { RequestRequestOptions } from "@octokit/types";
import { OctokitOptions } from "../types";
import type { OctokitOptions } from "../types.js";

type Options = {
cache: LRUCache<number, string>;
Expand Down
6 changes: 3 additions & 3 deletions src/octokit/get-webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Webhooks } from "@octokit/webhooks";

import type { State } from "../types";
import { getErrorHandler } from "../helpers/get-error-handler";
import { webhookTransform } from "./octokit-webhooks-transform";
import type { State } from "../types.js";
import { getErrorHandler } from "../helpers/get-error-handler.js";
import { webhookTransform } from "./octokit-webhooks-transform.js";

export function getWebhooks(state: State) {
const webhooks = new Webhooks({
Expand Down
4 changes: 2 additions & 2 deletions src/octokit/octokit-webhooks-transform.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { EmitterWebhookEvent as WebhookEvent } from "@octokit/webhooks";

import { Context } from "../context";
import type { State } from "../types";
import { Context } from "../context.js";
import type { State } from "../types.js";

/**
* Probot's transform option, which extends the `event` object that is passed
Expand Down
4 changes: 2 additions & 2 deletions src/octokit/probot-octokit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { throttling } from "@octokit/plugin-throttling";
import { config } from "@probot/octokit-plugin-config";
import { createProbotAuth } from "octokit-auth-probot";

import { probotRequestLogging } from "./octokit-plugin-probot-request-logging";
import { VERSION } from "../version";
import { probotRequestLogging } from "./octokit-plugin-probot-request-logging.js";
import { VERSION } from "../version.js";

const defaultOptions = {
authStrategy: createProbotAuth,
Expand Down
16 changes: 8 additions & 8 deletions src/probot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { LRUCache } from "lru-cache";
import type { Logger } from "pino";
import type { EmitterWebhookEvent as WebhookEvent } from "@octokit/webhooks";

import { auth } from "./auth";
import { getLog } from "./helpers/get-log";
import { getProbotOctokitWithDefaults } from "./octokit/get-probot-octokit-with-defaults";
import { getWebhooks } from "./octokit/get-webhooks";
import { ProbotOctokit } from "./octokit/probot-octokit";
import { VERSION } from "./version";
import { auth } from "./auth.js";
import { getLog } from "./helpers/get-log.js";
import { getProbotOctokitWithDefaults } from "./octokit/get-probot-octokit-with-defaults.js";
import { getWebhooks } from "./octokit/get-webhooks.js";
import { ProbotOctokit } from "./octokit/probot-octokit.js";
import { VERSION } from "./version.js";
import type {
ApplicationFunction,
ApplicationFunctionOptions,
Options,
ProbotWebhooks,
State,
} from "./types";
import { defaultWebhooksPath } from "./server/server";
} from "./types.js";
import { defaultWebhooksPath } from "./server/server.js";

export type Constructor<T = any> = new (...args: any[]) => T;

Expand Down
20 changes: 10 additions & 10 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import pkgConf from "pkg-conf";

import type { ApplicationFunction, Options, ServerOptions } from "./types";
import { Probot } from "./index";
import { setupAppFactory } from "./apps/setup";
import { getLog } from "./helpers/get-log";
import { readCliOptions } from "./bin/read-cli-options";
import { readEnvOptions } from "./bin/read-env-options";
import { Server } from "./server/server";
import { defaultApp } from "./apps/default";
import { resolveAppFunction } from "./helpers/resolve-app-function";
import { isProduction } from "./helpers/is-production";
import type { ApplicationFunction, Options, ServerOptions } from "./types.js";
import { Probot } from "./index.js";
import { setupAppFactory } from "./apps/setup.js";
import { getLog } from "./helpers/get-log.js";
import { readCliOptions } from "./bin/read-cli-options.js";
import { readEnvOptions } from "./bin/read-env-options.js";
import { Server } from "./server/server.js";
import { defaultApp } from "./apps/default.js";
import { resolveAppFunction } from "./helpers/resolve-app-function.js";
import { isProduction } from "./helpers/is-production.js";
import { config as dotenvConfig } from "dotenv";

type AdditionalOptions = {
Expand Down
2 changes: 1 addition & 1 deletion src/server/logging-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pinoHttp, { startTime, type Options, type HttpLogger } from "pino-http";
import { pinoHttp, startTime, type Options, type HttpLogger } from "pino-http";
import type { Logger } from "pino";
import { randomUUID as uuidv4 } from "crypto";

Expand Down
10 changes: 5 additions & 5 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { join } from "path";
import type { Logger } from "pino";
import { createNodeMiddleware as createWebhooksMiddleware } from "@octokit/webhooks";

import { getLoggingMiddleware } from "./logging-middleware";
import { createWebhookProxy } from "../helpers/webhook-proxy";
import { VERSION } from "../version";
import type { ApplicationFunction, ServerOptions } from "../types";
import type { Probot } from "../";
import { getLoggingMiddleware } from "./logging-middleware.js";
import { createWebhookProxy } from "../helpers/webhook-proxy.js";
import { VERSION } from "../version.js";
import type { ApplicationFunction, ServerOptions } from "../types.js";
import type { Probot } from "../index.js";
import type EventSource from "eventsource";

// the default path as defined in @octokit/webhooks
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import type { LRUCache } from "lru-cache";
import type { RedisOptions } from "ioredis";
import type { Options as LoggingOptions } from "pino-http";

import { Probot } from "./index";
import { Context } from "./context";
import { ProbotOctokit } from "./octokit/probot-octokit";
import { Probot } from "./index.js";
import { Context } from "./context.js";
import { ProbotOctokit } from "./octokit/probot-octokit.js";

import type { Logger } from "pino";
import type { RequestRequestOptions } from "@octokit/types";
Expand Down
6 changes: 3 additions & 3 deletions test/apps/default.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Stream from "stream";

import pino from "pino";
import { pino } from "pino";
import request from "supertest";
import { describe, expect, it } from "vitest";

import { Probot, Server } from "../../src";
import { defaultApp } from "../../src/apps/default";
import { Probot, Server } from "../../src/index.js";
import { defaultApp } from "../../src/apps/default.js";

describe("default app", () => {
let output = [];
Expand Down

0 comments on commit f5ee0de

Please sign in to comment.