"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var hint = "please use:\n  * `--private-key=/path/to/private-key` flag, or\n  * `PRIVATE_KEY` environment variable, or\n  * `PRIVATE_KEY_PATH` environment variable\n";
/**
 * Finds a private key through various user-(un)specified methods.
 * Order of precedence:
 * 1. Explicit path (CLI option)
 * 2. `PRIVATE_KEY` env var
 * 3. `PRIVATE_KEY_PATH` env var
 * 4. Any file w/ `.pem` extension in current working dir
 * @param {string} [filepath] - Explicit, user-defined path to keyfile
 * @returns {string} Private key
 * @private
 */
function findPrivateKey(filepath) {
    if (filepath) {
        return fs.readFileSync(filepath);
    }
    if (process.env.PRIVATE_KEY) {
        return process.env.PRIVATE_KEY.replace(/\\n/g, '\n');
    }
    if (process.env.PRIVATE_KEY_PATH) {
        return fs.readFileSync(process.env.PRIVATE_KEY_PATH);
    }
    var pemFiles = fs.readdirSync(process.cwd())
        .filter(function (path) { return path.endsWith('.pem'); });
    if (pemFiles.length > 1) {
        throw new Error("Found several private keys: " + pemFiles.join(', ') + ". " +
            ("To avoid ambiguity " + hint));
    }
    else if (pemFiles[0]) {
        return findPrivateKey(pemFiles[0]);
    }
    throw new Error("Missing private key for GitHub App, " + hint);
}
module.exports = {
    findPrivateKey: findPrivateKey
};
//# sourceMappingURL=private-key.js.map