@abhagsain/ai-cli
Version:
Get answers for CLI commands from GPT3 right from your terminal
40 lines (39 loc) • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const inquirer_1 = tslib_1.__importDefault(require("inquirer"));
const index_1 = require("../helpers/index");
class Auth extends core_1.Command {
async run() {
const existingAPIKey = await (0, index_1.getOpenAIKey)(this.config.configDir);
const filePath = (0, index_1.getAPIConfigFilePath)(this.config.configDir);
const message = existingAPIKey
? "Please enter your OpenAI API Key (This would overwrite the existing key)"
: "Please enter your OpenAI API Key";
const prompt = await inquirer_1.default.prompt([
{
name: "userAPIKey",
message,
type: "password",
validate: (value) => {
if (!value.trim()) {
return "Please enter a valid API key";
}
return true;
},
},
]);
const { userAPIKey } = prompt;
fs_extra_1.default.ensureFileSync(filePath);
if (userAPIKey) {
fs_extra_1.default.writeFileSync(filePath, `OPENAI_API_KEY=${userAPIKey}`);
}
this.log(`API Key is saved at ${chalk_1.default.bold.yellowBright(filePath)}`);
}
}
exports.default = Auth;
Auth.description = "Update existing or add new OpenAI API Key";
Auth.examples = ["<%= config.bin %> <%= command.id %> (Follow the prompt)"];