Skip to content

Commit 8fc3372

Browse files
authored
feat: cwd option (#2)
1 parent 13d0a16 commit 8fc3372

2 files changed

Lines changed: 46 additions & 16 deletions

File tree

src/index.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { resolve } from "path";
12
import { existsSync, readdirSync, readFileSync } from "fs";
23

34
import isBase64 from "is-base64";
@@ -11,15 +12,17 @@ type Options = {
1112
PRIVATE_KEY_PATH?: string;
1213
[key: string]: string | undefined;
1314
};
15+
cwd?: string;
1416
};
1517

1618
export function getPrivateKey(options: Options = {}): string | null {
19+
const env = options.env || process.env;
20+
const cwd = options.cwd || process.cwd();
21+
1722
if (options.filepath) {
18-
return readFileSync(options.filepath, "utf-8");
23+
return readFileSync(resolve(cwd, options.filepath), "utf-8");
1924
}
2025

21-
const env = options.env || process.env;
22-
2326
if (env.PRIVATE_KEY) {
2427
let privateKey = env.PRIVATE_KEY;
2528

@@ -41,24 +44,23 @@ export function getPrivateKey(options: Options = {}): string | null {
4144
}
4245

4346
if (env.PRIVATE_KEY_PATH) {
44-
if (existsSync(env.PRIVATE_KEY_PATH)) {
45-
return readFileSync(env.PRIVATE_KEY_PATH, "utf-8");
47+
const filepath = resolve(cwd, env.PRIVATE_KEY_PATH);
48+
if (existsSync(filepath)) {
49+
return readFileSync(filepath, "utf-8");
4650
} else {
4751
throw new Error(
4852
`[@probot/get-private-key] Private key does not exists at path: "${env.PRIVATE_KEY_PATH}". Please check to ensure that "env.PRIVATE_KEY_PATH" is correct.`
4953
);
5054
}
5155
}
52-
const pemFiles = readdirSync(process.cwd()).filter((path) =>
53-
path.endsWith(".pem")
54-
);
56+
const pemFiles = readdirSync(cwd).filter((path) => path.endsWith(".pem"));
5557
if (pemFiles.length > 1) {
5658
const paths = pemFiles.join(", ");
5759
throw new Error(
5860
`[@probot/get-private-key] More than one file found: "${paths}". Set { filepath } option or set one of the environment variables: PRIVATE_KEY, PRIVATE_KEY_PATH`
5961
);
6062
} else if (pemFiles[0]) {
61-
return getPrivateKey({ filepath: pemFiles[0] });
63+
return getPrivateKey({ filepath: pemFiles[0], cwd });
6264
}
6365
return null;
6466
}

test/get-private-key.test.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
jest.mock("fs");
22

3+
import { resolve } from "path";
34
import fs from "fs";
45

56
import { getPrivateKey } from "../src";
@@ -32,7 +33,10 @@ describe("getPrivateKey", () => {
3233
it("{ filepath } option", () => {
3334
const result = getPrivateKey({ filepath: "test.pem" });
3435
expect(readFileSync).toHaveBeenCalledTimes(1);
35-
expect(readFileSync).toHaveBeenCalledWith("test.pem", "utf-8");
36+
expect(readFileSync).toHaveBeenCalledWith(
37+
resolve(process.cwd(), "test.pem"),
38+
"utf-8"
39+
);
3640
expect(result).toEqual("test.pem content");
3741
});
3842

@@ -52,10 +56,15 @@ describe("getPrivateKey", () => {
5256
},
5357
});
5458
expect(existsSync).toHaveBeenCalledTimes(1);
55-
expect(existsSync).toHaveBeenCalledWith("test.pem");
59+
expect(existsSync).toHaveBeenCalledWith(
60+
resolve(process.cwd(), "test.pem")
61+
);
5662

5763
expect(readFileSync).toHaveBeenCalledTimes(1);
58-
expect(readFileSync).toHaveBeenCalledWith("test.pem", "utf-8");
64+
expect(readFileSync).toHaveBeenCalledWith(
65+
resolve(process.cwd(), "test.pem"),
66+
"utf-8"
67+
);
5968
expect(result).toEqual("test.pem content");
6069
});
6170

@@ -65,15 +74,21 @@ describe("getPrivateKey", () => {
6574
env: { PRIVATE_KEY },
6675
});
6776
expect(readFileSync).toHaveBeenCalledTimes(1);
68-
expect(readFileSync).toHaveBeenCalledWith("test.pem", "utf-8");
77+
expect(readFileSync).toHaveBeenCalledWith(
78+
resolve(process.cwd(), "test.pem"),
79+
"utf-8"
80+
);
6981
expect(result).toEqual("test.pem content");
7082
});
7183

7284
it("single test.pem file in current working directory", () => {
7385
readdirSync.mockReturnValue(["test.pem"]);
7486
const result = getPrivateKey();
7587
expect(readFileSync).toHaveBeenCalledTimes(1);
76-
expect(readFileSync).toHaveBeenCalledWith("test.pem", "utf-8");
88+
expect(readFileSync).toHaveBeenCalledWith(
89+
resolve(process.cwd(), "test.pem"),
90+
"utf-8"
91+
);
7792
expect(result).toEqual("test.pem content");
7893
});
7994

@@ -83,6 +98,14 @@ describe("getPrivateKey", () => {
8398
`[@probot/get-private-key] More than one file found: \"test1.pem, test2.pem\". Set { filepath } option or set one of the environment variables: PRIVATE_KEY, PRIVATE_KEY_PATH`
8499
);
85100
});
101+
102+
it("{ cwd }", () => {
103+
const result = getPrivateKey({
104+
cwd: "/app/current",
105+
});
106+
expect(result).toEqual(null);
107+
expect(readdirSync).toHaveBeenCalledWith("/app/current");
108+
});
86109
});
87110

88111
describe("with environment variables", () => {
@@ -111,10 +134,15 @@ describe("getPrivateKey", () => {
111134
process.env.PRIVATE_KEY_PATH = "test.pem";
112135
const result = getPrivateKey();
113136
expect(existsSync).toHaveBeenCalledTimes(1);
114-
expect(existsSync).toHaveBeenCalledWith("test.pem");
137+
expect(existsSync).toHaveBeenCalledWith(
138+
resolve(process.cwd(), "test.pem")
139+
);
115140

116141
expect(readFileSync).toHaveBeenCalledTimes(1);
117-
expect(readFileSync).toHaveBeenCalledWith("test.pem", "utf-8");
142+
expect(readFileSync).toHaveBeenCalledWith(
143+
resolve(process.cwd(), "test.pem"),
144+
"utf-8"
145+
);
118146
expect(result).toEqual("test.pem content");
119147
});
120148

0 commit comments

Comments
 (0)