Use stricter types for `CommitFunctions` and `ChangelogFunctions` (#2… · changesets/changesets@88f2abb · GitHub
Skip to content

Commit 88f2abb

Browse files
authored
Use stricter types for CommitFunctions and ChangelogFunctions (#2040)
* Use stricter types for `CommitFunctions` and `ChangelogFunctions` * Fix lint * Update more places * Fix types
1 parent 92a9571 commit 88f2abb

6 files changed

Lines changed: 38 additions & 22 deletions

File tree

‎.changeset/cozy-knives-brake.md‎

Lines changed: 5 additions & 0 deletions

‎.changeset/huge-flowers-fall.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/changelog-github": patch
3+
---
4+
5+
Improve type-check for options object

‎packages/apply-release-plan/src/get-changelog-entry.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function getChangelogEntry(
3030
releases: ModCompWithPackage[],
3131
changesets: NewChangesetWithCommit[],
3232
changelogFuncs: ChangelogFunctions,
33-
changelogOpts: any,
33+
changelogOpts: null | Record<string, unknown>,
3434
{
3535
updateInternalDependencies,
3636
onlyUpdatePeerDependentsWhenOutOfRange,

‎packages/changelog-github/src/index.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ const changelogFunctions: ChangelogFunctions = {
5151
dependenciesUpdated,
5252
options,
5353
) => {
54-
if (!options.repo) {
54+
const repo = options?.repo;
55+
if (!repo || typeof repo !== "string") {
5556
throw new Error(
5657
'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]',
5758
);
@@ -63,7 +64,7 @@ const changelogFunctions: ChangelogFunctions = {
6364
changesets.map(async (cs) => {
6465
if (cs.commit) {
6566
const { links } = await getInfo({
66-
repo: options.repo,
67+
repo,
6768
commit: cs.commit,
6869
});
6970
return links.commit;
@@ -81,13 +82,15 @@ const changelogFunctions: ChangelogFunctions = {
8182
return [changesetLink, ...updatedDepenenciesList].join("\n");
8283
},
8384
getReleaseLine: async (changeset, type, options) => {
84-
const { GITHUB_SERVER_URL } = await readEnv();
85-
if (!options || !options.repo) {
85+
const repo = options?.repo;
86+
if (!repo || typeof repo !== "string") {
8687
throw new Error(
8788
'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]',
8889
);
8990
}
9091

92+
const { GITHUB_SERVER_URL } = await readEnv();
93+
9194
let prFromSummary: number | undefined;
9295
let commitFromSummary: string | undefined;
9396
const usersFromSummary: string[] = [];
@@ -115,22 +118,22 @@ const changelogFunctions: ChangelogFunctions = {
115118
const links = await (async () => {
116119
if (prFromSummary != null) {
117120
let { links } = await getInfoFromPullRequest({
118-
repo: options.repo,
121+
repo,
119122
pull: prFromSummary,
120123
});
121124
if (commitFromSummary) {
122125
const shortCommitId = commitFromSummary.slice(0, 7);
123126
links = {
124127
...links,
125-
commit: `[\`${shortCommitId}\`](${GITHUB_SERVER_URL}/${options.repo}/commit/${commitFromSummary})`,
128+
commit: `[\`${shortCommitId}\`](${GITHUB_SERVER_URL}/${repo}/commit/${commitFromSummary})`,
126129
};
127130
}
128131
return links;
129132
}
130133
const commitToFetchFrom = commitFromSummary || changeset.commit;
131134
if (commitToFetchFrom) {
132135
const { links } = await getInfo({
133-
repo: options.repo,
136+
repo,
134137
commit: commitToFetchFrom,
135138
});
136139
return links;
@@ -161,13 +164,13 @@ const changelogFunctions: ChangelogFunctions = {
161164

162165
return `\n\n-${prefix ? `${prefix} -` : ""} ${linkifyIssueRefs(firstLine, {
163166
serverUrl: GITHUB_SERVER_URL,
164-
repo: options.repo,
167+
repo,
165168
})}\n${futureLines
166169
.map(
167170
(l) =>
168171
` ${linkifyIssueRefs(l, {
169172
serverUrl: GITHUB_SERVER_URL,
170-
repo: options.repo,
173+
repo,
171174
})}`,
172175
)
173176
.join("\n")}`;

‎packages/cli/src/commit/getCommitFunctions.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import path from "node:path";
22
import { pathToFileURL } from "node:url";
3-
import type { CommitFunctions } from "@changesets/types";
3+
import type { CommitFunctions, Config } from "@changesets/types";
44
import { resolve } from "import-meta-resolve";
55

66
function importResolveFromDir(specifier: string, dir: string) {
77
return resolve(specifier, pathToFileURL(path.join(dir, "x.mjs")).toString());
88
}
99

1010
export async function getCommitFunctions(
11-
commit: false | readonly [string, any],
11+
commit: Config["commit"],
1212
cwd: string,
1313
contextDir: string,
14-
): Promise<[CommitFunctions, any]> {
14+
): Promise<[CommitFunctions, null | Record<string, unknown>]> {
1515
let commitFunctions: CommitFunctions = {};
1616
if (!commit) {
1717
return [commitFunctions, null];
1818
}
19-
const commitOpts: any = commit[1];
19+
const commitOpts = commit[1];
2020
const changesetPath = path.join(cwd, ".changeset");
2121
let commitPath;
2222

‎packages/types/src/index.ts‎

Lines changed: 11 additions & 8 deletions

0 commit comments

Comments
 (0)