|
1 | 1 | import DataLoader from "dataloader"; |
| 2 | +import { parseEnv } from "node:util"; |
| 3 | +import fs from "node:fs/promises"; |
| 4 | +import path from "node:path"; |
2 | 5 |
|
3 | | -function readEnv() { |
| 6 | +async function readEnvFile() { |
| 7 | + const envFile = path.resolve(process.cwd(), ".env"); |
| 8 | + let content: string | undefined; |
| 9 | + try { |
| 10 | + content = await fs.readFile(envFile, "utf-8"); |
| 11 | + } catch { |
| 12 | + return {}; |
| 13 | + } |
| 14 | + return parseEnv(content); |
| 15 | +} |
| 16 | + |
| 17 | +let cachedEnv: ReturnType<typeof readEnvFile> | undefined; |
| 18 | +function readEnvFileCached() { |
| 19 | + cachedEnv ??= readEnvFile(); |
| 20 | + return cachedEnv; |
| 21 | +} |
| 22 | + |
| 23 | +async function readEnv() { |
4 | 24 | const GITHUB_GRAPHQL_URL = |
5 | | - process.env.GITHUB_GRAPHQL_URL || "https://api.github.com/graphql"; |
| 25 | + process.env.GITHUB_GRAPHQL_URL || |
| 26 | + (await readEnvFileCached()).GITHUB_GRAPHQL_URL || |
| 27 | + "https://api.github.com/graphql"; |
6 | 28 | const GITHUB_SERVER_URL = |
7 | | - process.env.GITHUB_SERVER_URL || "https://github.com"; |
8 | | - const GITHUB_TOKEN = process.env.GITHUB_TOKEN; |
| 29 | + process.env.GITHUB_SERVER_URL || |
| 30 | + (await readEnvFileCached()).GITHUB_SERVER_URL || |
| 31 | + "https://github.com"; |
| 32 | + const GITHUB_TOKEN = |
| 33 | + process.env.GITHUB_TOKEN || (await readEnvFileCached()).GITHUB_TOKEN; |
9 | 34 | return { GITHUB_GRAPHQL_URL, GITHUB_SERVER_URL, GITHUB_TOKEN }; |
10 | 35 | } |
11 | 36 |
|
@@ -33,9 +58,7 @@ function makeQuery(repos: ReposWithCommitsAndPRsToFetch) { |
33 | 58 | ${repos[repo] |
34 | 59 | .map((data) => |
35 | 60 | data.kind === "commit" |
36 | | - ? `a${data.commit}: object(expression: ${JSON.stringify( |
37 | | - data.commit, |
38 | | - )}) { |
| 61 | + ? `a${data.commit}: object(expression: ${JSON.stringify(data.commit)}) { |
39 | 62 | ... on Commit { |
40 | 63 | commitUrl |
41 | 64 | associatedPullRequests(first: 50) { |
@@ -86,7 +109,8 @@ function makeQuery(repos: ReposWithCommitsAndPRsToFetch) { |
86 | 109 | // getReleaseLine will be called a large number of times but it'll be called at the same time |
87 | 110 | // so instead of doing a bunch of network requests, we can do a single one. |
88 | 111 | const GHDataLoader = new DataLoader(async (requests: RequestData[]) => { |
89 | | - const { GITHUB_GRAPHQL_URL, GITHUB_SERVER_URL, GITHUB_TOKEN } = readEnv(); |
| 112 | + const { GITHUB_GRAPHQL_URL, GITHUB_SERVER_URL, GITHUB_TOKEN } = |
| 113 | + await readEnv(); |
90 | 114 | if (!GITHUB_TOKEN) { |
91 | 115 | throw new Error( |
92 | 116 | `Please create a GitHub personal access token at ${GITHUB_SERVER_URL}/settings/tokens/new?scopes=read:user,repo:status&description=changesets-${new Date() |
@@ -129,11 +153,7 @@ const GHDataLoader = new DataLoader(async (requests: RequestData[]) => { |
129 | 153 |
|
130 | 154 | if (data.errors) { |
131 | 155 | throw new Error( |
132 | | - `Fetched data from GitHub returned errors\n${JSON.stringify( |
133 | | - data.errors, |
134 | | - null, |
135 | | - 2, |
136 | | - )}`, |
| 156 | + `Fetched data from GitHub returned errors\n${JSON.stringify(data.errors, null, 2)}`, |
137 | 157 | ); |
138 | 158 | } |
139 | 159 |
|
|
0 commit comments