Skip to content

Commit 2f244fd

Browse files
authored
feat: make @octokit/openapi-webhooks ESM (#109)
* feat: make `@octokit/openapi-webhooks` ESM BREAKING CHANGE: The schema package is now ESM only BREAKING CHANGE: Require Node v18.20+ or >= 20.10 due to import attributes * fix: return `.default` and not the whole import
1 parent 171fe25 commit 2f244fd

File tree

6 files changed

+66
-23
lines changed

6 files changed

+66
-23
lines changed

packages/openapi-webhooks/index.js

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1-
module.exports = {
2-
schemas: {
3-
["api.github.com"]: require("./generated/api.github.com.json"),
4-
["ghec"]: require("./generated/ghec.json"),
5-
["ghes-3.10"]: require("./generated/ghes-3.10.json"),
6-
["ghes-3.11"]: require("./generated/ghes-3.11.json"),
7-
["ghes-3.12"]: require("./generated/ghes-3.12.json"),
8-
["ghes-3.13"]: require("./generated/ghes-3.13.json"),
9-
["ghes-3.14"]: require("./generated/ghes-3.14.json"),
10-
["ghes-3.15"]: require("./generated/ghes-3.15.json"),
11-
},
1+
export const schemas = {
2+
["api.github.com"]: (
3+
await import("./generated/api.github.com.json", { with: { type: "json" } })
4+
).default,
5+
["ghec"]: (await import("./generated/ghec.json", { with: { type: "json" } }))
6+
.default,
7+
["ghes-3.10"]: (
8+
await import("./generated/ghes-3.10.json", { with: { type: "json" } })
9+
).default,
10+
["ghes-3.11"]: (
11+
await import("./generated/ghes-3.11.json", { with: { type: "json" } })
12+
).default,
13+
["ghes-3.12"]: (
14+
await import("./generated/ghes-3.12.json", { with: { type: "json" } })
15+
).default,
16+
["ghes-3.13"]: (
17+
await import("./generated/ghes-3.13.json", { with: { type: "json" } })
18+
).default,
19+
["ghes-3.14"]: (
20+
await import("./generated/ghes-3.14.json", { with: { type: "json" } })
21+
).default,
22+
["ghes-3.15"]: (
23+
await import("./generated/ghes-3.15.json", { with: { type: "json" } })
24+
).default,
1225
};

packages/openapi-webhooks/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"generated/*",
88
"index.js"
99
],
10-
"type": "commonjs",
10+
"type": "module",
1111
"repository": {
1212
"type": "git",
1313
"url": "git+https://github.com/octokit/openapi-webhooks.git",
@@ -24,5 +24,8 @@
2424
"publishConfig": {
2525
"access": "public",
2626
"provenance": true
27+
},
28+
"engines": {
29+
"node": "^18.20.0 || >= 20.10.0"
2730
}
2831
}

packages/openapi-webhooks/types.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export declare const schemas: {
2+
"api.github.com": Record<string, unknown>;
3+
ghec: Record<string, unknown>;
4+
"ghes-3.10": Record<string, unknown>;
5+
"ghes-3.11": Record<string, unknown>;
6+
"ghes-3.12": Record<string, unknown>;
7+
"ghes-3.13": Record<string, unknown>;
8+
"ghes-3.14": Record<string, unknown>;
9+
"ghes-3.15": Record<string, unknown>;
10+
};

scripts/build.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,17 @@ async function run() {
128128
}
129129

130130
let schemasCode = "";
131+
let schemasTypes = "";
131132

132133
for (const name of schemaFileNames) {
133134
schemasCode += `["${name.replace(
134135
".json",
135136
"",
136-
)}"]: require("./generated/${name}"),`;
137+
)}"]: (await import("./generated/${name}", { with: { type: "json" } })).default,`;
138+
schemasTypes += `"${name.replace(
139+
".json",
140+
"",
141+
)}": Record<string, unknown>;`;
137142
}
138143

139144
writeFileSync(
@@ -149,17 +154,26 @@ They are all generated, your changes would be overwritten with the next update.
149154
"packages/openapi-webhooks/index.js",
150155
await prettier.format(
151156
`
152-
module.exports = {
153-
schemas: {
154-
${schemasCode}
155-
}
157+
export const schemas = {
158+
${schemasCode}
156159
}
157160
`,
158161
{
159162
parser: "babel",
160163
},
161164
),
162165
);
166+
writeFileSync(`packages/openapi-webhooks/types.d.ts`, await prettier.format(
167+
`
168+
export declare const schemas: {
169+
${schemasTypes}
170+
}
171+
`,
172+
{
173+
parser: "typescript",
174+
},
175+
)
176+
);
163177
writeFileSync(
164178
`packages/openapi-webhooks/package.json`,
165179
await prettier.format(
@@ -170,7 +184,7 @@ They are all generated, your changes would be overwritten with the next update.
170184
"GitHub's official Webhooks OpenAPI spec with Octokit extensions",
171185
main: "index.js",
172186
files: ["generated/*", "index.js"],
173-
type: "commonjs",
187+
type: "module",
174188
repository: {
175189
type: "git",
176190
url: "git+https://github.com/octokit/openapi-webhooks.git",
@@ -183,6 +197,9 @@ They are all generated, your changes would be overwritten with the next update.
183197
access: "public",
184198
provenance: true,
185199
},
200+
engines: {
201+
node: "^18.20.0 || >= 20.10.0",
202+
}
186203
}),
187204
{ parser: "json-stringify" },
188205
),

scripts/download.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { get } from "https";
2-
import fs from "fs";
1+
import { get } from "node:https";
2+
import fs from "node:fs";
33

44
import { Octokit } from "@octokit/core";
55
import { getCurrentVersions } from "github-enterprise-server-versions";

scripts/overrides/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { readFileSync } from "fs";
2-
import { resolve, join, dirname } from "path";
3-
import { fileURLToPath } from "url";
1+
import { readFileSync } from "node:fs";
2+
import { resolve, join, dirname } from "node:path";
3+
import { fileURLToPath } from "node:url";
44

55
const SUPPORTED_GHES_OPERATIONS = [
66
"3.10",

0 commit comments

Comments
 (0)