Skip to content

Commit 203f0b7

Browse files
authored
Remove APIs and group single entrypoint (#109)
* Remove APIs and group single entrypoint * Update package.json * Apply suggestions from code review Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com> * Format
1 parent ead1e55 commit 203f0b7

12 files changed

Lines changed: 94 additions & 279 deletions

.changeset/humble-weeks-watch.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@changesets/ghcommit": major
3+
---
4+
5+
Removed the `commitFilesFromBuffers` and `commitFilesFromDirectory` APIs. These APIs were simple wrappers over the core `commitChangesFromBase64` API, which should be used instead. Read the file and pass the base64-encoded content to `commitChangesFromBase64` `fileChanges` directly. For example:
6+
7+
- `Buffer.from("hello world").toString("base64")`
8+
- `await fs.readFile("path/to/file", "base64")`

.changeset/slick-bottles-try.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/ghcommit": major
3+
---
4+
5+
Removed all subpath exports. All APIs should be imported from root, e.g. `import { commitChangesFromRepo } from "@changesets/ghcommit"`.

README.md

Lines changed: 60 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,14 @@ pnpm install @changesets/ghcommit
4040

4141
### Usage in github actions
4242

43-
All functions in this library that interact with the GitHub API require an octokit client that can execute GraphQL. If you are writing code that is designed to be run from within a GitHub Action, this can be done using the `@actions.github` library:
43+
All functions in this library that interact with the GitHub API require an octokit client that can execute GraphQL. If you are writing code that is designed to be run from within a GitHub Action, this can be done using the `@actions/github` library:
4444

4545
```ts
4646
import { getOctokit } from "@actions/github";
4747

4848
const octokit = getOctokit(process.env.GITHUB_TOKEN);
4949
```
5050

51-
### Importing specific modules
52-
53-
To allow for you to produce smaller bundle sizes, the functionality exposed in this package is grouped into specific modules that only import the packages required for their use. We recommend that you import from the specific modules rather than the root of the package.
54-
5551
## API
5652

5753
All the functions below accept a single object as its argument, and share the following base arguments:
@@ -76,8 +72,66 @@ All the functions below accept a single object as its argument, and share the fo
7672
}
7773
```
7874

75+
### `commitFilesFromBase64`
76+
77+
> Works in Node.js and browsers
78+
79+
This function will add or delete specific files from a repository's branch based on the given `fileChanges` argument.
80+
81+
In addition to `CommitFilesBasedArgs`, this function has the following arguments:
82+
83+
```ts
84+
{
85+
/**
86+
* The current branch, tag or commit that the new branch should be based on.
87+
*/
88+
base: GitBase;
89+
/**
90+
* The file paths, relative to git root, to add or delete from the branch on GitHub.
91+
*/
92+
fileChanges: {
93+
/**
94+
* File paths, relative to git root, to add to the repo. Content is a base64-encoded string.
95+
*/
96+
additions?: { path: string; content: string }[];
97+
/**
98+
* File paths, relative to git root, to remove from the repo.
99+
*/
100+
deletions?: { path: string }[];
101+
};
102+
}
103+
```
104+
105+
Example:
106+
107+
```ts
108+
import fs from "node:fs/promises";
109+
import { context, getOctokit } from "@actions/github";
110+
import { commitFilesFromBase64 } from "@changesets/ghcommit";
111+
112+
const octokit = getOctokit(process.env.GITHUB_TOKEN);
113+
114+
// Commit the changes to README.md based on the main branch
115+
await commitFilesFromBase64({
116+
octokit,
117+
...context.repo,
118+
branch: "new-branch-to-create",
119+
message: "[chore] do something",
120+
base: {
121+
branch: "main",
122+
},
123+
fileChanges: {
124+
additions: [
125+
{ path: "README.md", content: await fs.readFile("README.md", "base64") },
126+
],
127+
},
128+
});
129+
```
130+
79131
### `commitChangesFromRepo`
80132

133+
> Works in Node.js only
134+
81135
This function will take an existing repository on your filesystem (defaulting to the current working directory). This function is good to use if you're usually working within the context of a git repository, such as after running `@actions/checkout` in github actions.
82136

83137
In addition to `CommitFilesBasedArgs`, this function has the following arguments:
@@ -121,7 +175,7 @@ Example:
121175

122176
```ts
123177
import { context, getOctokit } from "@actions/github";
124-
import { commitChangesFromRepo } from "@changesets/ghcommit/git";
178+
import { commitChangesFromRepo } from "@changesets/ghcommit";
125179

126180
const octokit = getOctokit(process.env.GITHUB_TOKEN);
127181

