Skip to content

Commit 4117e39

Browse files
authored
Remove any use of process.cwd(), and clean up directory options across functions (#40)
* Clean up arguments for multiple functions - Use `cwd` consistently across functions - Remove unnecessary extra directory args for `commitChangesFromRepo` - Introduce `recursivelyFindRoot` argument * Fix node tests
1 parent b3e603b commit 4117e39

File tree

10 files changed

+260
-462
lines changed

10 files changed

+260
-462
lines changed

.changeset/blue-carpets-jam.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
"@changesets/ghcommit": major
3+
---
4+
5+
Refactor & clean up options for multiple functions
6+
7+
- For `commitFilesFromDirectory`:
8+
- Rename `workingDirectory` to `cwd` for consistency across repos,
9+
and utils like `exec`
10+
- Make `cwd` a required argument
11+
- For `commitChangesFromRepo`:
12+
- Merge `repoDirectory` and `addFromDirectory` into a single required argument
13+
`cwd`. This folder will now both be used to filter which files are added,
14+
and to find the root of the repository.
15+
- Introduce `recursivelyFindRoot` option (default: `true`),
16+
to optionally search for the root of the repository,
17+
by checking for existence of `.git` directory in parent directories,
18+
starting from `cwd`.
19+
20+
This effectively removes all usage of process.cwd() within the package,
21+
instead requiring all usage to be very explicit with specifying paths.

README.md

+18-17
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ In addition to `CommitFilesBasedArgs`, this function has the following arguments
8282

8383
```ts
8484
{
85+
/**
86+
* The directory used to find the repository root,
87+
* and search for changed files to commit.
88+
*
89+
* Any files that have been changed outside of this directory will be ignored.
90+
*/
91+
cwd: string;
8592
/**
8693
* The base commit to build your changes on-top of
8794
*
@@ -91,21 +98,13 @@ In addition to `CommitFilesBasedArgs`, this function has the following arguments
9198
commit: string;
9299
};
93100
/**
94-
* The root of the repository.
95-
*
96-
* When unspecified, the root of the repository will be found by recursively
97-
* searching for the `.git` directory from the current working directory.
98-
*/
99-
repoDirectory?: string;
100-
/**
101-
* The starting directory to recurse from when detecting changed files.
102-
*
103-
* Useful for monorepos where you want to add files from a specific directory only.
101+
* Don't require {@link cwd} to be the root of the repository,
102+
* and use it as a starting point to recursively search for the `.git`
103+
* directory in parent directories.
104104
*
105-
* Defaults to resolved value of {@link repoDirectory},
106-
* which will add all changed files in the repository.
105+
* @default true
107106
*/
108-
addFromDirectory?: string;
107+
recursivelyFindRoot?: boolean;
109108
/**
110109
* An optional function that can be used to filter which files are included
111110
* in the commit. True should be returned for files that should be included.
@@ -131,6 +130,7 @@ await commitChangesFromRepo({
131130
...context.repo,
132131
branch: "new-branch-to-create",
133132
message: "[chore] do something",
133+
cwd: process.cwd(),
134134
});
135135

136136
// Commit & push the files from a specific directory
@@ -141,7 +141,7 @@ await commitChangesFromRepo({
141141
repository: "my-repo",
142142
branch: "another-new-branch-to-create",
143143
message: "[chore] do something else\n\nsome more details",
144-
repoDirectory: "/tmp/some-repo",
144+
cwd: "/tmp/some-repo",
145145
});
146146

147147
// Commit & push the files from the current directory,
@@ -154,6 +154,7 @@ await commitChangesFromRepo({
154154
headline: "[chore] do something else",
155155
body: "some more details",
156156
},
157+
cwd: process.cwd(),
157158
base: {
158159
// This will be the original sha from the workflow run,
159160
// even if we've made commits locally
@@ -178,7 +179,7 @@ In addition to `CommitFilesBasedArgs`, this function has the following arguments
178179
* The directory to consider the root of the repository when calculating
179180
* file paths
180181
*/
181-
workingDirectory?: string;
182+
cwd: string;
182183
/**
183184
* The file paths, relative to {@link workingDirectory},
184185
* to add or delete from the branch on GitHub.
@@ -210,7 +211,7 @@ await commitFilesFromDirectory({
210211
base: {
211212
branch: "main",
212213
},
213-
workingDirectory: "foo/bar",
214+
cwd: "foo/bar",
214215
fileChanges: {
215216
additions: ["package-lock.json", "package.json"],
216217
},
@@ -226,7 +227,7 @@ await commitFilesFromDirectory({
226227
base: {
227228
tag: "v1.0.0",
228229
},
229-
workingDirectory: "some-dir",
230+
cwd: "some-dir",
230231
fileChanges: {
231232
additions: ["index.html"],
232233
},

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
"eslint-plugin-jest": "^28.6.0",
7575
"eslint-plugin-only-warn": "^1.1.0",
7676
"jest": "^29.7.0",
77-
"mock-cwd": "^1.0.0",
7877
"pino": "^9.3.2",
7978
"pino-pretty": "^11.2.2",
8079
"prettier": "^3.3.3",

pnpm-lock.yaml

-32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import {
88
} from "./interface.js";
99

1010
export const commitFilesFromDirectory = async ({
11-
workingDirectory = process.cwd(),
11+
cwd,
1212
fileChanges,
1313
...otherArgs
1414
}: CommitFilesFromDirectoryArgs): Promise<CommitFilesResult> => {
1515
const additions: FileAddition[] = await Promise.all(
1616
(fileChanges.additions || []).map(async (p) => {
1717
return {
1818
path: p,
19-
contents: await fs.readFile(path.join(workingDirectory, p)),
19+
contents: await fs.readFile(path.join(cwd, p)),
2020
};
2121
}),
2222
);

src/git.ts

+11-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
CommitFilesFromBuffersArgs,
77
CommitFilesResult,
88
} from "./interface";
9-
import { isAbsolute, relative } from "path";
9+
import { relative, resolve } from "path";
1010

1111
/**
1212
* @see https://isomorphic-git.org/docs/en/walk#walkerentry-mode
@@ -20,18 +20,20 @@ const FILE_MODES = {
2020

2121
export const commitChangesFromRepo = async ({
2222
base,
23-
repoDirectory,
24-
addFromDirectory,
23+
cwd: workingDirectory,
24+
recursivelyFindRoot = true,
2525
filterFiles,
2626
log,
2727
...otherArgs
2828
}: CommitChangesFromRepoArgs): Promise<CommitFilesResult> => {
2929
const ref = base?.commit ?? "HEAD";
30-
const resolvedRepoDirectory =
31-
repoDirectory ?? (await git.findRoot({ fs, filepath: process.cwd() }));
30+
const cwd = resolve(workingDirectory);
31+
const repoRoot = recursivelyFindRoot
32+
? await git.findRoot({ fs, filepath: cwd })
33+
: cwd;
3234
const gitLog = await git.log({
3335
fs,
34-
dir: resolvedRepoDirectory,
36+
dir: repoRoot,
3537
ref,
3638
depth: 1,
3739
});
@@ -42,18 +44,12 @@ export const commitChangesFromRepo = async ({
4244
throw new Error(`Could not determine oid for ${ref}`);
4345
}
4446

45-
if (addFromDirectory && !isAbsolute(addFromDirectory)) {
46-
throw new Error(
47-
`addFromDirectory must be an absolute path, got ${addFromDirectory}`,
48-
);
49-
}
50-
5147
/**
5248
* The directory to add files from. This is relative to the repository
5349
* root, and is used to filter files.
5450
*/
5551
const relativeStartDirectory =
56-
addFromDirectory && relative(resolvedRepoDirectory, addFromDirectory) + "/";
52+
cwd === repoRoot ? null : relative(repoRoot, cwd) + "/";
5753

5854
// Determine changed files
5955
const trees = [git.TREE({ ref: oid }), git.WORKDIR()];
@@ -65,14 +61,14 @@ export const commitChangesFromRepo = async ({
6561
};
6662
await git.walk({
6763
fs,
68-
dir: resolvedRepoDirectory,
64+
dir: repoRoot,
6965
trees,
7066
map: async (filepath, [commit, workdir]) => {
7167
// Don't include ignored files
7268
if (
7369
await git.isIgnored({
7470
fs,
75-
dir: resolvedRepoDirectory,
71+
dir: repoRoot,
7672
filepath,
7773
})
7874
) {

src/interface.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,27 @@ export interface CommitFilesFromDirectoryArgs
7272
* The directory to consider the root of the repository when calculating
7373
* file paths
7474
*/
75-
workingDirectory?: string;
75+
cwd: string;
7676
/**
77-
* The file paths, relative to {@link workingDirectory},
77+
* The file paths, relative to {@link cwd},
7878
* to add or delete from the branch on GitHub.
7979
*/
8080
fileChanges: {
81-
/** File paths, relative to {@link workingDirectory}, to remove from the repo. */
81+
/** File paths, relative to {@link cwd}, to remove from the repo. */
8282
additions?: string[];
8383
/** File paths, relative to the repository root, to remove from the repo. */
8484
deletions?: string[];
8585
};
8686
}
8787

8888
export interface CommitChangesFromRepoArgs extends CommitFilesBasedArgs {
89+
/**
90+
* The directory used to find the repository root,
91+
* and search for changed files to commit.
92+
*
93+
* Any files that have been changed outside of this directory will be ignored.
94+
*/
95+
cwd: string;
8996
/**
9097
* The base commit to build your changes on-top of.
9198
*
@@ -105,21 +112,13 @@ export interface CommitChangesFromRepoArgs extends CommitFilesBasedArgs {
105112
commit: string;
106113
};
107114
/**
108-
* The root of the repository.
109-
*
110-
* When unspecified, the root of the repository will be found by recursively
111-
* searching for the `.git` directory from the current working directory.
112-
*/
113-
repoDirectory?: string;
114-
/**
115-
* The starting directory to recurse from when detecting changed files.
116-
*
117-
* Useful for monorepos where you want to add files from a specific directory only.
115+
* Don't require {@link cwd} to be the root of the repository,
116+
* and use it as a starting point to recursively search for the `.git`
117+
* directory in parent directories.
118118
*
119-
* Defaults to resolved value of {@link repoDirectory},
120-
* which will add all changed files in the repository.
119+
* @default true
121120
*/
122-
addFromDirectory?: string;
121+
recursivelyFindRoot?: boolean;
123122
/**
124123
* An optional function that can be used to filter which files are included
125124
* in the commit. True should be returned for files that should be included.

src/test/integration/fs.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe("fs", () => {
3737
headline: "Test commit",
3838
body: "This is a test commit",
3939
},
40-
workingDirectory: tmpDir,
40+
cwd: tmpDir,
4141
fileChanges: {
4242
additions: ["foo.txt"],
4343
},

0 commit comments

Comments
 (0)