Skip to content

Commit

Permalink
feat: cwd option (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Nov 18, 2020
1 parent 13d0a16 commit 8fc3372
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
20 changes: 11 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from "path";
import { existsSync, readdirSync, readFileSync } from "fs";

import isBase64 from "is-base64";
Expand All @@ -11,15 +12,17 @@ type Options = {
PRIVATE_KEY_PATH?: string;
[key: string]: string | undefined;
};
cwd?: string;
};

export function getPrivateKey(options: Options = {}): string | null {
const env = options.env || process.env;
const cwd = options.cwd || process.cwd();

if (options.filepath) {
return readFileSync(options.filepath, "utf-8");
return readFileSync(resolve(cwd, options.filepath), "utf-8");
}

const env = options.env || process.env;

if (env.PRIVATE_KEY) {
let privateKey = env.PRIVATE_KEY;

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

if (env.PRIVATE_KEY_PATH) {
if (existsSync(env.PRIVATE_KEY_PATH)) {
return readFileSync(env.PRIVATE_KEY_PATH, "utf-8");
const filepath = resolve(cwd, env.PRIVATE_KEY_PATH);
if (existsSync(filepath)) {
return readFileSync(filepath, "utf-8");
} else {
throw new Error(
`[@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.`
);
}
}
const pemFiles = readdirSync(process.cwd()).filter((path) =>
path.endsWith(".pem")
);
const pemFiles = readdirSync(cwd).filter((path) => path.endsWith(".pem"));
if (pemFiles.length > 1) {
const paths = pemFiles.join(", ");
throw new Error(
`[@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`
);
} else if (pemFiles[0]) {
return getPrivateKey({ filepath: pemFiles[0] });
return getPrivateKey({ filepath: pemFiles[0], cwd });
}
return null;
}
Expand Down
42 changes: 35 additions & 7 deletions test/get-private-key.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
jest.mock("fs");

import { resolve } from "path";
import fs from "fs";

import { getPrivateKey } from "../src";
Expand Down Expand Up @@ -32,7 +33,10 @@ describe("getPrivateKey", () => {
it("{ filepath } option", () => {
const result = getPrivateKey({ filepath: "test.pem" });
expect(readFileSync).toHaveBeenCalledTimes(1);
expect(readFileSync).toHaveBeenCalledWith("test.pem", "utf-8");
expect(readFileSync).toHaveBeenCalledWith(
resolve(process.cwd(), "test.pem"),
"utf-8"
);
expect(result).toEqual("test.pem content");
});

Expand All @@ -52,10 +56,15 @@ describe("getPrivateKey", () => {
},
});
expect(existsSync).toHaveBeenCalledTimes(1);
expect(existsSync).toHaveBeenCalledWith("test.pem");
expect(existsSync).toHaveBeenCalledWith(
resolve(process.cwd(), "test.pem")
);

expect(readFileSync).toHaveBeenCalledTimes(1);
expect(readFileSync).toHaveBeenCalledWith("test.pem", "utf-8");
expect(readFileSync).toHaveBeenCalledWith(
resolve(process.cwd(), "test.pem"),
"utf-8"
);
expect(result).toEqual("test.pem content");
});

Expand All @@ -65,15 +74,21 @@ describe("getPrivateKey", () => {
env: { PRIVATE_KEY },
});
expect(readFileSync).toHaveBeenCalledTimes(1);
expect(readFileSync).toHaveBeenCalledWith("test.pem", "utf-8");
expect(readFileSync).toHaveBeenCalledWith(
resolve(process.cwd(), "test.pem"),
"utf-8"
);
expect(result).toEqual("test.pem content");
});

it("single test.pem file in current working directory", () => {
readdirSync.mockReturnValue(["test.pem"]);
const result = getPrivateKey();
expect(readFileSync).toHaveBeenCalledTimes(1);
expect(readFileSync).toHaveBeenCalledWith("test.pem", "utf-8");
expect(readFileSync).toHaveBeenCalledWith(
resolve(process.cwd(), "test.pem"),
"utf-8"
);
expect(result).toEqual("test.pem content");
});

Expand All @@ -83,6 +98,14 @@ describe("getPrivateKey", () => {
`[@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`
);
});

it("{ cwd }", () => {
const result = getPrivateKey({
cwd: "/app/current",
});
expect(result).toEqual(null);
expect(readdirSync).toHaveBeenCalledWith("/app/current");
});
});

describe("with environment variables", () => {
Expand Down Expand Up @@ -111,10 +134,15 @@ describe("getPrivateKey", () => {
process.env.PRIVATE_KEY_PATH = "test.pem";
const result = getPrivateKey();
expect(existsSync).toHaveBeenCalledTimes(1);
expect(existsSync).toHaveBeenCalledWith("test.pem");
expect(existsSync).toHaveBeenCalledWith(
resolve(process.cwd(), "test.pem")
);

expect(readFileSync).toHaveBeenCalledTimes(1);
expect(readFileSync).toHaveBeenCalledWith("test.pem", "utf-8");
expect(readFileSync).toHaveBeenCalledWith(
resolve(process.cwd(), "test.pem"),
"utf-8"
);
expect(result).toEqual("test.pem content");
});

Expand Down

0 comments on commit 8fc3372

Please sign in to comment.