@@ -165,130 +219,6 @@ await commitChangesFromRepo({
165219
});
166220
```
167221

168-
### `commitFilesFromDirectory`
169-
170-
This function will add or delete specific files from a repository's branch based on files found on the local filesystem. This is good to use when there are specific files that need to be updated on a branch, or if many changes may have been made locally, but only some files need to be pushed.
171-
172-
In addition to `CommitFilesBasedArgs`, this function has the following arguments:
173-
174-
```ts
175-
{
176-
/**
177-
* The current branch, tag or commit that the new branch should be based on.
178-
*/
179-
base: GitBase;
180-
/**
181-
* The directory to consider the root of the repository when calculating
182-
* file paths
183-
*/
184-
cwd: string;
185-
/**
186-
* The file paths, relative to {@link workingDirectory},
187-
* to add or delete from the branch on GitHub.
188-
*/
189-
fileChanges: {
190-
/** File paths, relative to {@link workingDirectory}, to remove from the repo. */
191-
additions?: string[];
192-
/** File paths, relative to the repository root, to remove from the repo. */
193-
deletions?: string[];
194-
};
195-
}
196-
```
197-
198-
Example:
199-
200-
```ts
201-
import { context, getOctokit } from "@actions/github";
202-
import { commitFilesFromDirectory } from "@changesets/ghcommit/fs";
203-
204-
const octokit = getOctokit(process.env.GITHUB_TOKEN);
205-
206-
// Commit the changes to package.json and package-lock.json
207-
// based on the main branch
208-
await commitFilesFromDirectory({
209-
octokit,
210-
...context.repo,
211-
branch: "new-branch-to-create",
212-
message: "[chore] do something",
213-
base: {
214-
branch: "main",
215-
},
216-
cwd: "foo/bar",
217-
fileChanges: {
218-
additions: ["package-lock.json", "package.json"],
219-
},
220-
});
221-
222-
// Push just the index.html file to a new branch called docs, based off the tag v1.0.0
223-
await commitFilesFromDirectory({
224-
octokit,
225-
...context.repo,
226-
branch: "docs",
227-
message: "[chore] do something",
228-
force: true, // Overwrite any existing branch
229-
base: {
230-
tag: "v1.0.0",
231-
},
232-
cwd: "some-dir",
233-
fileChanges: {
234-
additions: ["index.html"],
235-
},
236-
});
237-
```
238-
239-
### `commitFilesFromBuffers`
240-
241-
This function will add or delete specific files from a repository's branch based on Node.js `Buffers` that can be any binary data in memory. This is useful for when you want to make changes to a repository / branch without cloning a repo or interacting with a filesystem.
242-
243-
In addition to `CommitFilesBasedArgs`, this function has the following arguments:
244-
245-
```ts
246-
{
247-
/**
248-
* The current branch, tag or commit that the new branch should be based on.
249-
*/
250-
base: GitBase;
251-
/**
252-
* The file changes, relative to the repository root, to make to the specified branch.
253-
*/
254-
fileChanges: {
255-
additions?: Array<{
256-
path: string;
257-
contents: Buffer;
258-
}>;
259-
deletions?: string[];
260-
};
261-
}
262-
```
263-
264-
Example:
265-
266-
```ts
267-
import { context, getOctokit } from "@actions/github";
268-
import { commitFilesFromBuffers } from "@changesets/ghcommit/node";
269-
270-
const octokit = getOctokit(process.env.GITHUB_TOKEN);
271-
272-
// Add a file called hello-world
273-
await commitFilesFromBuffers({
274-
octokit,
275-
...context.repo,
276-
branch: "new-branch-to-create",
277-
message: "[chore] do something",
278-
base: {
279-
branch: "main",
280-
},
281-
fileChanges: {
282-
additions: [
283-
{
284-
path: "hello/world.txt",
285-
contents: Buffer.alloc(1024, "Hello, world!"),
286-
},
287-
],
288-
},
289-
});
290-
```
291-
292222
## Known Limitations
293223

294224
Due to using the GitHub API to make changes to repository contents,

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
"type": "module",
2424
"sideEffects": false,
2525
"exports": {
26-
".": "./dist/index.mjs",
27-
"./core": "./dist/core.mjs",
28-
"./fs": "./dist/fs.mjs",
29-
"./git": "./dist/git.mjs",
30-
"./node": "./dist/node.mjs",
26+
".": {
27+
"types": "./dist/index-node.d.mts",
28+
"browser": "./dist/index-browser.mjs",
29+
"default": "./dist/index-node.mjs"
30+
},
3131
"./package.json": "./package.json"
3232
},
3333
"publishConfig": {

src/fs.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/index-browser.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export { commitFilesFromBase64 } from "./core.ts";
2+
3+
export function commitChangesFromRepo() {
4+
throw new Error(
5+
"commitChangesFromRepo is not supported in non-Node.js environments",
6+
);
7+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export { commitFilesFromBase64 } from "./core.ts";
22
export { commitChangesFromRepo } from "./git.ts";
3-
export { commitFilesFromDirectory } from "./fs.ts";

src/interface.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,37 +46,6 @@ export interface CommitFilesFromBase64Args extends CommitFilesSharedArgsWithBase
4646
fileChanges: FileChanges;
4747
}
4848

49-
export interface CommitFilesFromBuffersArgs extends CommitFilesSharedArgsWithBase {
50-
/**
51-
* The file changes, relative to the repository root, to make to the specified branch.
52-
*/
53-
fileChanges: {
54-
additions?: Array<{
55-
path: string;
56-
contents: Buffer;
57-
}>;
58-
deletions?: string[];
59-
};
60-
}
61-
62-
export interface CommitFilesFromDirectoryArgs extends CommitFilesSharedArgsWithBase {
63-
/**
64-
* The directory to consider the root of the repository when calculating
65-
* file paths
66-
*/
67-
cwd: string;
68-
/**
69-
* The file paths, relative to {@link cwd},
70-
* to add or delete from the branch on GitHub.
71-
*/
72-
fileChanges: {
73-
/** File paths, relative to {@link cwd}, to remove from the repo. */
74-
additions?: string[];
75-
/** File paths, relative to the repository root, to remove from the repo. */
76-
deletions?: string[];
77-
};
78-
}
79-
8049
export interface CommitChangesFromRepoArgs extends CommitFilesBasedArgs {
8150
/**
8251
* The directory used to find the repository root,

src/node.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)