diff --git a/src/cli/common.js b/src/cli/common.js new file mode 100644 index 0000000..8f9781b --- /dev/null +++ b/src/cli/common.js @@ -0,0 +1,26 @@ +// @flow +// Configuration and environment variables used by the CLI. + +import os from "os"; +import path from "path"; + +import * as NullUtil from "../util/null"; + +export type PluginName = "git" | "github"; + +export function defaultPlugins(): PluginName[] { + return ["git", "github"]; +} + +export function defaultSourcecredDirectory() { + return path.join(os.tmpdir(), "sourcecred"); +} + +export function sourcecredDirectory(): string { + const env = process.env.SOURCECRED_DIRECTORY; + return env != null ? env : defaultSourcecredDirectory(); +} + +export function githubToken(): string | null { + return NullUtil.orElse(process.env.SOURCECRED_GITHUB_TOKEN, null); +} diff --git a/src/cli/common.test.js b/src/cli/common.test.js new file mode 100644 index 0000000..df03101 --- /dev/null +++ b/src/cli/common.test.js @@ -0,0 +1,57 @@ +// @flow + +import path from "path"; + +import { + defaultPlugins, + defaultSourcecredDirectory, + sourcecredDirectory, + githubToken, +} from "./common"; + +describe("cli/common", () => { + beforeEach(() => { + jest + .spyOn(require("os"), "tmpdir") + .mockReturnValue(path.join("/", "your", "tmpdir")); + }); + + describe("defaultPlugins", () => { + it("gives an array including the Git plugin name", () => { + expect(defaultPlugins()).toEqual(expect.arrayContaining(["git"])); + }); + }); + + describe("defaultSourcecredDirectory", () => { + it("gives a file under the OS's temporary directory", () => { + expect(defaultSourcecredDirectory()).toEqual( + path.join("/", "your", "tmpdir", "sourcecred") + ); + }); + }); + + describe("sourcecredDirectory", () => { + it("uses the environment variable when available", () => { + const dir = path.join("/", "my", "sourcecred"); + process.env.SOURCECRED_DIRECTORY = dir; + expect(sourcecredDirectory()).toEqual(dir); + }); + it("uses the default directory if no environment variable is set", () => { + delete process.env.SOURCECRED_DIRECTORY; + expect(sourcecredDirectory()).toEqual( + path.join("/", "your", "tmpdir", "sourcecred") + ); + }); + }); + + describe("githubToken", () => { + it("uses the environment variable when available", () => { + process.env.SOURCECRED_GITHUB_TOKEN = "010101"; + expect(githubToken()).toEqual("010101"); + }); + it("returns `null` if the environment variable is not set", () => { + delete process.env.SOURCECRED_GITHUB_TOKEN; + expect(githubToken()).toBe(null); + }); + }); +});